Apache ImportError: No module named 'django'

Apache ImportError: No module named 'django'

使用 django(我尝试过很多版本 1.8.7、1.11.X、2.0.1)python3.5 amd wsgi 4.5.24 从源代码编译确保 python路径设置正确/usr/bin/python3.5

我有一个带有几个 wsgi sript 别名的 apache 文件,除了 django 脚本别名外,所有这些别名都有效,它只在解析 wsgi.py 文件时失败,因为它无法导入 django。所以首先是来自 apache 的错误:

[Tue Jan 02 18:46:32.247088 2018] [wsgi:error] [pid 2475:tid 139822294771456] [remote 192.168.254.101:2418] mod_wsgi (pid=2475): Target WSGI script '/var/www/protectionprofiles/protectionprofiles/wsgi.py' cannot be loaded as Python module.
[Tue Jan 02 18:46:32.247260 2018] [wsgi:error] [pid 2475:tid 139822294771456] [remote 192.168.254.101:2418] mod_wsgi (pid=2475): Exception occurred processing WSGI script '/var/www/protectionprofiles/protectionprofiles/wsgi.py'.
[Tue Jan 02 18:46:32.247523 2018] [wsgi:error] [pid 2475:tid 139822294771456] [remote 192.168.254.101:2418] Traceback (most recent call last):
[Tue Jan 02 18:46:32.247637 2018] [wsgi:error] [pid 2475:tid 139822294771456] [remote 192.168.254.101:2418]   File "/var/www/protectionprofiles/protectionprofiles/wsgi.py", line 12, in <module>
[Tue Jan 02 18:46:32.247699 2018] [wsgi:error] [pid 2475:tid 139822294771456] [remote 192.168.254.101:2418]     from django.core.wsgi import get_wsgi_application
[Tue Jan 02 18:46:32.247772 2018] [wsgi:error] [pid 2475:tid 139822294771456] [remote 192.168.254.101:2418] ImportError: No module named 'django'

和我的 Apache 配置:

<VirtualHost *:80>
 WSGIScriptAlias /certs /var/www/scripts/CavsCertSearch/CavsCertSearch/certstrip.wsgi
        WSGIScriptAlias /testcerts /var/www/scripts/CavsCertSearchTest/CavsCertSearch/certstriptest.wsgi
        WSGIScriptAlias /debug /var/www/scripts/debug/debug.wsgi
        WSGIDaemonProcess protectionprofiles python-path=/var/www/protectionprofiles
        WSGIProcessGroup protectionprofiles
        WSGIApplicationGroup %{GLOBAL}  
        WSGIScriptAlias /pp /var/www/protectionprofiles/protectionprofiles/wsgi.py process-group=protectionprofiles       

        <Directory /var/www/protectionprofiles/protectionprofiles>
        <Files wsgi.py>
                Require all granted
        </Files>
        </Directory>

        Alias /static/  /var/www/protectionprofiles/static/
        <Directory /var/www/protectionprofiles/static>
                Require all granted
        </Directory>

</VirtualHost>

这只是保护配置文件失败的最后一个脚本别名。 django 是用 pip3 安装的,这是我指定版本的方式。我不需要使用虚拟环境,我仍然不太明白这一点。但是当我在解释器上使用 python 时,我可以说

import django 

而且效果很好。任何关于如何在 mod_wsgi 下将 django 包获取到 运行 的提示都会有所帮助,感觉我已经遵循了所有教程并且其中 none 似乎有效。提前谢谢你。

想添加一些版本信息。 运行宁的例子: http://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html#python-installation-in-use 产生:

sys.version = '3.5.3 (default, Nov 23 2017, 11:34:05) \n[GCC 6.3.0 20170406]'
sys.prefix = '/usr'
sys.path = ['/var/www/protectionprofiles', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']

因此,正如 Graham 帮助我弄清楚的那样,在 python 中显然有包管理器的本地和全局版本。所以我需要做的就是

pip3 uninstall django
sudo -H pip3 install django==1.11.9