0

我正在将 Django 与 virtualenv 一起部署在生产服务器上,并且无法在服务器上激活 virtualenv

source .../bin/activate

我做了一些研究,发现pythonpath会根据我们是否在virtualenv中而改变。

sys.path(激活 virtualenv)

['',
'/.../virtualenv/test_path/bin',
'/.../virtualenv/test_path/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
'/.../virtualenv/test_path/local/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg',
'/.../virtualenv/test_path/lib/python2.7',
'/.../virtualenv/test_path/lib/python2.7/plat-linux2',
'/.../virtualenv/test_path/lib/python2.7/lib-tk',
'/.../virtualenv/test_path/lib/python2.7/lib-old',
'/.../virtualenv/test_path/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/.../virtualenv/test_path/local/lib/python2.7/site-packages',
'/.../virtualenv/test_path/local/lib/python2.7/site-packages/IPython/extensions']

sys.path(不激活 virtualenv):

['',
'/usr/local/bin',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PIL',
'/usr/lib/python2.7/dist-packages/gst-0.10',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/local/lib/python2.7/dist-packages/IPython/extensions']

只需将 pythonpath 更改为指向 virtualenv 就足够了

.../python2.7/site-packages

文件夹以获得与运行相同的结果

source .../bin/activate

?

4

1 回答 1

2

不它不是。virtualenv 不仅仅是关于site-packages,它是关于一个完整的孤立的 python 环境。

这样做source /path/to/venv/bin/activate只是更改您的$PATH环境变量以包含您的 virtualenvbin目录作为第一次查找。

如果你python直接调用,它只是一个快捷方式:

$ /path/to/venv/bin/python myscript.py

如果你调用pip一个激活的virtualenv,它是一样的:

$ /path/to/venv/bin/pip install XYZ
于 2012-12-15T14:22:42.367 回答