OSX 10.5 上的 Python 2.7 Virtualenv 路径问题
问题描述
当我尝试加载正在使用的 Django 网站时遇到的问题 mod_wsgi、python 2.7 和 virtualenv 的问题是,当我在浏览器中访问该站点时,出现 500 错误。什么时候 我查看了 apache 日志中创建的错误,得到以下内容...
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] mod_wsgi (pid=69270): Exception occurred processing WSGI script '/Volumes/Ontario/Sites/emails/config/apache/dev.emails.wenatcheeworld.com.wsgi'.
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] Traceback (most recent call last):
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] File "/Library/Python/2.5/site-packages/Django-1.2.1-py2.5.egg/django/core/handlers/wsgi.py", line 230, in __call__
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] self.load_middleware()
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] File "/Library/Python/2.5/site-packages/Django-1.2.1-py2.5.egg/django/core/handlers/base.py", line 33, in load_middleware
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] for middleware_path in settings.MIDDLEWARE_CLASSES:
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] File "/Library/Python/2.5/site-packages/Django-1.2.1-py2.5.egg/django/utils/functional.py", line 276, in __getattr__
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] self._setup()
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] File "/Library/Python/2.5/site-packages/Django-1.2.1-py2.5.egg/django/conf/__init__.py", line 40, in _setup
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] self._wrapped = Settings(settings_module)
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] File "/Library/Python/2.5/site-packages/Django-1.2.1-py2.5.egg/django/conf/__init__.py", line 75, in __init__
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] raise ImportError("Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e))
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] ImportError: Could not import settings 'config.settings' (Is it on sys.path? Does it have syntax errors?): No module named config.settings
回溯没有意义,因为正在加载的文件来自全局 python 2.5 库。
Virtualenv 设置信息
我在运行 OS X 10.5.8 的 OS X 服务器上安装了 python 2.7。我 使用以下命令安装了 python Python 2.7.2 Mac OS X 32-位 i386/PPC 安装程序。服务器当前安装了 virtualenv 1.6.4。
我使用以下命令为新网站设置 virtualenv,
$ cd /usr/local/virtualenvs
$ virtualenv --no-site-packages -p /usr/local/bin/python2.7 dev.emails
然后安装所有 nessary python 软件包。当我运行 pip freeze
时,这是我的输出
$ pip freeze
Django==1.3
PIL==1.1.7
South==0.7.3
amqplib==1.0.2
anyjson==0.3.1
boto==2.0
celery==2.2.7
django-celery==2.2.4
django-compressor==0.9.2
django-grappelli==2.3.4
django-picklefield==0.1.9
feedparser==5.0.1
html2text==3.02
kombu==1.4.1
lxml==2.2.2
psycopg2==2.4.2
pyparsing==1.5.6
python-dateutil==1.5
python-sendgrid==0.2.0dev
redis==2.4.9
requests==0.6.2
wsgiref==0.1.2
故障排除步骤
我创建了一个名为 path_test.py
的脚本来测试我的 virtualenv 路径...
import os
import sys
# Activate Python Virtual Enviroment
activate_this = '/usr/local/virtualenvs/dev.emails/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
# start django
import django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'config.settings'
for p in sys.path:
print p
application = django.core.handlers.wsgi.WSGIHandler()
当我使用python path_test.py
我得到以下输出:
/usr/local/virtualenvs/dev.emails/lib/python2.5/site-packages
/Volumes/Ontario/Sites/emails/bin
/Library/Python/2.5/site-packages/MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg
/Library/Python/2.5/site-packages/paramiko-1.7.4-py2.5.egg
/Library/Python/2.5/site-packages/simplejson-2.0.7-py2.5-macosx-10.5-i386.egg
/Library/Python/2.5/site-packages/python_ldap-2.3.7-py2.5-macosx-10.5-i386.egg
/Library/Python/2.5/site-packages/lxml-2.2.2-py2.5-macosx-10.5-i386.egg
/Library/Python/2.5/site-packages/pytz-2010h-py2.5.egg
/Library/Python/2.5/site-packages/python_openid-2.2.4-py2.5.egg
/Library/Python/2.5/site-packages/Markdown-2.0.3-py2.5.egg
/Library/Python/2.5/site-packages/django_authopenid-1.0.1-py2.5.egg
/Library/Python/2.5/site-packages/Django-1.2.1-py2.5.egg
/Library/Python/2.5/site-packages/django_profiles-0.2-py2.5.egg
/Library/Python/2.5/site-packages/django_openid_consumer-0.1.1-py2.5.egg
/Library/Python/2.5/site-packages/setuptools-0.6c11-py2.5.egg
/Library/Python/2.5/site-packages/ipython-0.10-py2.5.egg
/Library/Python/2.5/site-packages/httplib2-0.6.0-py2.5.egg
/Library/Python/2.5/site-packages/guess_language-0.2-py2.5.egg
/Library/Python/2.5/site-packages/django_ratings-0.3.4-py2.5.egg
/Library/Python/2.5/site-packages/pip-0.8.1-py2.5.egg
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload
/Library/Python/2.5/site-packages
/Library/Python/2.5/site-packages/PIL
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC
如果我首先使用 source /path/to/virtualenv/bin/activate
激活 virtualenv,然后运行 python path_test .py
我明白了以下输出:
/usr/local/virtualenvs/dev.emails/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/usr/local/virtualenvs/dev.emails/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg
/usr/local/virtualenvs/dev.emails/lib/python2.7/site-packages/lxml-2.2.2-py2.7-macosx-10.3-intel.egg
/usr/local/virtualenvs/dev.emails/lib/python2.7/site-packages
/usr/local/virtualenvs/dev.emails/lib/python2.7/site-packages/PIL
/Volumes/Ontario/Sites/emails/bin
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/site-packages/lxml-2.2.2-py2.7-macosx-10.3-intel.egg
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python27.zip
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/plat-darwin
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/plat-mac
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/plat-mac/lib-scriptpackages
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/lib-tk
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/lib-old
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/lib-dynload
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/site-packages
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/site-packages/PIL
/Volumes/Ontario/Sites/emails
此时我很确定问题是 virtualenv 中的 activate_this.py
没有设置正确的 sys.path
。
Problem Discription
The issues I'm having when I try to load a Django website that's using
mod_wsgi, python 2.7 and virtualenv, is that I get a 500 error when I go to the site in my browser. When
I look at the error that's created in my apache log I get the following...
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] mod_wsgi (pid=69270): Exception occurred processing WSGI script '/Volumes/Ontario/Sites/emails/config/apache/dev.emails.wenatcheeworld.com.wsgi'.
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] Traceback (most recent call last):
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] File "/Library/Python/2.5/site-packages/Django-1.2.1-py2.5.egg/django/core/handlers/wsgi.py", line 230, in __call__
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] self.load_middleware()
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] File "/Library/Python/2.5/site-packages/Django-1.2.1-py2.5.egg/django/core/handlers/base.py", line 33, in load_middleware
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] for middleware_path in settings.MIDDLEWARE_CLASSES:
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] File "/Library/Python/2.5/site-packages/Django-1.2.1-py2.5.egg/django/utils/functional.py", line 276, in __getattr__
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] self._setup()
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] File "/Library/Python/2.5/site-packages/Django-1.2.1-py2.5.egg/django/conf/__init__.py", line 40, in _setup
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] self._wrapped = Settings(settings_module)
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] File "/Library/Python/2.5/site-packages/Django-1.2.1-py2.5.egg/django/conf/__init__.py", line 75, in __init__
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] raise ImportError("Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e))
[Tue Oct 11 09:32:30 2011] [error] [client 206.130.131.226] ImportError: Could not import settings 'config.settings' (Is it on sys.path? Does it have syntax errors?): No module named config.settings
The traceback doesn't make sense because the files that are getting loaded are from the global python 2.5 library.
Virtualenv Setup Information
I installed python 2.7 on an OS X Server that's running OS X 10.5.8. I
installed python using the
Python 2.7.2 Mac OS X 32-bit i386/PPC Installer. The server currently has virtualenv 1.6.4 installed.
I setup my virtualenv using for a new website using the following
$ cd /usr/local/virtualenvs
$ virtualenv --no-site-packages -p /usr/local/bin/python2.7 dev.emails
I then install all the nessary python packages. When I run pip freeze
this is my output
$ pip freeze
Django==1.3
PIL==1.1.7
South==0.7.3
amqplib==1.0.2
anyjson==0.3.1
boto==2.0
celery==2.2.7
django-celery==2.2.4
django-compressor==0.9.2
django-grappelli==2.3.4
django-picklefield==0.1.9
feedparser==5.0.1
html2text==3.02
kombu==1.4.1
lxml==2.2.2
psycopg2==2.4.2
pyparsing==1.5.6
python-dateutil==1.5
python-sendgrid==0.2.0dev
redis==2.4.9
requests==0.6.2
wsgiref==0.1.2
Trouble Shooting Steps
I created a script called path_test.py
in order to test my virtualenv path...
import os
import sys
# Activate Python Virtual Enviroment
activate_this = '/usr/local/virtualenvs/dev.emails/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
# start django
import django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'config.settings'
for p in sys.path:
print p
application = django.core.handlers.wsgi.WSGIHandler()
When I run the script using python path_test.py
I get the following output:
/usr/local/virtualenvs/dev.emails/lib/python2.5/site-packages
/Volumes/Ontario/Sites/emails/bin
/Library/Python/2.5/site-packages/MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg
/Library/Python/2.5/site-packages/paramiko-1.7.4-py2.5.egg
/Library/Python/2.5/site-packages/simplejson-2.0.7-py2.5-macosx-10.5-i386.egg
/Library/Python/2.5/site-packages/python_ldap-2.3.7-py2.5-macosx-10.5-i386.egg
/Library/Python/2.5/site-packages/lxml-2.2.2-py2.5-macosx-10.5-i386.egg
/Library/Python/2.5/site-packages/pytz-2010h-py2.5.egg
/Library/Python/2.5/site-packages/python_openid-2.2.4-py2.5.egg
/Library/Python/2.5/site-packages/Markdown-2.0.3-py2.5.egg
/Library/Python/2.5/site-packages/django_authopenid-1.0.1-py2.5.egg
/Library/Python/2.5/site-packages/Django-1.2.1-py2.5.egg
/Library/Python/2.5/site-packages/django_profiles-0.2-py2.5.egg
/Library/Python/2.5/site-packages/django_openid_consumer-0.1.1-py2.5.egg
/Library/Python/2.5/site-packages/setuptools-0.6c11-py2.5.egg
/Library/Python/2.5/site-packages/ipython-0.10-py2.5.egg
/Library/Python/2.5/site-packages/httplib2-0.6.0-py2.5.egg
/Library/Python/2.5/site-packages/guess_language-0.2-py2.5.egg
/Library/Python/2.5/site-packages/django_ratings-0.3.4-py2.5.egg
/Library/Python/2.5/site-packages/pip-0.8.1-py2.5.egg
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload
/Library/Python/2.5/site-packages
/Library/Python/2.5/site-packages/PIL
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC
If I activate the virtualenv first using source /path/to/virtualenv/bin/activate
and then run python path_test.py
I get the following output:
/usr/local/virtualenvs/dev.emails/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/usr/local/virtualenvs/dev.emails/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg
/usr/local/virtualenvs/dev.emails/lib/python2.7/site-packages/lxml-2.2.2-py2.7-macosx-10.3-intel.egg
/usr/local/virtualenvs/dev.emails/lib/python2.7/site-packages
/usr/local/virtualenvs/dev.emails/lib/python2.7/site-packages/PIL
/Volumes/Ontario/Sites/emails/bin
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/site-packages/lxml-2.2.2-py2.7-macosx-10.3-intel.egg
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python27.zip
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/plat-darwin
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/plat-mac
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/plat-mac/lib-scriptpackages
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/lib-tk
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/lib-old
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/lib-dynload
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/site-packages
/Volumes/Ontario/local/virtualenvs/dev.emails/lib/python2.7/site-packages/PIL
/Volumes/Ontario/Sites/emails
At this point I'm pretty sure the problem is that activate_this.py
in the virtualenv isn't setting up the correct sys.path
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你有几个问题。
首先,activate_this.py 工作正常。问题出在你的设置上。您正在尝试激活 virtualenv,但您使用的是系统默认的 2.5 python,而不是 virtualenv 的 2.7 python。这就是为什么您会看到系统站点包; ''--no-site-packages”仅适用于 virtualenv 创建的 python 二进制文件。这也是它添加“/usr/local/virtualenvs/dev.emails/lib/python2.5/site-packages”的原因。 activate_this.py 脚本使用执行脚本的 python 版本来确定要使用的 site-packages 目录:
当您激活 virtualenv 时,它使用正确的 2.7 二进制文件
其次,您的 mod_wsgi 版本是针对构建的 。系统是 2.5 python,因此它会遇到与未激活 virtualenv 时看到的相同问题。“activate_this.py”脚本实际上没有执行任何操作来强制执行“--no-site-packages”选项,它依赖于python 二进制文件由 virtualenv 创建,因此 mod_wsgi 永远不会强制执行“--no-site-packages”,这就是为什么当它找不到 2.5 时,您会看到它回退到系统上安装的 Django 版本。 第三,Apache 在10.5
上以 64 位运行,并且您安装了一个仅 32 位的 python 二进制文件,因此即使您针对 2.7 版本重建了 mod_wsgi,Apache 在尝试时也会崩溃。加载它。当然,所有 2.5 站点都会遇到与 2.7 站点现在相同的问题。你最好的选择是在gunicorn 之类的东西后面运行你的2.7 站点。
You've got a couple of problems.
First, activate_this.py is working fine. The problem is with your setup. You're trying to activate the virtualenv, but you're using the system's default 2.5 python and not the virtualenv's 2.7 python. That is why you are seeing the system site-packages; ''--no-site-packages" only applies to the python binary created by virtualenv. That is also why it is adding "/usr/local/virtualenvs/dev.emails/lib/python2.5/site-packages". The activate_this.py script uses the version of python executing the script to determine the site-packages directory to use:
When you activate the virtualenv, it uses the correct 2.7 binary.
Second, your version of mod_wsgi was built against the system's 2.5 python, so it is encountering the same problem you see when the virtualenv isn't activated. The "activate_this.py" script actually does nothing to enforce the "--no-site-packages" option, it relies on the python binary created by virtualenv, so mod_wsgi will never enforce "--no-site-packages". Which is why you see it fall back to the version of Django installed on the system when it doesn't find a 2.5 site-packages directory in the virtualenv.
Third, Apache runs in 64-bit on 10.5 and you've installed a python binary that is 32-bit only, so even if you rebuilt mod_wsgi against your 2.7 version, Apache would crash when trying to load it. And of course all of your 2.5 sites would have the same problem your 2.7 ones do now. Your best bet is to run your 2.7 sites behind something like gunicorn.
看起来 mod_wsgi 默认为 Python 2.5。你编译正确吗?请查看 http://code.google.com/p /modwsgi/wiki/InstallationIssues#Multiple%5FPython%5FVersions。
从 virtualenv 和 mod_wsgi[1] 的页面:
[1] http://code.google.com/p/modwsgi/wiki/VirtualEnvironments< /a>
It looks like mod_wsgi is defaulting to Python 2.5. Did you compile it correctly? Have a look at http://code.google.com/p/modwsgi/wiki/InstallationIssues#Multiple%5FPython%5FVersions.
From the page on virtualenv and mod_wsgi[1]:
[1] http://code.google.com/p/modwsgi/wiki/VirtualEnvironments
如果您已经根据 Python 2.7 从源代码构建了 mod_wsgi,但发现它采用了 Python 2.5 框架(并且由于某种原因没有崩溃),请
在使用 Python 2.7 从源代码构建 mod_wsgi 时尝试使用:进行“配置”。
如果您甚至没有针对 Python 2.7 重建 mod_wsgi,那么请先这样做。
If you have built mod_wsgi from source code against Python 2.7, but find it picking up Python 2.5 framework (and not crashing for some reason), try using the:
to 'configure' when building mod_wsgi from source code with Python 2.7.
If you didn't even rebuild mod_wsgi against Python 2.7, then do that first.
正如我在回答我自己的问题时指出的以及其他人的回答那样,不可能将不同版本的 python 与 mod_wsgi 一起使用。我的解决方案是为需要不同版本的 python 的特定站点安装和设置 Gunicorn。
首先我安装了gunicorn...
然后我设置了一个launchd.plist来在
/Library/LaunchDaemons/my.examplesite.com.plist
中运行gunicorn:接下来我设置nginx来服务器媒体并代理到我的gunicorn服务器:
As I pointed out in my reply to my own question and as others have replied it's not possible to use different version of python with mod_wsgi. My solution was to install and setup gunicorn for my specific site that needed a different version of python.
First I installed gunicorn...
Then I setup a launchd.plist to run gunicorn in
/Library/LaunchDaemons/my.examplesite.com.plist
:Next I setup nginx to server media and proxy to my gunicorn server: