我尝试在Ubuntu服务器上托管我的Django应用程序,当我尝试访问我的网站时,我从Apache日志中得到这个错误:
ModuleNotFoundError: No module named 'django'
我正在为我的Django应用程序使用一个venv,python版本为3.8(我也在我的venv中编译并安装了mod_wsgi)。运行pip freeze,我发现我的venv中确实安装了Django:
APScheduler==3.8.1
asgiref==3.5.0
backports.zoneinfo==0.2.1
certifi==2021.10.8
charset-normalizer==2.0.10
colorama==0.4.4
commonmark==0.9.1
deepdiff==5.7.0
Django==4.0.1
django-cors-headers==3.11.0
djangorestframework==3.13.1
idna==3.3
lxml==4.7.1
mod-wsgi==4.9.1.dev1
ordered-set==4.0.2
prettytable==3.0.0
psycopg2-binary==2.9.3
Pygments==2.11.2
pytz==2021.3
pytz-deprecation-shim==0.1.0.post0
requests==2.27.1
rich==11.1.0
six==1.16.0
soupsieve==2.3.1
sqlparse==0.4.2
tzdata==2021.5
tzlocal==4.1
urllib3==1.26.8
wcwidth==0.2.5
whitenoise==5.3.0
为了以防万一,我在全局安装了Django,但在Apache中仍然出现了这个错误。我一直在尝试一些常见的解决方案,但似乎无法让它工作。是否有什么我遗漏的或任何设置可能关闭?
我注意到我的Apache说它是用3.6配置的,这可能是原因吗?有没有办法让它使用3.8,这是我的python3默认值?
我的Django项目的wsgi(backend/core/wsgi.py):
import os, sys
sys.path.append('/home/brickmane/djangoapp/pricewatcher/backend/')
sys.path.append('/home/brickmane/djangoapp/pricewatcher/backend/core/')
sys.path.append('/home/brickmane/djangoapp/pricewatcher/venv/lib/python3.8/site-packages')
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
application = get_wsgi_application()
/etc/apache 2/apache2.conf中的我的虚拟主机设置:
WSGIPythonHome /home/brickmane/djangoapp/pricewatcher/venv
WSGIPythonPath /home/brickmane/djangoapp/pricewatcher/backend
<VirtualHost *:80>
ServerAlias www.d8pricecheck.tk
WSGIProcessGroup backend
Alias /static/ /home/brickmane/djangoapp/pricewatcher/backend/static/
<Directory /home/brickmane/djangoapp/pricewatcher/backend/static>
Require all granted
</Directory>
WSGIScriptAlias / /home/brickmane/djangoapp/pricewatcher/backend/core/wsgi.py
<Directory /home/brickmane/djangoapp/pricewatcher/backend/core>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess backend python-path=/home/brickmane/djangoapp/pricewatcher/backend/core python-home=/home/brickmane/djangoapp/pricewatcher/venv
WSGIProcessGroup backend
</VirtualHost>
重新启动和访问网站时的完整Apache错误日志:
[Tue Feb 01 16:24:09.488925 2022] [mpm_prefork:notice] [pid 30163] AH00169: caught SIGTERM, shutting down
[Tue Feb 01 16:24:09.618015 2022] [ssl:warn] [pid 30456] AH01916: Init: (brickmaneserver.hsd1.ga.comcast.net:443) You configured HTTP(80) on the standard HTTPS(443) port!
[Tue Feb 01 16:24:09.696360 2022] [ssl:warn] [pid 30459] AH01916: Init: (brickmaneserver.hsd1.ga.comcast.net:443) You configured HTTP(80) on the standard HTTPS(443) port!
[Tue Feb 01 16:24:09.702058 2022] [mpm_prefork:notice] [pid 30459] AH00163: Apache/2.4.29 (Ubuntu) OpenSSL/1.1.1g mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Tue Feb 01 16:24:09.702100 2022] [core:notice] [pid 30459] AH00094: Command line: '/usr/sbin/apache2'
[Tue Feb 01 16:24:11.607243 2022] [wsgi:error] [pid 30490] [remote 27.147.213.242:33593] mod_wsgi (pid=30490): Target WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py' cannot be loaded as Python module.
[Tue Feb 01 16:24:11.607289 2022] [wsgi:error] [pid 30490] [remote 27.147.213.242:33593] mod_wsgi (pid=30490): Exception occurred processing WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py'.
[Tue Feb 01 16:24:11.607468 2022] [wsgi:error] [pid 30490] [remote 27.147.213.242:33593] Traceback (most recent call last):
[Tue Feb 01 16:24:11.607495 2022] [wsgi:error] [pid 30490] [remote 27.147.213.242:33593] File "/home/brickmane/djangoapp/myserver/backend/core/wsgi.py", line 21, in <module>
[Tue Feb 01 16:24:11.607502 2022] [wsgi:error] [pid 30490] [remote 27.147.213.242:33593] from django.core.wsgi import get_wsgi_application
[Tue Feb 01 16:24:11.607520 2022] [wsgi:error] [pid 30490] [remote 27.147.213.242:33593] ModuleNotFoundError: No module named 'django'
[Tue Feb 01 16:24:14.525726 2022] [wsgi:error] [pid 30490] [remote 108.162.238.29:17852] mod_wsgi (pid=30490): Target WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py' cannot be loaded as Python module.
[Tue Feb 01 16:24:14.525891 2022] [wsgi:error] [pid 30490] [remote 108.162.238.29:17852] mod_wsgi (pid=30490): Exception occurred processing WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py'.
[Tue Feb 01 16:24:14.526144 2022] [wsgi:error] [pid 30490] [remote 108.162.238.29:17852] Traceback (most recent call last):
[Tue Feb 01 16:24:14.526242 2022] [wsgi:error] [pid 30490] [remote 108.162.238.29:17852] File "/home/brickmane/djangoapp/myserver/backend/core/wsgi.py", line 21, in <module>
[Tue Feb 01 16:24:14.526266 2022] [wsgi:error] [pid 30490] [remote 108.162.238.29:17852] from django.core.wsgi import get_wsgi_application
[Tue Feb 01 16:24:14.526327 2022] [wsgi:error] [pid 30490] [remote 108.162.238.29:17852] ModuleNotFoundError: No module named 'django'
[Tue Feb 01 16:24:14.810004 2022] [wsgi:error] [pid 30490] [remote 162.158.187.159:17484] mod_wsgi (pid=30490): Target WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py' cannot be loaded as Python module.
[Tue Feb 01 16:24:14.810169 2022] [wsgi:error] [pid 30490] [remote 162.158.187.159:17484] mod_wsgi (pid=30490): Exception occurred processing WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py'.
[Tue Feb 01 16:24:14.810397 2022] [wsgi:error] [pid 30490] [remote 162.158.187.159:17484] Traceback (most recent call last):
[Tue Feb 01 16:24:14.810484 2022] [wsgi:error] [pid 30490] [remote 162.158.187.159:17484] File "/home/brickmane/djangoapp/myserver/backend/core/wsgi.py", line 21, in <module>
[Tue Feb 01 16:24:14.810506 2022] [wsgi:error] [pid 30490] [remote 162.158.187.159:17484] from django.core.wsgi import get_wsgi_application
[Tue Feb 01 16:24:14.810566 2022] [wsgi:error] [pid 30490] [remote 162.158.187.159:17484] ModuleNotFoundError: No module named 'django'
3条答案
按热度按时间mfpqipee1#
你必须在venv中安装Python版本至少3.8,因为3.7和更低版本与Django 4.0+不兼容。你应该完全删除venv,然后用已安装的Python的更新版本(最新的是3.10.2)重新初始化。
如果Ubuntu至少有3.8,那么它应该没问题。如果没有,试着安装/升级它。这里有一些如何做的提示:https://cloudbytes.dev/snippets/upgrade-python-to-latest-version-on-ubuntu-linux
oymdgrw72#
这个问题有点老了,但如果有人仍然面临这个问题。我已经解决了这个问题,通过设置python路径到我的python的site-packages。也适用于你上面的情况
这解决了我的问题
nfs0ujit3#
对我来说,我确定Apache2 mod_wsgi是针对python 3.9编译的,而我的django示例使用的是python 3.10。
确保我的系统Python也设置为3.9修复了这个错误。