网站使用默认的python解释器而不是安装virtualenv解释器

时间:2014-10-08 16:46:02

标签: python django python-2.7 centos virtualenv

我在centos上安装了python 2.7。

我创建并激活了与此解释器关联的virtualenv。

我的路径如下:

/home/ec2-user/django-venv/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin

当我点击我的网站时,我收到以下错误,表明它正在使用旧版本的python:

mod_wsgi (pid=19386): Exception occurred processing WSGI script '/var/www/html/-django//wsgi.py'.
 Traceback (most recent call last):
 File "/var/www/html/-django//wsgi.py", line 23, in <module>
   from django.core.wsgi import get_wsgi_application
 File "/usr/lib/python2.6/site-packages/django/core/wsgi.py", line 2, in <module>
   from django.core.handlers.wsgi import WSGIHandler
 File "/usr/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 11, in <module>
   from django import http
 File "/usr/lib/python2.6/site-packages/django/http/__init__.py", line 2, in <module>
   from django.http.request import (HttpRequest, QueryDict,
 File "/usr/lib/python2.6/site-packages/django/http/request.py", line 11, in <module>
   from django.conf import settings
 File "/usr/lib/python2.6/site-packages/django/conf/__init__.py", line 9, in <module>
   import importlib
 ImportError: No module named importlib

我创建了这样的virtualenv:

virtualenv --python=/usr/local/bin/python2.7 django-venv
然后我按照这样激活它:

source django-venv/bin/activate

当我发起“哪个python”时,我得到以下输出:

~/django-venv/bin/python

我的apache httpd.conf条目如下所示:

Alias /static/ /var/www/html/django/shared/static/

<Directory /var/www/html/django/shared/static/>
Order deny,allow
Allow from all
</Directory>


WSGIScriptAlias / /var/www/html/django/wsgi.py
WSGIPythonPath /var/www/html/django

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

但我仍然得到上面的错误,它使用旧的Python版本2.6

如何让我的网站使用这个指向正确位置的virtualenv?

2 个答案:

答案 0 :(得分:1)

我只使用wsgi.py生成的manage.py startproject文件,然后将这样的内容放在我的httpd conf文件中:

WSGIScriptAlias / /full/path/to/wsgi.py
WSGIPythonPath /full/path/to/django/project:/full/path/to/django-venv/lib/python2.7/site-packages

官方文档cover this

答案 1 :(得分:0)

如果你只是在shell中运行它,那只会影响那个shell会话,甚至不会在登录之间持续存在,更不用说影响像Apache这样的系统范围的服务。

通常你会在你的wsgi.py中添加一些东西:

activate_this = '/path/to/virtualenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))