且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

导入错误:没有名为 django.core.wsgi 的模块 Apache + VirtualEnv + AWS + WSGI

更新时间:2022-05-21 21:25:37

我知道这是一个旧线程,但我刚刚遇到了同样的问题,我不认为这是由于缺少包引起的.由于 Django 核心发行版已经包含正确的 wsgi 处理程序.

I know that this is an old thread but I've just bumped into the same issue and I don't think that this is caused by a missing package. As the Django core distribution contains the correct wsgi handler already.

这里的问题是,当 wsgi.py 被执行时,它缺少来自您的 virtualenv 的站点包的包.(如果你已经激活了你的 virtualenv,并完成了 pip install django 那么一切都很好.你有必要的 django 包).

The problem here is that when wsgi.py is executed it's missing the packages of the site-packages from your virtualenv. (If you have activated your virtualenv, and done pip install django then everything is fine. You have the necessary django packages).

就我而言,我修复了修改 Path/to/Project/Project/wsgi.py 文件中的 sys.path 的问题.

As far as I'm concerned, I fixed the issue modifying the sys.path in my Path/to/Project/Project/wsgi.py file.

您必须将您的项目目录和您的 virtualenv 站点包附加到 sys.path 列表中.这是我的项目中包含的 wsgi.py 文件(谈论使用 django-admin.py start-project 创建的 wsgi.py)...我必须对其进行修改才能使其与 Apache 一起使用

You have to append your project dir and your virtualenv site-packages to the sys.path List. Here is my wsgi.py file contained in my project (Talking about the wsgi.py created with django-admin.py start-project)... that I had to modify in order to make it work with Apache

# =====================
# wsgi.py file begin 

import os, sys
# add the hellodjango project path into the sys.path
sys.path.append('<PATH_TO_MY_DJANGO_PROJECT>/hellodjango')

# add the virtualenv site-packages path to the sys.path
sys.path.append('<PATH_TO_VIRTUALENV>/Lib/site-packages')

# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

# wsgi.py file end
# ===================

确保:

  1. 您将 mod_wsgi 添加到 Apache 模块目录mod_wsgi 必须针对您拥有的操作系​​统、Apache 和 Python 版本进行编译

  1. you added mod_wsgi to the Apache modules dir mod_wsgi must be compiled for the OS, Apache and Python version you have

在你的 httpd.conf 中添加了 load module 命令来加载 mod_wsgi 模块LoadModule wsgi_module modules/mod_wsgi.so

added the load module command into your httpd.conf to load mod_wsgi module LoadModule wsgi_module modules/mod_wsgi.so

在您的 httpd.conf 或您在 httpd.conf 中包含的任何配置中配置的 Django 细节

configured Django specifics in your httpd.conf or any conf you include in your httpd.conf

基于文档如何在 Apache 和 mod_wsgi 中使用 Django>

WSGIScriptAlias / <PATH_TO_PROJECT>/hellodjango/hellodjango/wsgi.py
WSGIPythonPath <PATH_TO_PROJECT>:<PATH_TO_VIRTUALENV>/Lib/site-packages

<Directory <PATH_TO_PROJECT>/hellodjango/hellodjango> 
  <Files wsgi.py>
    Order deny,allow
    Require all granted
  </Files>
</Directory>

希望这会有所帮助.它对我有用.

Hope this helps. It worked for me.