【发布时间】:2015-06-27 10:08:41
【问题描述】:
$ virtualenv --version
13.0.3
我使用 Python3 创建了一个新的 virtualenv,但无法访问全局站点包。
$ virtualenv --no-site-packages venv_pygments --python=/usr/local/bin/python3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4'
New python executable in venv_pygments/bin/python3.4
Also creating executable in venv_pygments/bin/python
Installing setuptools, pip, wheel...done.
然后我使用virtualenv的Python3解释器并尝试导入pygments
$ cd venv_pygments
$ venv_pygments bin/python3
Python 3.4.3 (default, May 1 2015, 19:14:18)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.49)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygments
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Applications/QGIS.app/Contents/Resources/python/pygments/__init__.py", line 46
except TypeError, err:
^
SyntaxError: invalid syntax
pip freeze 虽然只显示这些包
$ bin/pip freeze
wheel==0.24.0
所以看起来 virtualenv 的 Python3 正在访问全局站点包。我怎样才能避免这种情况?
当我为 virtualenv 安装 pygments 时它不会改变
$ bin/pip install pygments
Collecting pygments
Using cached Pygments-2.0.2-py3-none-any.whl
Installing collected packages: pygments
Successfully installed pygments-2.0.2
$ venv_pygments bin/python3
Python 3.4.3 (default, May 1 2015, 19:14:18)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.49)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygments
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Applications/QGIS.app/Contents/Resources/python/pygments/__init__.py", line 46
except TypeError, err:
^
SyntaxError: invalid syntax
>>>
注意:原始问题是根据 cmets 更新的。
【问题讨论】:
-
尝试对 pip 和 python 使用相对或绝对路径。可能是你实际使用系统 python 资源。
-
您是否尝试过使用
pip install --upgrade pip升级pip,就像输出消息中所说的那样? -
@miindlek 是的。没有区别?
-
@SaschaGottfried 你能举个例子,我如何将 pip 与相对/绝对路径一起使用?
-
现在我很确定,您的问题与访问全局站点包有关。查找 mkvirtualenv 的相关选项以限制对全局站点包的访问。
标签: python virtualenv qgis pygments