简体   繁体   English

Django ModuleNotFoundError:没有名为“ myProject”的模块

[英]Django ModuleNotFoundError: No module named 'myProject'

ModuleNotFoundError: No module named 'myProject' error, but I do not know the cause. ModuleNotFoundError:没有名为“ myProject”的模块错误,但我不知道原因。 I am glad if you tell me. 如果您告诉我,我感到很高兴。

・ CentOS 7.2 ・ CentOS 7.2
・ Python 3.6 ・ Python 3.6
・ Django 2.0 ・ Django 2.0
・ apache 2.4 ・ Apache 2.4

I set the virtual environment as follows 我将虚拟环境设置如下

pip install virtualenv
mkdir xxx
cd xxx 
python3 -m venv xxx
source xxx/bin/activate
pip install mod_wsgi
mod_wsgi-express module-config
LoadModule wsgi_module "/home/username/myProject/myProject/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
WSGIPythonHome "/home/username/myProject/myProject"

■ /etc/httpd/conf.d/django.conf ■/etc/httpd/conf.d/django.conf

NameVirtualHost *:80
LoadModule wsgi_module /home/username/myProject/myProject/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
WSGIPythonHome /home/username/myProject/myProject
WSGISocketPrefix /var/run/wsgi
<VirtualHost *:80>

  ServerName xxx.com
  DocumentRoot /home/username

  WSGIApplicationGroup %{GLOBAL}
  WSGIDaemonProcess xxx python-home=/home/username/myProject/myProject python-path=/home/username/myProject/myProject/lib/python3.6/site-packages
  WSGIProcessGroup xxx
  WSGIScriptAlias / /home/username/myProject/myProject/wsgi.py

  <Directory /home/username/myProject/myProject/static>
      Require all granted
  </Directory>

  <Directory /home/username/myProject/myProject>
      <Files wsgi.py>
          Require all granted
      </Files>
  </Directory>
</VirtualHost>

■ wsgi.py ■wsgi.py

import os,sys

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxx.settings")

application = get_wsgi_application()

Read: 读:

The WSGIPythonHome directive or python-home option should point at the root directory for the Python virtual environment. WSGIPythonHome指令或python-home选项应指向Python虚拟环境的根目录。 This should be the same value as sys.prefix has when Python is run for that virtual environment. 该值应与针对该虚拟环境运行Python时的sys.prefix值相同。

The WSGIPythonPath directive or python-path option would then be set to your project code directory in which modules/packages exist, likely /home/username/myProject in your case. 然后,将WSGIPythonPath指令或python-path选项设置为项目代码目录,其中存在模块/包,在您的情况下可能是/home/username/myProject

Do not use these later options to point at the site-packages directory, use the first options for specifying the location of the virtual environment. 不要使用这些后面的选项来指向site-packages目录,而要使用第一个选项来指定虚拟环境的位置。

I would suggest since you are using daemon mode to add: 我建议您使用守护程序模式来添加:

WSGIRestrictEmbedded On

after the LoadModule line. LoadModule行之后。 Then remove the WSGIPythonHome and WSGIPythonPath and rely on python-home and python-path options on WSGIDaemonProcess instead. 然后取出WSGIPythonHomeWSGIPythonPath并依靠python-homepython-path上的选项WSGIDaemonProcess代替。

BTW, DJANGO_SETTINGS_MODULE wouldn't be xxx.settings . 顺便说一句, DJANGO_SETTINGS_MODULE不会是xxx.settings You likely mean myProject.settings . 您可能是指myProject.settings

Also, it appears that you have your virtual environment being your project code directory. 另外,似乎您的虚拟环境是项目代码目录。 If that is the case, is not really recommended. 如果是这样,则不建议您这样做。 Create the virtual environment as a distinct sub directory somewhere of its own. 将虚拟环境创建为其自己某个地方的不同子目录。 Don't intermingle it with your code as then it becomes really hard to remove the virtual environment and re-create it. 不要将其与您的代码混合在一起,因为那样将很难删除虚拟环境并重新创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM