Python 3 virtualenv with site packages does not bo

2020-07-24 06:55发布

问题:

I'm creating a new Python virtual environment with Python's 3.5 built-in virrtualenv package:

D:\Projects>python -m venv --system-site-packages proj_3

D:\Projects>cd proj_3

D:\Projects\proj_3>Scripts\activate.bat

(proj_3) D:\Projects\proj_3>pip install comtypes
Collecting comtypes
  Downloading comtypes-1.1.2.zip (179kB)
    100% |################################| 184kB 569kB/s
Installing collected packages: comtypes
  Running setup.py install for comtypes ... done
Successfully installed comtypes-1.1.2

(proj_3) D:\Projects\proj_3>dir Lib\site-packages
 Volume in drive D has no label.
 Volume Serial Number is 0E52-CE22

 Directory of D:\Projects\proj_3\Lib\site-packages

20.05.2016  14:15    <DIR>          .
20.05.2016  14:15    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  268,620,853,248 bytes free

So I want to reuse existing system packages and install new package in my created virtualenv. But instead, although I'm in virtualenv, pip installed the package in my system libraries instead in local project.

(proj_3) D:\Projects\proj_3>where pip
C:\Python35\Scripts\pip.exe

(proj_3) D:\Projects\proj_3>python -m ensurepip
Ignoring indexes: https://pypi.python.org/simple
Requirement already satisfied (use --upgrade to upgrade): setuptools in c:\python35\lib\site-packages
Requirement already satisfied (use --upgrade to upgrade): pip in c:\python35\lib\site-packages

It looks like pip is not bootstraped in my local package because it already exists in my system packages. Unless I did something wrong, this is unexpected as using virtualenv with Python 2 does not behave like this.

Any ideas what might be wrong?

回答1:

It is a bug already reported here: https://bugs.python.org/issue24875

Before it is fixed, try this workaround.

First, create the virtual environment without --system-site-packages. Then modify the created environment.

pyvenv /path/to/env && pyvenv --system-site-packages /path/to/env