gpt4 book ai didi

Django Apache 和 Virtualenv ImportError : No module named site

转载 作者:行者123 更新时间:2023-12-03 15:59:28 26 4
gpt4 key购买 nike

504页面后来自apache的错误

[info] mod_wsgi (pid=): Python home /var/venv/mybox.
[info] mod_wsgi (pid=): Initializing Python.
ImportError: No module named site

这是一个几乎没有配置的应用程序。
<IfModule mod_wsgi.c>
WSGIDaemonProcess myapp python-home=/var/venv/mybox
WSGIProcessGroup myapp
WSGIScriptAlias / /var/www/html/web/myapp/wsgi.py
WSGISocketPrefix /var/run/wsgi

<Directory /var/www/html/web>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</IfModule>

尽我所能关注每一篇文章和教程。我在 CENTOS6 上。使用虚拟环境 python 2.7 默认系统环境是 2.6
$ ldd /etc/httpd/modules/mod_wsgi.so
linux-vdso.so.1 => (0x00007ffc06174000)

mywsgi.py
 import os,sys     
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
sys.path.insert(0,'/var/www/html/web')
activate_this = '/var/venv/mybox/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
application = get_wsgi_application()

PYHTONHOME 未设置

最佳答案

在 mod_wsgi 中使用虚拟环境的文档可以在以下位置找到:

  • http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html

  • 在您的情况下,最重要的是以下部分:
  • Virtual Environment and Python Version

  • 在该部分中,它指出:

    When using a Python virtual environment with mod_wsgi, it is very important that it has been created using the same Python installation that mod_wsgi was originally compiled for. It is not possible to use a Python virtual environment to force mod_wsgi to use a different Python version, or even a different Python installation.

    You cannot for example force mod_wsgi to use a Python virtual environment created using Python 3.5 when mod_wsgi was originally compiled for Python 2.7. This is because the Python library for the Python installation it was originally compiled against is linked directly into the mod_wsgi module.


    所以最有可能发生的事情是 mod_wsgi 是为 Python 2.6 编译的。在这种情况下,您不能强制它使用从 Python 2.7 创建的 Python 虚拟环境。当你这样做时,你会得到你看到的关于 site 的错误。模块丢失。
    您需要从系统包中卸载 mod_wsgi 并从源代码安装 mod_wsgi,针对 Python 2.7 进行编译。最简单的方法可能是使用 pip install方法如中所述:
  • https://pypi.python.org/pypi/mod_wsgi

  • 运行 pip install将其安装在您的虚拟环境中,然后按照“连接到 Apache 安装”部分中有关配置 Apache 以使用它的说明进行操作。

    关于Django Apache 和 Virtualenv ImportError : No module named site,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41005030/

    26 4 0