After upgrading: "ModuleNotFoundError". How to fix?

Hello,

After upgrading Python from 3.12.0 to 3.13.1, and manually editing Windows’ PATH (otherwise, it still ran the previous version)… Python can no longer find a module that was available before:
ModuleNotFoundError: No module named 'ebooklib'

Re-installing the modules doesn’t help, since some modules don’t install (not yet compatible with 3.13.1?):

pip3 freeze --path c:\Users\joe\AppData\Local\Programs\Python\Python312\Lib\site-packages\ > python_requirements.txt
pip3 install -r python_requirements.txt

So I gave up, and edited Windows’ user PATH to go back… but, while “python --version” does say 3.12.0… one of the modules that used to work no longer loads :-/
"ModuleNotFoundError: No module named 'ebooklib'"

Provided 3.13.1 just is too early for some modules, how do I go back to the previous, working setup?

And generally speaking, for the next time, what’s the right way to upgrade Python so things don’t break?

Thank you.

Can you run where pip3 and tell us what folder it’s in?

Python.exe is installed in Python313 while pip.exe is installed in Python313\Scripts, so you may still be running the 3.12 pip if you didn’t update both in PATH.

It’s on Windows so “where” doesn’t work. Search shows that each install has its own pip3.exe:

Like I said, I had to edit Windows’ user PATH to use 3.13.1 since the installer didn’t do it — as a result, “python --version” still said 3.12.0.

But after editing PATH, while “python” did run 3.13.1… modules were no longer loading. I guess the installer doesn’t take care of downloading and installing all the modules that were part of the previous version.

Is there something else that must be edited besides Windows’ user PATH after running the upgrade so that Python uses/installs modules to match the new version?

Windows should have a where command built in (it does the same thing as which on Linux and macOS.) If you edited PATH so that python runs 3.13’s python.exe but didn’t updated PATH so that pip3 runs 3.13’s pip3.exe, then Python will report missing modules you thought you installed. (It’s a bit confusing that python.exe and pip3.exe are in different folders.)

Either way, you can also run pip from python by running python.exe -m pip install foo as well.

2 Likes

Indeed, something is wrong: Even though PATH is correct, running a Python script says 3.13.1 instead, which explains why the module doesn’t load… since the installer doesn’t install them when upgrading (which would be a nice touch).

I’ll see if I can find the source of the problem (Windows? Python? Other?) and if it can be solved without closing everything and rebooting.

C:\temp>where pip3.exe
C:\Users\fred\AppData\Local\Programs\Python\Python312\Scripts\pip3.exe

C:\temp>set | grep "^Path"
Path=…;C:\Users\fred\AppData\Local\Programs\Python\Python312\Scripts\;C:\Users\fred\AppData\Local\Programs\Python\Python312\;…

C:\temp>type test.python.pip.module.py

import platform
print(platform.python_version())

import ebooklib

C:\temp>test.python.pip.module.py
3.13.1
Traceback (most recent call last):
  File "C:\temp\test.python.pip.module.py", line 5, in <module>
    import ebooklib
ModuleNotFoundError: No module named 'ebooklib'

Running the script with just the filename, using:

C:\temp>test.python.pip.module.py

might be using the py launcher, which will pick the latest version of python that it can find by default.

My guess is that you have install using one python version and are running using another. You may well have p[ython 3.13 being the default when running a script. Run py -0 to find out the default (its the entry with a *).

Try using explicit python versions with the py.exe command for pip and your
script to avoid mismatched pip and python versions.

py -3.12 -m pip install -r python_requirements.txt
py -3.12  test.python.pip.module.py
1 Like

Yup:

C:\temp>python -V
Python 3.12.0

C:\temp>python test.python.pip.module.py
3.12.0

I used Windows’ Apps section to remove Python 3.13.1… which also takes cares of fixing the Py launcher, so I can simple run “blah.py” again and have it run Python 3.12.

Lessons learned:

  1. The Python installer doesn’t take care of installing modules, so run the required commands before (pip3 freeze + pip3 install)
  2. Some modules might not be compatible, so won’t work with the latest release of Python

Thanks all.