英文:
ModuleNotFoundError: No module named 'projectName' - wsgi Django Deployment
问题
I am trying to run my Django Project using Apache.
当我启动Apache时,我收到一个'Internal Server Error',检查日志,我发现它无法执行我的项目的wsgi.py文件,并抛出以下错误。
错误信息显示没有找到'localassets'模块,即项目名称。
mod_wsgi (pid=5128): Failed to exec Python script file 'C:/Env/localassets/localassets/wsgi.py'., referer: http://localhost/
mod_wsgi (pid=5128): Exception occurred processing WSGI script 'C:/Env/localassets/localassets/wsgi.py'., referer: http://localhost/
Traceback (most recent call last):, referer: http://localhost/
File "C:/Env/localassets/localassets/wsgi.py", line 16, in
application = get_wsgi_application(), referer: http://localhost/
File "C:\Env\Lib\site-packages\django\core\wsgi.py", line 12, in get_wsgi_application, referer: http://localhost/
django.setup(set_prefix=False), referer: http://localhost/
File "C:\Env\Lib\site-packages\django_init_.py", line 19, in setup, referer: http://localhost/
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING), referer: http://localhost/
File "C:\Env\Lib\site-packages\django\conf_init_.py", line 102, in getattr, referer: http://localhost/
self.setup(name), referer: http://localhost/
File "C:\Env\Lib\site-packages\django\conf_init.py", line 89, in setup, referer: http://localhost/
self.wrapped = Settings(settings_module), referer: http://localhost/
File "C:\Env\Lib\site-packages\django\conf_init.py", line 217, in init, referer: http://localhost/
mod = importlib.import_module(self.SETTINGS_MODULE), referer: http://localhost/
File "C:\Program Files\Python310\Lib\importlib_init.py", line 126, in import_module, referer: http://localhost/
return _bootstrap._gcd_import(name[level:], package, level), referer: http://localhost/
File "
File "
File "
File "
File "
File "
File "
ModuleNotFoundError: No module named 'localassets', referer: http://localhost/
httpd.conf
LoadFile "C:/Program Files/Python310/python310.dll"
LoadModule wsgi_module "C:/Env/Lib/site-packages/mod_wsgi/server/mod_wsgi.cp310-win_amd64.pyd"
WSGIPythonHome "C:/Program Files/Python310"
WSGIScriptAlias / "C:/Env/localassets/localassets/wsgi.py"
WSGIPythonPath "C:/Env/Lib/site-packages"
<Directory "C:/Env/localassets/localassets/">
Require all granted
Alias /static "C:/Env/localassets/localassets/static"
<Directory "C:/Env/localassets/localassets/static">
Require all granted
检查wsgi.py文件,我在这里也没有看到任何问题。
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'localassets.settings')
application = get_wsgi_application()
看起来我在wsgi.py中漏掉了一些东西,但我不确定缺少什么才能使它正常工作?
英文:
I am trying to run my Django Project using Apache.
When I launch Apache I receive an 'Internal Server Error', inspecting the logs I have found that it fails to execute the wsgi.py for my project and throws the errors below.
The error presented says there is No module named 'localassets', i.e. the projectname.
mod_wsgi (pid=5128): Failed to exec Python script file 'C:/Env/localassets/localassets/wsgi.py'., referer: http://localhost/
mod_wsgi (pid=5128): Exception occurred processing WSGI script 'C:/Env/localassets/localassets/wsgi.py'., referer: http://localhost/
Traceback (most recent call last):\r, referer: http://localhost/
File "C:/Env/localassets/localassets/wsgi.py", line 16, in <module>\r, referer: http://localhost/
application = get_wsgi_application()\r, referer: http://localhost/
File "C:\\Env\\Lib\\site-packages\\django\\core\\wsgi.py", line 12, in get_wsgi_application\r, referer: http://localhost/
django.setup(set_prefix=False)\r, referer: http://localhost/
File "C:\\Env\\Lib\\site-packages\\django\\__init__.py", line 19, in setup\r, referer: http://localhost/
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)\r, referer: http://localhost/
File "C:\\Env\\Lib\\site-packages\\django\\conf\\__init__.py", line 102, in __getattr__\r, referer: http://localhost/
self._setup(name)\r, referer: http://localhost/
File "C:\\Env\\Lib\\site-packages\\django\\conf\\__init__.py", line 89, in _setup\r, referer: http://localhost/
self._wrapped = Settings(settings_module)\r, referer: http://localhost/
File "C:\\Env\\Lib\\site-packages\\django\\conf\\__init__.py", line 217, in __init__\r, referer: http://localhost/
mod = importlib.import_module(self.SETTINGS_MODULE)\r, referer: http://localhost/
File "C:\\Program Files\\Python310\\Lib\\importlib\\__init__.py", line 126, in import_module\r, referer: http://localhost/
return _bootstrap._gcd_import(name[level:], package, level)\r, referer: http://localhost/
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import\r, referer: http://localhost/
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load\r, referer: http://localhost/
File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked\r, referer: http://localhost/
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed\r, referer: http://localhost/
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import\r, referer: http://localhost/
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load\r, referer: http://localhost/
File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked\r, referer: http://localhost/
ModuleNotFoundError: No module named 'localassets'\r, referer: http://localhost/
httpd.conf
LoadFile "C:/Program Files/Python310/python310.dll"
LoadModule wsgi_module "C:/Env/Lib/site-packages/mod_wsgi/server/mod_wsgi.cp310-win_amd64.pyd"
WSGIPythonHome "C:/Program Files/Python310"
WSGIScriptAlias / "C:/Env/localassets/localassets/wsgi.py"
WSGIPythonPath "C:/Env/Lib/site-packages"
<Directory "C:/Env/localassets/localassets/">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static "C:/Env/localassets/localassets/static"
<Directory "C:/Env/localassets/localassets/static">
Require all granted
</Directory>
Inspecting the wsgi.py file I don't see any issues here either.
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'localassets.settings')
application = get_wsgi_application()
It seems as though I am missing something in wsgi.py but I am unsure as to what I am missing for this to work correctly?
答案1
得分: 0
在你的Python环境中,你需要安装这个库。你可以使用以下命令安装projectname:
pip install projectname 或 pip3 install projectname
在安装了projectname Python库之后,ModuleNotFoundError: No module named 'projectname' 错误将被解决。
如果错误仍然存在,请检查你的imports - 你可能需要从django_backend或user_profile中导入。
英文:
In your python environment you have to install the library. You can install projectname with following command:
pip install projectname or pip3 install projectname<br>
After the installation of projectname python library, ModuleNotFoundError: No module named 'projectname' error will be solved.<br>
If the error is still there, check your imports - you might have to import from either django_backend or user_profile
答案2
得分: 0
只需将应用程序文件夹添加到 wsgi.py 中的 sys.path 中:
import os
import sys
sys.path.insert(0, 'C:/Env/localassets')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'localassets.settings')
application = get_wsgi_application()
另外,你似乎将虚拟环境文件夹与你的应用程序混合在一起。我建议分开,如下所示:
project_folder
env
localassets
localassets
wsgi.py
英文:
just add the apps folder to sys.path in wsgi.py:
import os
import sys
sys.path.insert(0, 'C:/Env/localassets')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'localassets.settings')
application = get_wsgi_application()
btw. you seem to have the virtual env folder mixed with your app. I would recommend to separate like
project_folder
env
localassets
localassets
wsgi.py
etc.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论