Why does venv Python interpreter say "no module named __" when pip freeze & reinstalling indicates the module is installed?

2

I'm using venv for Python programming for the first time; so I'm assuming my difficulties are coming from that.

I've followed these instructions to create my virtual environment; which seemed to work flawlessly.

Next I followed along with this article but quickly ran into trouble. The command "pip install __" works; but curiously PowerShell seems to hang up after listing "Successfully installed...", pressing enter and spamming keystrokes does nothing; I have to close the PowerShell window. Waiting doesn't seem to do anything either.

After that happened; I figured I broke something so I tried to reinstall the module; in this case matplotlib first, and nmap second, just because nmap was the one I wanted to actually use.

Both returned, after hanging up on the "successful" install, some variation of "Requirement already satisfied: [details about install path here]"

My matplotlib output, for example:

Requirement already satisfied: matplotlib in c:\users\admin\environments\python1\lib\site-packages (3.1.1)
Requirement already satisfied: kiwisolver>=1.0.1 in c:\users\admin\environments\python1\lib\site-packages (from matplotlib) (1.1.0)
Requirement already satisfied: numpy>=1.11 in c:\users\admin\environments\python1\lib\site-packages (from matplotlib) (1.17.2)
Requirement already satisfied: cycler>=0.10 in c:\users\admin\environments\python1\lib\site-packages (from matplotlib) (0.10.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in c:\users\admin\environments\python1\lib\site-packages (from matplotlib) (2.4.2)
Requirement already satisfied: python-dateutil>=2.1 in c:\users\admin\environments\python1\lib\site-packages (from matplotlib) (2.8.0)
Requirement already satisfied: setuptools in c:\users\admin\environments\python1\lib\site-packages (from kiwisolver>=1.0.1->matplotlib) (40.8.0)
Requirement already satisfied: six in c:\users\admin\environments\python1\lib\site-packages (from cycler>=0.10->matplotlib) (1.12.0)

Which also hung up, curiously. It does seem to indicate the package was successfully installed though.

"pip freeze" ALSO supports the idea that these modules ARE installed right; and in my venv no less:

(python1) PS C:\Users\Admin\Environments> pip freeze
cycler==0.10.0
kiwisolver==1.1.0
matplotlib==3.1.1
nmap==0.0.1
numpy==1.17.2
pyparsing==2.4.2
python-dateutil==2.8.0
six==1.12.0

So again, I've never used a venv to program before - I only need to now to use an nmap module and play with some simple networking scripting; but from what I understand using virtual environments to code is the industry standard way of doing things; and thus I want to get the process right.

I'm running Python 3.7.4; and Python HAS been successfully added to PATH, confirmed by typing 'python' in command prompt - though from what I understand it shouldn't matter as each venv for a project is isolated and standalone.

Terribly appreciative of any kind of illumination anyone can provide.

Spooky

Posted 2019-09-16T22:07:21.507

Reputation: 73

Answers

0

Note that the following answer is relatively generic. Please feel free to follow up with specific questions, clarifications, etc.

Why does my venv Python interpreter say "no module named __" when pip freeze & reinstalling indicates the module is installed?

Just like Python interpreters, there can be multiple versions of pip installed on a system (each one associated with a different interpreter). What this error typically indicates is that you have used a version of pip to install a module that isn't associated with the current version of the interpreter you are using.


Another explanation is that Python has been installed into C:\Programs Files, C:\Program Files(x86) or into another special Windows folder and that is causing issues with finding modules.


I would also add that Powershell may not be the best way to use Python, broadly speaking. It seems to potentially introduce issues that may not exist otherwise.

"pip freeze" ALSO supports the idea that these modules ARE installed right; and in my venv no less[.]

Then you should be able to e.g. import nmap while your virtual environment is activated:

(python1) PS C:\Users\Admin\Environments> python
>>> import nmap
>>>

If you do not see the (python1) i.e. just PS C:\Users\Admin\Environments>, then you are not using the python interpreter associated with your virtual environment and will likely get a "No module named __" error.

It's probably also worth mentioning that Python virtual environments are only associated with a single terminal instance (cmd, powershell, etc.) per activation.

I figured I broke something so I tried to reinstall the module; in this case matplotlib first, and nmap second, just because nmap was the one I wanted to actually use.

Two small points here:

  • Current versions of matplotlib seem to have issues on Windows with Python 3.7.4, at least in some cases (particularly, crashing the Python interpreter).

  • nmap does not appear to have matplotlib as a prerequisite (as far as I am aware).

Anaksunaman

Posted 2019-09-16T22:07:21.507

Reputation: 9 278

Thank you for your insight. Would you have any suggested areas of reading or search terms for learning more about how pip may not be linked to the python interpreter I'm trying to use? Edit: As far as the Python install; I did a clean uninstall/reinstall to eliminate that issue, and checked that my Windows PATH is right -- so I don't think at least that that's the issue. – Spooky – 2019-09-17T23:34:36.123

Unfortunately, I don't know of any links off-hand. But where python and where pip can return any paths currently available globally. Furthermore, pip is always located in the Scripts folder of a given Python installation. So C:\PythonA\python.exe and C:\PythonA\Scripts\pip.exe would be associated and C:\PythonA\python.exe could use modules installed with C:\PythonA\Scripts\pip.exe.C:\PythonA\python.exeandC:\PythonB\pip.exewould not be associated andC:\PythonA\python.execould not use modules installed withC:\PythonB\pip.exe`. – Anaksunaman – 2019-09-18T00:00:03.390

More generally, Python virtual environments work by temporarily overriding any existing path variables in a terminal session. So if a module is installed with pip into path A and path A is overridden or unavailable, then the module won't be able to be found. – Anaksunaman – 2019-09-18T00:04:41.720

As a quick suggestion, I would try creating a new virtual environment, activating it, installing nmap in the exact same terminal window, then run python in that same window ,then run import nmap. If that doesn't work, then there may be other issues at play. – Anaksunaman – 2019-09-18T00:11:35.167

Thank you @Anaksunaman, I'll try this as soon as I have time! – Spooky – 2019-09-19T18:52:09.260