Django ModuleNotFoundError:没有名为'myProject'的模块

时间:2018-05-24 14:22:01

标签: python django wsgi

ModuleNotFoundError:没有名为'myProject'的模块错误,但我不知道原因。如果你告诉我,我很高兴。

·CentOS 7.2
·Python 3.6
·Django 2.0
·apache 2.4

我按如下方式设置虚拟环境

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

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

import os,sys

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

1 个答案:

答案 0 :(得分:1)

读:

WSGIPythonHome指令或python-home选项应指向Python虚拟环境的根目录。这应该与sys.prefix在为该虚拟环境运行Python时具有的值相同。

然后将WSGIPythonPath指令或python-path选项设置到存在模块/包的项目代码目录中,在您的情况下可能/home/username/myProject

请勿使用这些后面的选项指向site-packages目录,使用第一个选项指定虚拟环境的位置。

我建议你使用守护进程模式添加:

WSGIRestrictEmbedded On

LoadModule行之后。然后移除WSGIPythonHomeWSGIPythonPath,并依赖python-home上的python-pathWSGIDaemonProcess选项。

BTW,DJANGO_SETTINGS_MODULE不会xxx.settings。您可能意味着myProject.settings

此外,您的虚拟环境似乎是您的项目代码目录。如果是这种情况,不是真的推荐。将虚拟环境创建为独立的子目录。不要将它与您的代码混合在一起,因此删除虚拟环境并重新创建它变得非常困难。