Click will abort further execution because Python 3 was configured to use ASCII as encoding for the environment
Asked Answered
D

2

65

I downloaded Quokka Python/Flask CMS to a CentOS7 server. Everything works fine with command

sudo python3 manage.py runserver --host 0.0.0.0 --port 80

Then I create a file /etc/init.d/quokkacms. The file contains following code

start() {
        echo -n "Starting quokkacms: "
        python3 /var/www/quokka/manage.py runserver --host 0.0.0.0 --port 80
        touch /var/lock/subsys/quokkacms
        return 0
}
stop() {
        echo -n "Shutting down quokkacms: "
        rm -f /var/lock/subsys/quokkacms
        return 0
}
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)

        ;;
    restart)
        stop
        start
        ;;

    *)
        echo "Usage: quokkacms {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $?

But I get error when running sudo service quokkacms start

RuntimeError: Click will abort further execution because Python 3 was configured to use ASCII as encoding for the environment. Either switch to Python 2 or consult http://click.pocoo.org/python3/ for
mitigation steps.

It seems to me that it is the bash script. How come I get different results? Also I followed instructions in the link in the error message but still had no luck.

[update] I had already tried the solution provided by Click before I posted this question. Check the results below (i run in root):

[root@webserver quokka]# python3
Python 3.4.3 (default, Jan 26 2016, 02:25:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> import codecs
>>> print(locale.getpreferredencoding())
UTF-8
>>> print(codecs.lookup(locale.getpreferredencoding()).name)
utf-8
>>> locale.getdefaultlocale()
('en_US', 'UTF-8')
>>> locale.CODESET
14
>>>
Discreet answered 15/4, 2016 at 15:52 Comment(4)
So did you consult the link given you by the helpful error message? The answer is there. Explicitly.Battat
Hint: the problem is not the initscript itself, but the environment in which the script runs.Battat
FYI for those wondering "why python3 can error out because of something small like unset locale (i.e. env vars LANG, LC_ALL)" --> read PEP 538 and the related PEP 540. The error appears to only be an issue for python 3.0 to 3.6 because PEP 538 fixes the issues for python >= 3.7.Wynd
Does this answer your question? Encoding issue with python3 and click packageWynd
J
112

If you are trying to execute tests case you must set the following environment variables each time:

export LC_ALL=en_US.utf-8
export LANG=en_US.utf-8

Doing this each time will resolve the error.

It may also be possible to set this in your IDE run configuration as

LC_ALL=en_US.UTF-8;LANG=en_US.UTF-8

For example see the following setting in PyCharm 2016:

Jarlen answered 16/6, 2016 at 20:10 Comment(3)
In case that didn't work for you, setting the values as "en_US.UTF-8" worked for me (I upcased the UTF-8).Guam
also for newbies like me in Linux world, adding these env variable into user default will help you after you reboot the machine... ~/.bashrcGroundnut
definitely! adding these to your .bashrc or .zshrc file will always initialize them in your environment.Jarlen
F
11

Adding more to the existing solutions:

If you see something like this error in Python 3:

Traceback (most recent call last):
  ...
RuntimeError: Click will abort further execution because Python 3 was
  configured to use ASCII as encoding for the environment. Either switch
  to Python 2 or consult http://click.pocoo.org/python3/ for
  mitigation steps.

You are dealing with an environment where Python 3 thinks you are restricted to ASCII data. The solution to these problems is different depending on which locale your computer is running in.

For instance, if you have a German Linux machine, you can fix the problem by exporting the locale to de_DE.utf-8:

export LC_ALL=de_DE.utf-8
export LANG=de_DE.utf-8

If you are on a US machine, en_US.utf-8 is the encoding of choice. On some newer Linux systems, you could also try C.UTF-8 as the locale:

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

Taken from the Python 3 Surrogate Handling

Foretoken answered 27/2, 2019 at 10:34 Comment(2)
This fixed my Click issues as well, on a Django app (don't ask). However, it's error message was actually pretty helpful, as it said, along with the rest of the error message quoted above: This system supports the C.UTF-8 locale which is recommended.. Ubuntu 18.04, Python 3.6. So Click itself may provide you hints on what to use.Laplace
Are these env vars usually set by /etc/bashrc or something? i'm in a container. so maybe that is why i get these errors and have to set these env vars in my container entrypoint.sh.Wynd

© 2022 - 2024 — McMap. All rights reserved.