我知道这里有一个类似的问题:
django-admin startproject results in "ModuleNotFoundError: No module named 'config'"
但是答案对我不起作用。
我有一个运行Ubuntu(18.04)+ Apache(2.4.29)的VPS。
应用程序通过以下命令行运行良好:
python3 manage.py runserver 0.0.0.0:8000 --settings=config.settings.production
我的Apache conf看起来像这样:
WSGIDaemonProcess myapp python-home=/home/my_repository/ENV
WSGIProcessGroup myapp
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /home/my_repository/config/wsgi.py
<Directory /home/my_repository/>
Require all granted
</Directory>
我在Apache.log上看到此错误:
mod_wsgi (pid=32745): Target WSGI script '/home/my_repository/config/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=32745): Exception occurred processing WSGI script '/home/my_repository/config/wsgi.py'.
Traceback (most recent call last):
File "/home/my_repository/config/wsgi.py", line 39, in <module>
application = get_wsgi_application()
File "/usr/local/lib/python3.6/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
django.setup(set_prefix=False)
File "/usr/local/lib/python3.6/dist-packages/django/__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/usr/local/lib/python3.6/dist-packages/django/conf/__init__.py", line 76, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.6/dist-packages/django/conf/__init__.py", line 63, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python3.6/dist-packages/django/conf/__init__.py", line 142, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'config'
我还确认了我的wsgi.py
配置了哪些版本和变量:
DJANGO_SETTINGS_MODULE: config.settings.production
Python version: 3.6.9
Django version: 3.0.3
我希望有人能帮助我。我现在要尝试训练营将近一个月?
答案 0 :(得分:0)
问题出在python工作目录上。
它指向/
。
要设置工作目录,您需要在home
处指定WSGIDaemonProcess
参数。
我的apache conf最终像这样:
WSGIDaemonProcess myapp python-home=/home/my_repository/ENV home=/home/my_repository
WSGIProcessGroup myapp
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /home/my_repository/config/wsgi.py
<Directory /home/my_repository/>
Require all granted
</Directory>