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

Dustin Sun picture Dustin Sun · Apr 15, 2016 · Viewed 29.4k times · Source

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
>>>

Answer

GHETTO.CHiLD picture GHETTO.CHiLD · Jun 16, 2016

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: