Published on

Fixing the Python Error Python 3 was configured to use ASCII as encoding for the environment

Authors

Today I was trying to get a pip module installed in a Docker container.

I use the excellent pipenv to help me with my python workflows.

When I tried to install the dependency in a docker container via docker build I ran into the error:

RuntimeError: Click will abort further execution because Python 3 was configured to use ASCII as encoding for the environment. Consult https://click.palletsprojects.com/en/7.x/python3/ for mitigation steps.

This system supports the C.UTF-8 locale which is recommended.
You might be able to resolve your issue by exporting the
following environment variables:

    export LC_ALL=C.UTF-8
    export LANG=C.UTF-8

The full run in Docker can be seen below:

...
RUN cd &&    cd python-sandbox &&    pipenv install --three my-dependency
 ---> Running in f975b889c43d
Traceback (most recent call last):
  File "/usr/local/bin/pipenv", line 11, in <module>
    sys.exit(cli())
  File "/usr/local/lib/python3.6/dist-packages/pipenv/vendor/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/pipenv/vendor/click/core.py", line 696, in main
    _verify_python3_env()
  File "/usr/local/lib/python3.6/dist-packages/pipenv/vendor/click/_unicodefun.py", line 124, in _verify_python3_env
    ' mitigation steps.' + extra
RuntimeError: Click will abort further execution because Python 3 was configured to use ASCII as encoding for the environment. Consult https://click.palletsprojects.com/en/7.x/python3/ for mitigation steps.

This system supports the C.UTF-8 locale which is recommended.
You might be able to resolve your issue by exporting the
following environment variables:

    export LC_ALL=C.UTF-8
    export LANG=C.UTF-8

I tried to fix this using the suggestion of exporting those 2 environment variables which you can do in docker using ENV:

...
ENV LC_ALL="en_US.utf-8"
ENV LANG="en_US.utf-8"
RUN cd && cd python-sandbox && pipenv install --three my-dependency

But this still gave the same error and when I commented out the breaking pipenv install and logged into the container I received the warning:

bash: warning: setlocale: LC_ALL: cannot change locale (en_US.utf-8)

So I tried my luck and changed the pipenv install line to include the setting of the 2 recommended environment variables before running pipenv as below and it worked!

...
RUN cd && \
   cd python-sandbox && \
   LC_ALL=C.UTF-8 LANG=C.UTF-8 pipenv install --three my-dependency

This does not seem to be a dockerfile/ENV issue as I run into the same problem when running these commands manually inside the container.