4

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)

  • windows 10
  • VS code 1.59.0
  • python 3.9.6 64-bit

Any help provided would be greatly appreciated!

2
  • I am having exactly the same issue! Commented Jan 8, 2022 at 7:38
  • I've tried many ways to solve it. However one of my computer working perfectly fine despite of another. Commented Jun 10, 2022 at 9:26

2 Answers 2

1

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.
Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

1. py -m pip freeze is the same. Returns nothing. 2. yes, I am running as python scripty.py. That line of code returns of path of my venv folder. py -m pip show <pkg> returns the following error: WARNING: Package(s) not found: <pkg>
1. You need to replace <pkg> woth the real package name, did you fo that? 2. Are you sure the installation is completed successfully? 3. Why do you use py for installation instead of python which you use to run your script?
yes, I did replace <pkg> with pyqt5, which is what I was trying to install, I am just trying to be generic here. 2. If by installation is completed successfully means having them showed up in the env/Lib/site-packages folder, and terminal giving me the output Installed succesfully, then yes. 3. I can run my script with either python or py, its the same. For installation, I used py as I am following the official documentation seen here.
Edit to my previous comment, I have to use python to run my script, but not py. I created a new folder on my desktop, and moved the venv folder to it, and pip freeze is working as expected now. Weird that pip freeze is not working at my original directory (where it used to work), but I can run my script without problem with python script.py.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.