mod_wsgi: ImportError: No module named 'encodings'

限于喜欢 提交于 2019-11-27 22:54:22

So with some help with my friends (IE: SysAdmins), we got this figured out last night. I learn best by example, so let's assume you're running Apache with mod_wsgi as Linux group apache with user flipperpa. Let's assume you're hosting in /home/my_project with wsgi.py in my_project/wsgi.py.

At the top level (ls -l /):

drwxr-xr-x.  47 root root  4096 Jul  9 09:43 home

In the home directory (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. The final "x", of course, means anyone can execute.

Check your permissions down the tree; this did the trick for us.

The issue was caused by SELinux. What I did was this:

  • moved the project files into /home/admin/myproject
  • changed permissions as in FlipperPA's answer
  • and, crucially I think, set the httpd_enable_homedirs SELinux boolean to on using setsebool -P httpd_enable_homedirs on.

The SELinux guide on the CentOS wiki was very helpful. For example, to debug you can run setenforce Permissive so SELinux won't enforce its rules, but still write to the log. Just remember to setenforce Enforcing again afterwards!

On Windows, I solved this problem by putting the Lib directory of my Python installation directly on the 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:

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. I downloaded mine from the Unofficial Binaries page and chose it to match the Python (64 bit 3.4 in the example above). My website works correctly with Python after the above change to the .bat file starting Apache.

Note my version of Python, WinPython, is installed by unzipping the install and placing it in the C: drive. Thus is it not installed in the Registry in the usual Windows way, which should minimize Registry interaction considerations.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!