Virtual Environments in Python 3.7.9. What am I missing?

2.6k Views Asked by At

My old computer died. Here's my chance to start fresh using virtual environments.

I started with a fresh install of python 2.7.18 and 3.7.9 on a new Windows 10 computer. (It doesn’t matter if I install python 3.7.9 for all users or just one account. I’ve tried both with the same results.)

I want to set up venv for python 3. (I tried with venv and virtualenv with the same results.)

I go to my project directory and type:

py -3.7 -m pip list

>Package    Version
>---------- -------
>pip        20.2.3  
>setuptools 47.1.0

I create a virtual environment for my project.

py -3.7 -m venv venv

and activate the virtual environment.

venv\Scripts\activate.bat

The prompt changes to show I’m using the virtual environment.

py -3.7 -m pip list

>Package    Version
>---------- -------
>pip        20.2.3  
>setuptools 47.1.0

Now I install a daily build of wxPython to the virtual environment.

py -3.7 -m pip install "d:\((username))\Downloads\Python\python3.7-64\wxPython-4.1.1a1.dev4959+47c4a913-cp37-cp37m-win_amd64.whl"

This successfully installs wxPython. The prompt shows I’m still in virtual environment. I confirm the installation.

py -3.7 -m pip list

>Package    Version
>---------- ------------------------
>numpy      1.19.2   
>Pillow     7.2.0   
>pip        20.2.3   
>setuptools 47.1.0   
>six        1.15.0   
>wxPython   4.1.1a1.dev4959+47c4a913  

This is just what I expect. I now deactivate the virtual environment.

deactivate

The prompt changes, so I am no longer using the virtual environment. But

py -3.7 -m pip list

>Package    Version
>---------- ------------------------
>numpy      1.19.2  
>Pillow     7.2.0  
>pip        20.2.3  
>setuptools 47.1.0  
>six        1.15.0  
>wxPython   4.1.1a1.dev4959+47c4a913  

That's not what I expected. I thought wxPython (along with numpy, Pillow, and six) would no longer show up. Looking at where files are added, I see that the pip changes are made to C:\Users((username))\AppData\Local\Programs\Python\Python37\Lib\site-packages, not to D:((username))\Documents\Programming\Python((projectname))\venv\Lib\site-packages, which is what I expected.

And when I start Python 3.7.9 again, I still have all the packages installed without loading my virtual environment. So the whole virtual environment concept is totally ignored.

Can anybody help me understand what I've done wrong, and more importantly, what I need to do differently?

Thanks

3

There are 3 best solutions below

1
On BEST ANSWER

Thanks for the replies. It turns out that py launcher and virtual environments are incompatible. Here's what I've done.

I've set up batch files so that I can select which python (2.7 or 3.7) I'm using. The batch files set the Windows PATH to point to the correct Python installation. (I haven't had to set any other environment variables yet, but will edit this if I find I need to.)

I then use "python" rather than "py -3.7" when using python 3.7 to set up and to access my virtual environments.

So when I'm in my virtual environment,

python -m pip list

>numpy      1.19.2  
>Pillow     7.2.0  
>pip        20.1.1  
>setuptools 47.1.0  
>six        1.15.0  
>wxPython   4.1.1a1.dev4959+47c4a913  

gives me different results than

py -3.7 -m pip list

>pip        20.1.1   
>setuptools 47.1.0

So hello setting paths, goodbye py launcher. Now virtual environments work like I expected.

1
On

since you have more than one python in your system pip is mislead. There are two options that you can follow:

  1. Hard way: You can change the environment variables to set which one you want to use and remove other paths for python. Therefore unless you specify the location of python your packages will be okay. You should do the same for your pip also. It is much easier to break things when you just rely on environment variables.

  2. Easy way: Remove all your python versions and download anaconda. It manages environments easily and smoothly while you are enjoying your coding.

4 months ago I had the same issue and here is the question and answer: Python and pip versions are different and potentially causing problem

Try this one and let me know if the issue continues.

3
On

py -3.7 will always refer to the global python installation and ignore your virtualenv

while you're inside your virtualenv you should use python -m pip instead