Module not found but it's already installed

  • I’m fairly new to Python. I use Python 3.11.8 (what MS Azure supports, my eventual production environment) on Windows 10.
  • I’m testing a program on Windows 10.
  • I have created and activated my virtual environment in the cmd.exe window.
  • My program is in c:\users\MYUSER\onedrive - CONAME\documents\pythonprojects\CONAME\CUSTNAME\buscard\buscard.py
  • This program worked last week just fine. The only thing that changed is at the top of the program, I changed the order of imports to make importing my custom utils (call it ggutils.py) last as I may have to change the sys.path right before I import ggutils. But the error happens before I change the sys.path.
  • Imports of standard modules work fine. Pandas has to be installed separately. That’s the one that fails.
  • I edit my program in VS Code but run it in cmd.exe with the command line: py -m pdb buscard.py input "Test 1"

Here is my code:

import os
import re
import sys
print("Sys.path is:",sys.path)
import glob  # To get list of files.
import inspect
from pathlib import Path
import pandas # NOT FOUND
import shutil
import xmltodict
sys.path.append(sys.path[0] + r"c:\users\MYUSER\Pycharmprojects\ggutil2024") # Required to use ggutil
from ggutil import *  # My Custom utils.

When I run my buscard.py program and I get to a line import pandas then I get an error “Module not found”. When I go to cmd.exe and run pip install pandas I get “Requirements already satisfied”.

I’m not sure what I’m doing wrong here. I ran the program the same way last week via cmd.exe and it worked fine.

Can someone help please? Thank you.

The python that pandas is installed for is not the same as the one you are running your code for.

Try using py -m pip install pandas and not pip.

Running pip --version will report which python it is installed for.
Compare that with py -m pip --version

You were right. My current pip is for Python 3.9, the old one I’m not using.

I don’t have a pip for python 3.11 which I’m using.

How do I install pip for my Python 3.11? Is that possible? I’m looking how to install pip myself for Python 3.11.

When I run py -m pip install pandas I get an error “C:\Users\chuck\AppData\Local\Programs\Python\Python311\python.exe: No module named pip”

When I open a cmd.exe window it runs a program to set up paths called addpath.bat. I’m changing that now to point to Python 3.11.

I’m also checking Windows user environment variables, and fixed those to point to Python 3.11.

Got pip installed. This works for Python 3.11: py -m ensurepip --upgrade

The command is now pip3 not pip. It was installed in C:\Users\MYUSER\AppData\Local\Programs\Python\Python311\Scripts so make sure your path points to there.

I will put updates below in this message:

  • Program seems to be working now. I will test my custom utils file now.

Thank you so much! I’m still new to this. :slight_smile:

FYI ensurepip is never required with a python.org installation.

Glad you have it working.

What is the path to the virtual environment?

Do you intend to use it to run this code?

Do you intend for Pandas to be installed in that virtual environment?

What result do you get from py -c "import sys; print(sys.executable)"?

What result do you get from where pip, pip --version and py -m pip version? It looks like you diagnosed this part of the puzzle already.

How did you install the 3.11 Python, exactly? Everything you get from the Python website should include Pip already.

Are you generally familiar with why this is done or would you be interested in an overview?

I downloaded a Windows installer file from Python.org. I always start with the official website for that.

I got it working. Thanks for your help.

Are you generally familiar with why this is done or would you be interested in an overview?

In Python 3.9 I used pip. But in Python 3.11 it was renamed to pip3. In a tutorial I saw that was done on a Mac he also used pip3 for Python 3.x. I’d be interested to know why.

There are two worlds of python commands Windows and the rest, macOS and Unix.

On Windows its been a matter of changing the PATH to point at the version of python, and its tools like pip, that you want to use. If you look in the Scripts folder you will see that python3.12, for example, has pip.exe, pip3.exe and pip3.12.exe.

However after the py command was added there is a more robust way. Use py to start python and py -m pip to start pip.

If you install more then one version of python on Windows you can run the exact version you want using py -3.10 and py -3.12. To run the matching pip you can use py -3.10 -m pip. This avoids the need to change the PATH.

On the rest, macOS and Unix, python installs with multiple names.
For example for python 3.12 there will be python3.12 and, often, python3 as a symlink to python3.12.
If you install multiple versions of python then you would have python3.10 etc.

The same naming is done for pip versions that match python versions.
pip3.12, pip3.10 and pip3 as a symlink to one of them.

Lastly why is pip called pip3? Becuase of python 2 already using the pip name.