pythonvirtualenv拾取错误的库
我使用Python3创建了一个新的virtualenv,但无法访问全局站点包pythonvirtualenv拾取错误的库,python,virtualenv,qgis,pygments,Python,Virtualenv,Qgis,Pygments,我使用Python3创建了一个新的virtualenv,但无法访问全局站点包 $ virtualenv --version 13.0.3 然后我使用virtualenv的Python3解释器,尝试导入pygments $ virtualenv --no-site-packages venv_pygments --python=/usr/local/bin/python3 Running virtualenv with interpreter /usr/local/bin/python3 Usi
$ virtualenv --version
13.0.3
然后我使用virtualenv的Python3解释器,尝试导入pygments
$ 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正在访问全局站点包。我怎样才能避免呢
当我为virtualenv安装pygments时,它不会改变
$ bin/pip freeze
wheel==0.24.0
$bin/pip安装pygments
收集pygments
使用缓存的Pygments-2.0.2-py3-none-any.whl
安装收集的软件包:pygments
已成功安装pygments-2.0.2
$venv_pygments bin/python3
Python 3.4.3(默认值,2015年5月1日,19:14:18)
[GCC 4.2.1达尔文兼容苹果LLVM 6.1.0(clang-602.0.49)]
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>导入pygments
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/Applications/QGIS.app/Contents/Resources/python/pygments/_uinit__;u.py”,第46行
除了TypeError,err:
^
SyntaxError:无效语法
>>>
注意:原始问题已根据注释进行了更新。创建隔离环境而不访问全局站点包。有意启用/禁用该行为。默认情况下,最新版本禁用访问 我假设您需要一个隔离的环境来测试Python 3.3.4。 下面我使用的是系统python 2.7.6提供的virtualenv
$ 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
>>>
我创建了一个virtualenv,切换到目录中,并使用相对路径从这个virtualenv中寻址python解释器和pip
$ virtualenv --version
1.11.2
$ virtualenv
You must provide a DEST_DIR
Usage: virtualenv [OPTIONS] DEST_DIR
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-v, --verbose Increase verbosity.
-q, --quiet Decrease verbosity.
-p PYTHON_EXE, --python=PYTHON_EXE
The Python interpreter to use, e.g.,
--python=python2.5 will use the python2.5 interpreter
to create the new environment. The default is the
interpreter that virtualenv was installed with
(/usr/bin/python)
--clear Clear out the non-root install and start from scratch.
--no-site-packages DEPRECATED. Retained only for backward compatibility.
Not having access to global site-packages is now the
default behavior.
--system-site-packages
Give the virtual environment access to the global
site-packages.
--always-copy Always copy files rather than symlinking.
--unzip-setuptools Unzip Setuptools when installing it.
--relocatable Make an EXISTING virtualenv environment relocatable.
This fixes up scripts and makes all .pth files
relative.
--no-setuptools Do not install setuptools (or pip) in the new
virtualenv.
--no-pip Do not install pip in the new virtualenv.
--extra-search-dir=DIR
Directory to look for setuptools/pip distributions in.
This option can be used multiple times.
--never-download DEPRECATED. Retained only for backward compatibility.
This option has no effect. Virtualenv never downloads
pip or setuptools.
--prompt=PROMPT Provides an alternative prompt prefix for this
environment.
--setuptools DEPRECATED. Retained only for backward compatibility.
This option has no effect.
--distribute DEPRECATED. Retained only for backward compatibility.
This option has no effect.
在创建virtualenv时,您需要继续使用该选项
--python=/usr/local/bin/python3
如您所做的那样引用Python3.3.4解释器
最近的默认virtualenv通常只安装了几个软件包。如果你有更多,你可以访问全球网站包
$ virtualenv --no-site-packages venv_pygments
New python executable in venv_pygments/bin/python
Installing setuptools, pip...done.
$ cd venv_pygments/
$ bin/pip install pygments
Downloading/unpacking pygments
Downloading Pygments-2.0.2-py2-none-any.whl (672kB): 672kB downloaded
Installing collected packages: pygments
Successfully installed pygments
Cleaning up...
$ bin/python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygments
>>> pygments.__version__
'2.0.2'
尝试对pip和python使用相对或绝对路径。可能您实际使用的是系统python资源。您是否尝试过使用
pip安装升级pip——升级pip
,就像输出消息中所说的那样?@miindlek是这样做的。没有区别?@saschagotfried您能举个例子说明我如何使用具有相对/绝对路径的pip吗?现在我很确定,您的问题与访问全局站点包有关。查找mkvirtualenv的相关选项以限制对全局站点包的访问。按照您的步骤,我在为python3创建virtualenv时仍然会遇到相同的错误。在创建了一个新的virtualenv之后,pip freeze没有给我预期的站点包。请更新您的问题以反映您所做的事情。请在安装pygments之前尝试导入它。这应该引起两个不同的例外。第一个应该指出缺少一个包,因为您还没有安装它。或者调查virtualenv中的文件和文件夹(站点包),以查找pygments资源或*.pth文件
$ virtualenv --no-site-packages venv_pygments
New python executable in venv_pygments/bin/python
Installing setuptools, pip...done.
$ cd venv_pygments/
$ bin/pip install pygments
Downloading/unpacking pygments
Downloading Pygments-2.0.2-py2-none-any.whl (672kB): 672kB downloaded
Installing collected packages: pygments
Successfully installed pygments
Cleaning up...
$ bin/python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygments
>>> pygments.__version__
'2.0.2'
$ bin/pip freeze
Pygments==2.0.2
argparse==1.2.1
wsgiref==0.1.2