【问题标题】:My python in virtual-env is 2.7 but the Django debug screen shows the system's 2.6.6我在 virtual-env 中的 python 是 2.7,但 Django 调试屏幕显示系统的 2.6.6
【发布时间】:2015-03-18 04:22:27
【问题描述】:

所以在我的虚拟环境中,如果我激活它并输入python,2.7 版就会打开,应该是这样。

当 Django 出现错误时,它会显示 2.6 版本,这是系统默认值。这是我的 Apache 配置的样子:

<VirtualHost *:80>
    ServerName www.mysite.com
    ErrorLog /var/www/virtualenv-2.7/django-error-log
    Alias /static/ /var/www/virtualenv-2.7/mysite/mainapp/static/
    WSGIDaemonProcess mysite python-path=/var/www/virtualenv-2.7/mysite:/var/www/virtualenv-2.7/lib/python2.7/site-packages
    WSGIProcessGroup mysite
    WSGIScriptAlias / /var/www/virtualenv-2.7/mysite/mysite/wsgi.py

    <Directory /var/www/virtualenv-2.7>
    Order allow,deny
    Allow from all
    </Directory>
</VirtualHost>

WSGISocketPrefix /var/run/wsgi
WSGIPythonPath /var/www/virtualenv-2.7/mysite:var/www/virutalenv-2.7/lib/python2.7/site-packages

我的 wsgi.py:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

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

当我service httpd restart 时,这是放在 apache 错误日志中的内容:

[Mon Jan 19 20:19:03 2015] [notice] caught SIGTERM, shutting down
[Mon Jan 19 20:19:04 2015] [notice] SELinux policy enabled; httpd running as context unconfined_u:system_r:httpd_t:s0
[Mon Jan 19 20:19:04 2015] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Mon Jan 19 20:19:04 2015] [notice] Digest: generating secret for digest authentication ...
[Mon Jan 19 20:19:04 2015] [notice] Digest: done
[Mon Jan 19 20:19:04 2015] [notice] Apache/2.2.15 (Unix) DAV/2 mod_wsgi/3.2 Python/2.6.6 configured -- resuming normal operations

知道为什么 Django 不使用虚拟环境中的 2.7 吗?

【问题讨论】:

  • 可能是因为您的 mod_wsgi 是针对系统 Python 编译的。检查 Apache 的错误日志以获取启动消息。
  • 我从 apache error_log 添加了日志

标签: python django apache python-2.7 mod-wsgi


【解决方案1】:

mod_wsgi 模块是针对 Python 的系统版本编译的;线索在启动消息中:

                           mod_wsgi library version
                           vvvvvvvvvvvv
Apache/2.2.15 (Unix) DAV/2 mod_wsgi/3.2 Python/2.6.6 configured
                                        ^^^^^^^^^^^^
                                        Python version

要解决此问题,您可以针对所需的 Python 版本编译 mod_wsgi,或使用 WSGIPythonHome 配置指令:

用于指示 Python 何时初始化其库的位置 文件已安装。这应该在 Python 中定义 可执行文件不在 Apache 运行的用户的 PATH 中,或者 一个系统在不同的地方安装了多个版本的 Python 文件系统中的位置,尤其是不同的安装 相同的主要/次要版本,以及 Apache 找到的安装 在它的 PATH 中不是想要的。

该指令也可用于指示 Python 虚拟 使用 virtualenv 等工具创建的环境,用于 整个 mod_wsgi。

在您的 Apache 配置中,添加以下内容,然后重新加载服务器:

WSGIPythonHome /var/www/virtualenv-2.7/lib/python2.7

【讨论】:

  • Starting httpd: Syntax error on line 1023 of /etc/httpd/conf/httpd.conf: WSGIPythonHome cannot occur within &lt;VirtualHost&gt; section [FAILED]
  • 仅在服务器级别支持。
  • 已修复,日志显示:[notice] Apache/2.2.15 (Unix) DAV/2 mod_wsgi/3.2 Python/2.6.6 configured -- resuming normal operations [error] mod_wsgi (pid=15153): Unable to import 'site' module.
  • 那是因为你把你的源代码和你的虚拟环境放在了同一个目录下;我只是在看了你的路径后才意识到这一点。你不应该玩虚拟环境;它不是您应该放置源代码的地方。无论如何,我用我希望是正确的路径编辑了答案。
  • 据我所知,我没有编辑任何东西,所以不确定你在做什么。我更新了 httpd.conf 并重新启动,消息是一样的。
猜你喜欢
  • 2019-01-31
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
  • 2011-01-03
  • 2014-04-12
  • 2017-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多