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
>>>
Dustin Sun
  • 5,292
  • 9
  • 49
  • 87
  • 1
    So did you consult the link given you by the helpful error message? The answer is there. Explicitly. – John Bollinger Apr 15 '16 at 16:05
  • Hint: the problem is not the initscript itself, but the environment in which the script runs. – John Bollinger Apr 15 '16 at 16:05
  • 1
    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](https://www.python.org/dev/peps/pep-0538/) and the related [PEP 540](https://www.python.org/dev/peps/pep-0540/). 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. – Trevor Boyd Smith Oct 18 '21 at 14:40
  • Does this answer your question? [Encoding issue with python3 and click package](https://stackoverflow.com/questions/32234393/encoding-issue-with-python3-and-click-package) – Trevor Boyd Smith Oct 18 '21 at 15:25

2 Answers2

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:

Dimitar Ivanov
  • 116
  • 1
  • 1
  • 10
GHETTO.CHiLD
  • 3,267
  • 1
  • 22
  • 34
  • In case that didn't work for you, setting the values as "en_US.UTF-8" worked for me (I upcased the UTF-8). – Valentin V May 17 '18 at 14:42
  • also for newbies like me in Linux world, adding these env variable into user default will help you after you reboot the machine... ~/.bashrc – Emrah Mehmedov Jan 03 '23 at 09:14
  • definitely! adding these to your `.bashrc` or `.zshrc` file will always initialize them in your environment. – GHETTO.CHiLD Jan 05 '23 at 19:59
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

yardstick17
  • 4,322
  • 1
  • 26
  • 33
  • 1
    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. – JL Peyret Mar 19 '20 at 07:16
  • 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. – Trevor Boyd Smith Oct 15 '21 at 20:52