Python virtualenv使用了错误的库

2024-05-17 04:34:46 发布

您现在位置:Python中文网/ 问答频道 /正文

$ virtualenv --version
13.0.3

我用Python3创建了一个新的virtualenv,没有访问全局站点包的权限。在

^{pr2}$

然后我使用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
>>> 

注:原问题根据评论进行了更新。在


Tags: pipdefaultapplebinvirtualenvvenvpygments站点
1条回答
网友
1楼 · 发布于 2024-05-17 04:34:46

创建无法访问全局站点包的隔离环境。有意启用/禁用该行为。默认情况下,最新版本禁用访问。在

我假设您需要一个独立的环境来测试python3.3.4。 下面我使用的是系统python2.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。在

^{pr2}$

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

python=/usr/local/bin/python3

引用Python3.3.4解释器。在

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

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

相关问题 更多 >