Friday, September 2, 2022

[SOLVED] "sudo pip list" and "pip list" inside virtual environment gives a different result

Issue

I can't figure this out. Maybe one of you guys in the community can.

I've been testing the Python virtual environments, specifically with the inheritance of the installed site packages when a virtual environment is created.

So my global python is:

$ sudo python -V 
Python 3.9.5

Same when omitting the sudo command and running the version command as a normal user:

$ python -V
Python 3.9.5

After knowing this, I created a test virtual environment. Just like this:

$ python -m venv test_venv --system-site-packages

Next, the activation and the sanity check:

$ source test_env/bin/activation
$ (test_venv) [me@pc 11:43 me]$ python -V
Python 3.9.5

What I would have expected inside the new environment is that the same packages used by the global python installation would be listed, but this doesn't seem to be the case. For starters, there are more inside the venv when compared with the global pip list.

$ sudo pip list | wc -l
17

$  pip list | wc -l
19

$(test_venv) [me@pc 12:17 me]$ pip list | wc -l
80

And there is a different version used compared to the global installation.

venv     :      zeroconf        0.28.8
sudo pip :      zeroconf        0.28.5
user pip :      zeroconf        0.28.5

And there are other packages which are not visible in the pip list like:

toml       0.10.2
sip        4.19.25

Where are these packages coming from?


Solution

I've found the answer to my own question.

It turns out the packages only need to be present inside the site packages folder to get included inside the virtual environment. It has nothing to do with the installed packages present under "pip list".

But when the virtual environment was created the installer also looks at your local site package folder and takes the newest packages.

/usr/lib/<Your current python version>/site-packages

~/.local/lib/<Your current Python version>/site-packages

So, I had a newer version of zeroconf inside my local site-package folder and in the global folder all sort of packages which where never installed by pip.



Answered By - Whois_me
Answer Checked By - David Marino (WPSolving Volunteer)