ModuleNotFoundError: 找不到模块名为 ‘projectName’ – 在 Django 部署中的 wsgi

huangapple go评论71阅读模式
英文:

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 , referer: http://localhost/
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 "", line 1050, in _gcd_import, referer: http://localhost/
File "", line 1027, in _find_and_load, referer: http://localhost/
File "", line 992, in _find_and_load_unlocked, referer: http://localhost/
File "", line 241, in _call_with_frames_removed, referer: http://localhost/
File "", line 1050, in _gcd_import, referer: http://localhost/
File "", line 1027, in _find_and_load, referer: http://localhost/
File "", line 1004, in _find_and_load_unlocked, referer: http://localhost/
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 &#39;localassets&#39;, i.e. the projectname.

mod_wsgi (pid=5128): Failed to exec Python script file &#39;C:/Env/localassets/localassets/wsgi.py&#39;., referer: http://localhost/
mod_wsgi (pid=5128): Exception occurred processing WSGI script &#39;C:/Env/localassets/localassets/wsgi.py&#39;., referer: http://localhost/
Traceback (most recent call last):\r, referer: http://localhost/
   File &quot;C:/Env/localassets/localassets/wsgi.py&quot;, line 16, in &lt;module&gt;\r, referer: http://localhost/
     application = get_wsgi_application()\r, referer: http://localhost/
   File &quot;C:\\Env\\Lib\\site-packages\\django\\core\\wsgi.py&quot;, line 12, in get_wsgi_application\r, referer: http://localhost/
     django.setup(set_prefix=False)\r, referer: http://localhost/
   File &quot;C:\\Env\\Lib\\site-packages\\django\\__init__.py&quot;, line 19, in setup\r, referer: http://localhost/
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)\r, referer: http://localhost/
   File &quot;C:\\Env\\Lib\\site-packages\\django\\conf\\__init__.py&quot;, line 102, in __getattr__\r, referer: http://localhost/
    self._setup(name)\r, referer: http://localhost/
   File &quot;C:\\Env\\Lib\\site-packages\\django\\conf\\__init__.py&quot;, line 89, in _setup\r, referer: http://localhost/
     self._wrapped = Settings(settings_module)\r, referer: http://localhost/
   File &quot;C:\\Env\\Lib\\site-packages\\django\\conf\\__init__.py&quot;, line 217, in __init__\r, referer: http://localhost/
     mod = importlib.import_module(self.SETTINGS_MODULE)\r, referer: http://localhost/
   File &quot;C:\\Program Files\\Python310\\Lib\\importlib\\__init__.py&quot;, line 126, in import_module\r, referer: http://localhost/
     return _bootstrap._gcd_import(name[level:], package, level)\r, referer: http://localhost/
   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1050, in _gcd_import\r, referer: http://localhost/
   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1027, in _find_and_load\r, referer: http://localhost/
   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 992, in _find_and_load_unlocked\r, referer: http://localhost/
   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 241, in _call_with_frames_removed\r, referer: http://localhost/
   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1050, in _gcd_import\r, referer: http://localhost/
   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1027, in _find_and_load\r, referer: http://localhost/
   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1004, in _find_and_load_unlocked\r, referer: http://localhost/
 ModuleNotFoundError: No module named &#39;localassets&#39;\r, referer: http://localhost/

httpd.conf

LoadFile &quot;C:/Program Files/Python310/python310.dll&quot;
LoadModule wsgi_module &quot;C:/Env/Lib/site-packages/mod_wsgi/server/mod_wsgi.cp310-win_amd64.pyd&quot;
WSGIPythonHome &quot;C:/Program Files/Python310&quot;
WSGIScriptAlias / &quot;C:/Env/localassets/localassets/wsgi.py&quot;
WSGIPythonPath &quot;C:/Env/Lib/site-packages&quot;

&lt;Directory &quot;C:/Env/localassets/localassets/&quot;&gt;
    &lt;Files wsgi.py&gt;
        Require all granted
    &lt;/Files&gt;
&lt;/Directory&gt;

Alias /static &quot;C:/Env/localassets/localassets/static&quot;
&lt;Directory &quot;C:/Env/localassets/localassets/static&quot;&gt;
    Require all granted
&lt;/Directory&gt;

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(&#39;DJANGO_SETTINGS_MODULE&#39;, &#39;localassets.settings&#39;)

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 projectnamepip3 install projectname

在安装了projectname Python库之后,ModuleNotFoundError: No module named 'projectname' 错误将被解决。

如果错误仍然存在,请检查你的imports - 你可能需要从django_backenduser_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 &#39;projectname&#39; 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, &#39;C:/Env/localassets&#39;)

os.environ.setdefault(&#39;DJANGO_SETTINGS_MODULE&#39;, &#39;localassets.settings&#39;)

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.

huangapple
  • 本文由 发表于 2023年6月6日 06:52:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定