Ubuntu, Apache2, Django) 致命的 Python 错误:Py_Initialize: 无法获取区域设置编码 ImportError: 找不到名为 'encodings' 的模块。

9
I'm trying to set up my django(1.8) application with AWS EC2 having Ubuntu 14.04, Apache2, python 3.4.
When I run 'sudo service apache2 start', the page keeps re-loading and the same error message is stacking at '/var/log/apache2/error.log'.
The error message is:
[Fri Aug 26 2016] [mpm_event:notice] [pid n:tid m] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/4.5.5 Python/3.4.3 configured -- resuming normal operations [Fri Aug 26 2016] [core:notice] [pid n:tid m] AH00094: Command line: '/usr/sbin/apache2' Fatal Python error: Py_Initialize: Unable to get the locale encoding ImportError: No module named 'encodings'
My configuration is below:
I added one line: 'Include /etc/apache2/httpd.conf' at the bottom of '/etc/apache2/apache2.conf'.
'/etc/apache2/httpd.conf':
WSGIScriptAlias / /home/ubuntu/project/project/project/wsgi.py
WSGIDaemonProcess project python-path=/home/ubuntu/project/project
WSGIProcessGroup project
WSGIPythonHome /usr/bin/python3.4

<Directory /home/ubuntu/project/project/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

Alias /static/ /home/ubuntu/project/project/deploy_to_server/
<Directory /home/ubuntu/project/project/deploy_to_server>
Require all granted
</Directory>

我认为我没有做错任何事情。

但是它一直记录着相同的错误。是否有什么我漏掉的东西?

我已经更改了mod_wsgi/3.x Python/2.7配置-->mod_wsgi/4.5.5 Python/3.4.3配置以同步Python版本。

2个回答

5
因为在 /etc/apache2/httpd.conf 中有一行 'WSGIPythonHome /usr/bin/pytyon3.4',所以出现了这个问题。
如果没有这行代码,就可以正常运行,谢谢。

1
作为额外信息,如果设置了“WSGIPythonHome”,应该引用Python安装的“sys.prefix”所给出的目录。注意,“WSGIPythonHome”不能用于指代与mod_wsgi编译版本不同的Python安装或虚拟环境。因此,如果mod_wsgi编译为Python 2.7,则不能使用它来尝试强制使用Python 3.5。 - Graham Dumpleton
关键是,只有在使用虚拟环境时才需要这行代码。如果您已经安装了Python,并且像Django这样的pip依赖项是系统范围内安装的 - 只需省略WSGIPythonHome指令即可。 - drchuck

4

对我来说,问题出在试图指向虚拟环境下的Python可执行文件。

错误示例:

WSGIPythonHome /path/to/virtualenv/bin/python3.4

Correct:

WSGIPythonHome /path/to/virtualenv/

这在WSGI虚拟环境的文档中有所描述。点击此处查看。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接