[英]mod_wsgi: ImportError: No module named 'encodings'
I'm using Apache 2.2.15 on CentOS 6.5. 我在CentOS 6.5上使用Apache 2.2.15。 I'm trying to set up a Django app using mod_wsgi. 我正在尝试使用mod_wsgi设置Django应用程序。 I'm using a virtual environment, and mod_wsgi was configured with --with-python=/path/to/virtualenv/bin/python3.4
. 我正在使用虚拟环境,并且mod_wsgi配置了--with-python=/path/to/virtualenv/bin/python3.4
。
I've added this to my httpd.conf
: 我把它添加到我的httpd.conf
:
WSGIPythonPath /srv/myproject:/path/to/virtualenv/lib/python3.4/site-packages
WSGIPythonHome /path/to/virtualenv
<VirtualHost *:80>
WSGIScriptAlias / /srv/myproject/myproject/wsgi.py
...
</VirtualHost>
In wsgi.py
, I added 在wsgi.py
,我补充道
sys.path.insert(1, "/path/to/virtualenv/lib/python3.4/site-packages")
The problem is that when I try to open the app in my browser, it loads indefinitely. 问题是,当我尝试在浏览器中打开应用程序时,它会无限期地加载。 Here's the Apache error log: 这是Apache错误日志:
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
[Mon Jun 30 17:37:28 2014] [notice] child pid 19370 exit signal Aborted (6)
[Mon Jun 30 17:37:28 2014] [notice] child pid 19371 exit signal Aborted (6)
...
[Mon Jun 30 17:37:28 2014] [notice] child pid 19377 exit signal Aborted (6)
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
What's interesting is that in both the system installation of Python (2.6) and the virtual environment version (3.4), import encodings
works fine! 有趣的是,在Python(2.6)的系统安装和虚拟环境版本(3.4)中, import encodings
工作正常! I've tried using the example WSGI script from the mod_wsgi CheckingYourInstallation page to confirm which version of Python is being used by Apache, but I get the same ImportError. 我已经尝试使用mod_wsgi CheckingYourInstallation页面中的示例WSGI脚本来确认Apache正在使用哪个版本的Python,但是我得到了相同的ImportError。
Does anyone have a suggestion for next steps? 有没有人对后续步骤有什么建议? I've scoured the docs but I don't know where to go from here. 我已经搜过了文档,但我不知道从哪里开始。
So with some help with my friends (IE: SysAdmins), we got this figured out last night. 所以在我朋友的帮助下(IE:SysAdmins),我们昨晚得到了这个。 I learn best by example, so let's assume you're running Apache with mod_wsgi as Linux group apache with user flipperpa. 我通过示例最好地学习,所以让我们假设您使用mod_wsgi运行Apache作为Linux组apache并使用用户flipperpa。 Let's assume you're hosting in /home/my_project with wsgi.py in my_project/wsgi.py. 假设您在my_project / wsgi.py中使用wsgi.py托管/ home / my_project。
At the top level (ls -l /): 在顶层(ls -l /):
drwxr-xr-x. 47 root root 4096 Jul 9 09:43 home
In the home directory (ls -l /home): 在主目录(ls -l / home)中:
drwxrwsr-x 7 flipper apache 4096 Jul 29 10:22 my_project
This was the key. 这是关键。 The lower case "s" means the apache group's setgid bit is set, and the execute bit is set. 小写“s”表示设置了apache组的setgid位,并设置了执行位。 The final "x", of course, means anyone can execute. 当然,最后的“x”意味着任何人都可以执行。
Check your permissions down the tree; 检查树下的权限; this did the trick for us. 这对我们起了作用。
The issue was caused by SELinux. 这个问题是由SELinux引起的。 What I did was this: 我做的是这样的:
httpd_enable_homedirs
SELinux boolean to on
using setsebool -P httpd_enable_homedirs on
. 而关键的是,我认为,设置httpd_enable_homedirs
中的SELinux到on
使用setsebool -P httpd_enable_homedirs on
。 The SELinux guide on the CentOS wiki was very helpful. CentOS wiki上的SELinux指南非常有用。 For example, to debug you can run setenforce Permissive
so SELinux won't enforce its rules, but still write to the log. 例如,要调试,您可以运行setenforce Permissive
因此SELinux不会强制执行其规则,但仍会写入日志。 Just remember to setenforce Enforcing
again afterwards! 请记住之后再次setenforce Enforcing
!
On Windows, I solved this problem by putting the Lib directory of my Python installation directly on the PYTHONPATH. 在Windows上,我通过将Python安装的Lib目录直接放在PYTHONPATH上解决了这个问题。 This is necessary even though, if you run Python directly in the same shell, it doesn't require this to find the encodings package: 这是必要的,即使你在同一个shell中直接运行Python,它也不需要这个来找到编码包:
set PYTHONPATH=C:\WinPython-64bit-3.4.4.6Qt5\python-3.4.4.amd64\Lib;%PYTHONPATH%
c:\apache24\bin\httpd.exe
Also be sure to use the correct mod_wsgi version. 另外一定要使用正确的mod_wsgi版本。 I downloaded mine from the Unofficial Binaries page and chose it to match the Python (64 bit 3.4 in the example above). 我从非官方二进制文件页面下载了我的,并选择它来匹配Python(上例中的64位3.4)。 My website works correctly with Python after the above change to the .bat file starting Apache. 在上面更改为启动Apache的.bat文件之后,我的网站可以正常使用Python。
Note my version of Python, WinPython , is installed by unzipping the install and placing it in the C: drive. 注意我的Python版本WinPython是通过解压缩安装并将其放在C:驱动器中来安装的。 Thus is it not installed in the Registry in the usual Windows way, which should minimize Registry interaction considerations. 因此,它没有以通常的Windows方式安装在注册表中,这应该最小化注册表交互注意事项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.