Pin-Hao
Pin-Hao

Reputation: 41

pip installed packages not found in virtual environment

I am working with venv in VS code on windows 10, and it kept throwing ModuleNotFoundError, even if I have already tried py -m pip install <package name>. The weirdest part was that pip freeze or pip list returns nothing.

If I navigate to the env/Lib/site-packages folder, I can see that the package folder is already there, so I am assuming the package has been installed.

I think it's worth noting that the above problem only happens after I activated the virtual environment, and pip freeze is still working normally when I am not in venv.

Some details: (can provide more if needed)

Any help provided would be greatly appreciated!

Upvotes: 4

Views: 12593

Answers (2)

Pushkar
Pushkar

Reputation: 140

The query is still unclear. Can you show the exact error msg thrown? i.e. message after ModuleNotFoundError.

pip is a package installer and it is present by default when you create a new virtual environment

if the error still persists, you can try this:

First reason to why pip freeze or pip list doesn't show anything is that you don't have any packages installed in your virtual environment. If it doesn't throw any other error messages, it means it working just fine and showing a null list. The packages may be installed outside the virtual env.

Second reason is that somehow your package installer pip is uninstalled from the virtual env. But in this case whenever you use any pip command it always throws an error something like 'pip' module not found.

To resolve this (second reason)

  • you can simply deactivate the virtual env and create a new one. python -m venv new_env_name. Then install all your libraries at once using the requirements.txt file.
  • Or you will have to download and install pip manually in your virtual env. Since I find it cumbersome, I prefer the previous workaround.

Upvotes: 1

ronpi
ronpi

Reputation: 490

It is possible that your pip command as in pip freeze is not the one bound to your venv. Similar to the install command, try py -m pip freeze and then check if the package is installed.

How do you run your python program? just using python scripty.py? Try to check which site-packages your python is using:

python -c "import site; print(site.getsitepackages()[0])"

Then check where your package is installed: py -m pip show <package-name>

It should provide info regarding the package, including its location. Check that this is the same location as shown in the previous command.

If there are not the same locations, you are using python and pip that are not binded to each other

Upvotes: 0

Related Questions