Categories: Python

No module named ‘encodings’ Stumps Python N00b.

I tried running a program on my desktop recently and got this error:

$ python myscript.py
Fatal Python error: Failed to import encodings module
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x0000b500 (most recent call first):
  <no Python frame>

I had realized that I was accumulating Python installations, so I uninstalled the older versions from my machine. I still had a valid 3.13 installation, so this error was confusing to me. I ensured there were no other versions on my machine.

$ python --version
Python 3.13.3

$ py -0
 -V:3.13 *        Python 3.13 (64-bit)

$ py -0p
 -V:3.13 *        C:\Python313\python.exe

I decided to uninstall 3.14 and install 3.14 since I’m in cleanup mode anyway. This gave me an opportunity to review the active python releases and their end of life dates on the Download Python | Python.org page. I found it interesting that the installer itself is being retired (here’s the More info link).

Once installation of 3.14 was done, I was surprised to find that the problem was still there!

$ python --version
Python 3.14.6

$ python myscript.py
Fatal Python error: Failed to import encodings module
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007edc (most recent call first):
  <no Python frame>

$ which python
/c/Python314/python

$ /c/Python314/python myscript.py
Fatal Python error: Failed to import encodings module
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f60 (most recent call first):
  <no Python frame>

I consulted my AI assistant and its next recommendation was to check for corrupted environment variables! Look at this culprit!

$ echo $PYTHONHOME
C:\Python311\

All I needed to do to this fix was to clear the PYTHONHOME environment variable.

$ export PYTHONHOME=

I also deleted it from the “User variables for saint” section of the Environment Variables dialog box.