【问题标题】:django apache ImportError: No module nameddjango apache ImportError:没有命名的模块
【发布时间】:2018-03-08 08:26:46
【问题描述】:

所以这让我发疯了,我有 python3、modwsgi、apache 和一个虚拟主机,它们工作得很好,因为我有几个其他 wsgi 脚本在服务器上工作得很好。我还有一个 django 应用程序,在我运行开发服务器时效果很好。

我已检查“ldd mod_wsgi.so”是否与 python3.5 正确链接

每当我尝试访问我的网站时,都会收到错误消息,并且 apache 日志指出: ImportError:没有名为“protectionprofiles”的模块 保护配置文件是我的站点名称以下是我的虚拟主机配置

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ServerName <my ip>
WSGIScriptAlias /certs /var/www/scripts/CavsCertSearch/CavsCertSearch/certstrip.wsgi
        WSGIScriptAlias /testcerts /var/www/scripts/CavsCertSearchTest/CavsCertSearch/certstriptest.wsgi
        WSGIScriptAlias /protectionprofiles /var/www/protectionprofiles/protectionprofiles/wsgi.py
        <Directory /var/www/protectionprofiles/protectionprofiles>
        <Files wsgi.py>
        Require all granted
        </Files>
        </Directory>
</VirtualHost>

我的站点应用程序是保护配置文件别名。我不知道问题是什么我已经尝试了几十个不同的 apache 教程,但它们似乎都不起作用。任何帮助是极大的赞赏。

------ 添加我尝试过的其他东西-------- 所以我在虚拟主机中添加了以下2个命令

WSGIDaemonProcess protectionprofiles python-path=/var/www/protectionprofiles/
WSGIProcessGroup protectionprofiles

不,我得到一个不同的错误

Error was: No module named 'django.db.backends.postgresql'

这是我的后端,但开发服务器工作正常?下面是我的数据库配置

DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'USER': 'myuser',
            'PASSWORD': 'abcd1234',
            'HOST': '127.0.0.1',
            'PORT': '5432',
            'NAME': 'protectionprofile',
        }
    }

----另一个错误---- 偶尔我会得到

RuntimeError: populate() isn't reentrant

----另一个错误!!---现在当我更新时

WSGIDaemonProcess protectionprofiles python-path=/var/www/protectionprofiles/:/usr/lib/python3/dist-packages/django

我明白了

 ImportError: cannot import name 'SimpleCookie'

---另一个奇怪的事情是当我有

WSGIProcessGroup protectionprofiles

启用我没有得到它找不到模块“protectionprofiles”的错误,但是当我的其他 wsgi 脚本不起作用时!他们只在那里没有的时候工作。对此的任何解释都会非常有帮助

【问题讨论】:

  • Django 安装在哪里?它是真的安装在您的系统 Python 安装中,还是您使用的是虚拟环境? mod_wsgi 是为哪个版本的 Python 编译的,您要使用哪个版本? modwsgi.readthedocs.io/en/develop/user-guides/…
  • django 是用 pip3 install django 安装的,我不认为我使用的是虚拟环境?一切都是用apt或pip安装的。此外,我所有不是 django 脚本的 wsgi 脚本(另外两个)都可以正常工作。让我弄清楚一切的版本
  • populate() 错误是尝试加载 WSGI 应用程序时发生的错误的副作用。所以此时可以忽略。
  • 你是否安装了 Python 包psycopg2
  • 您应该需要为安装到 Python 安装中的包添加任何额外的路径。这表明您的 mod_wsgi 实际上并未针对您想要的 Python 安装进行编译。找到 mod_wsgi.so 模块并在其上运行 ldd 以查看它与 modwsgi.readthedocs.io/en/develop/user-guides/… 链接的内容

标签: django apache python-3.x mod-wsgi


【解决方案1】:

您可能需要告诉 mod_wsgi 您的项目代码在哪里。对于嵌入式模式,这是通过 WSGIPythonPath 指令完成的。您最好使用守护程序模式,在这种情况下,您可以使用python-path 选项来代替WSGIDaemonProcess 指令。

【讨论】:

  • 所以我试图准确理解 WSGIPythonPath 的作用。您是否列出了 python 代码的每个位置的指令?把 WSGIPythonPath /var/www/protectionprofiles/protectionprofiles/wsgi.py 仍然得到同样的错误
  • 您不能在VirtualHostLocationDirectory 指令中使用WSGIPythonPath。这就是为什么最好使用守护程序模式。为每个应用程序创建单独的守护进程组,并将每个应用程序委托给他们自己的守护进程组。无论哪种方式,WSGIDaemonProcessWSGIPythonPathpython-path 选项的参数都是一个目录,而不是您正在使用的文件。
  • 否则,从 WSGI 脚本文件内部将目录添加到sys.path
【解决方案2】:

好的,我终于找到了解决问题的方法,但不确定具体原因。首先,当我混合自己的 wsgi scipts 和 django 时,我必须仅为 django 的脚本别名指定守护进程

WSGIScriptAlias /certs /var/www/scripts/CavsCertSearch/CavsCertSearch/certstrip.wsgi
    WSGIScriptAlias /testcerts /var/www/scripts/CavsCertSearchTest/CavsCertSearch/certstriptest.wsgi
    WSGIScriptAlias /debug /var/www/scripts/debug/debug.wsgi
    WSGIScriptAlias / /var/www/protectionprofiles/protectionprofiles/wsgi.py process-group=protectionprofiles
    WSGIDaemonProcess protectionprofiles python-path=/var/www/protectionprofiles/
WSGIProcessGroup protectionprofiles

然后在开发模式下的 app/settings.py 中,一个后端工作,但在 mod_wsgi 中运行时,我需要一个不同的后端: 设置.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'databse',
        'USER': 'user',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

在开发服务器中“'django.db.backends.postgresql”工作正常,但在发布时我必须指定确切的模块类型。我不知道为什么,但它现在可以工作了!!

【讨论】:

  • 听起来你在生产和开发中有不同的 Django 版本。 process-group 选项到 WSGIScriptAlias 不应该产生影响,因为 WSGIProcessGroup 做同样的事情。
  • 嗯,我得调查一下。我在浏览文档时验证了我的 django 版本,并且安装它的唯一方法是 pip3 install django....
猜你喜欢
  • 2021-01-28
  • 1970-01-01
  • 2020-11-03
  • 2018-12-21
  • 2017-06-13
  • 2012-10-21
  • 2017-04-21
相关资源
最近更新 更多