54

我将 Quokka Python/Flask CMS 下载到了 CentOS7 服务器。使用命令一切正常

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

然后我创建一个文件 /etc/init.d/quokkacms。该文件包含以下代码

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

但是运行时出现错误sudo service quokkacms start

RuntimeError: Click 将中止进一步的执行,因为 Python 3 被配置为使用 ASCII 作为环境的编码。切换到 Python 2 或咨询http://click.pocoo.org/python3/以了解
缓解步骤。

在我看来,它是 bash 脚本。我怎么会得到不同的结果?我也按照错误消息中链接中的说明进行操作,但仍然没有运气。

[更新] 在发布此问题之前,我已经尝试过 Click 提供的解决方案。检查下面的结果(我在 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
>>>
4

2 回答 2

97

如果您尝试执行测试用例,则必须每次设置以下环境变量:

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

每次都这样做可以解决错误。

也可以在 IDE 运行配置中将其设置为

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

例如,请参阅 PyCharm 2016 中的以下设置:

于 2016-06-16T20:10:20.683 回答
9

在现有解决方案中添加更多内容:

如果你在 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.

您正在处理 Python 3 认为您仅限于 ASCII 数据的环境。这些问题的解决方案因您的计算机运行的语言环境而异。

例如,如果您有一台德国 Linux 机器,您可以通过将语言环境导出到 de_DE.utf-8 来解决问题:

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

如果您在美国机器上,en_US.utf-8 是首选编码。在一些较新的 Linux 系统上,您还可以尝试 C.UTF-8 作为语言环境:

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

取自Python 3 代理处理

于 2019-02-27T10:34:10.860 回答