【问题标题】:Python virtualenv picks up wrong libraryPython virtualenv 选择了错误的库
【发布时间】: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


【解决方案1】:

创建隔离环境,无需访问全局站点包。有意启用/禁用该行为。最近的版本默认禁用访问。

我假设您想要一个隔离的环境来测试 Python 3.3.4。 下面我使用的是系统python 2.7.6提供的virtualenv

$ 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,切换到目录并使用相对路径从这个virtualenv中寻址python解释器和pip。

$ 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'

在创建您的 virtualenv 时,您需要继续使用该选项

--python=/usr/local/bin/python3

像你已经做的那样引用你的 Python 3.3.4 解释器。

最近的默认 virtualenv 通常只安装了几个包。如果您有更多,您可能可以访问全球站点包。

$ bin/pip freeze
Pygments==2.0.2
argparse==1.2.1
wsgiref==0.1.2

【讨论】:

  • 按照你的步骤,当我为 python3 创建一个 virtualenv 时,我仍然得到同样的错误。创建新的 virtualenv 后,pip freeze 没有给我预期的站点包。
  • 更新您的问题,然后反映您所做的事情。在安装之前尝试导入 pygments。这应该引发两个不同的例外。第一个应该指出一个丢失的包,因为你还没有安装它。
  • 获取另一个 Python 3.4 安装,重复所有步骤。或调查您的 virtualenv 中的文件和文件夹(站点包)以查找 pygments 资源或 *.pth 文件
猜你喜欢
  • 2017-06-26
  • 2020-07-22
  • 1970-01-01
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 2011-07-01
相关资源
最近更新 更多