ModuleNotFoundError: No module named 'encodings' in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# ModuleNotFoundError: No module named 'encodings' in Python

The Python "ModuleNotFoundError: No module named 'encodings'" occurs for multiple reasons:

  1. Having a glitched virtual environment.
  2. Not having Python in your system's PATH environment variable.
  3. Having a corrupted Python installation.
shell
Fatal Python error: Py_Initialize: Unable to get the locale encoding ModuleNotFoundError: No module named 'encodings'

The first thing you should try is to recreate your virtual environment if you have one.

shell
# ๐Ÿ‘‡๏ธ Optionally update your requirements.txt file pip freeze > requirements.txt # ๐Ÿ‘‡๏ธ Deactivate virtual environment deactivate # ๐Ÿ‘‡๏ธ Remove the old virtual environment folder: macOS and Linux rm -rf venv # ๐Ÿ‘‡๏ธ Remove the old virtual environment folder: Windows rd /s /q "venv" # ๐Ÿ‘‡๏ธ specify correct Python version when creating virtual environment python -m venv venv # ๐Ÿ‘‡๏ธ Activate on Unix or MacOS source venv/bin/activate # ๐Ÿ‘‡๏ธ Activate on Windows (cmd.exe) venv\Scripts\activate.bat # ๐Ÿ‘‡๏ธ Activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # ๐Ÿ‘‡๏ธ Install the modules in your requirements.txt file (optional) pip install -r requirements.txt

If the python -m venv venv command doesn't work, try the following 2 commands:

  • python3 -m venv venv
  • py -m venv venv

Make sure to use the correct activation command depending on your operating system and your shell.

Try running your script with the new virtual environment active.

If the error is not resolved, you might have a corrupted Python installation or Python might not be set up correctly in your system's PATH environment variable.

# Setting Python in your system's PATH environment variable

Here is an example of how to correctly set up Python on Windows and add Python to the PATH environment variable in a straightforward way.

Download the installer from the official python.org website.

If you have Python already installed, start the installer and click on "Modify".

click modify

You can leave the optional features ticked.

optional features

On the "Advanced Options" screen, make sure to tick the "Add Python to environment variables" option.

add python to environment variables

Once the "Add Python to environment variables" checkbox is checked, click "Install".

Now your Python installation should be set up correctly and Python should be added to your system's PATH environment variable.

If that didn't work, your Python installation might be corrupted.

Start the installer again and click on "Uninstall".

uninstall python

Now that you don't have Python installed on your machine, start the installer again and make sure to tick the "Add python.exe to PATH" option.

add python exe to path

The checkbox won't be checked by default.

Once the "Add python.exe to PATH" checkbox is checked, click on "Install Now".

After the installation, Python will be installed and configured properly.

If you are on Linux or macOS, check if you have the PYTOHNHOME environment variable set to a wrong value.

shell
echo $PYTOHNHOME echo $PYTHONPATH

check if pythonhome environment variable is set

If the environment variables are incorrectly set, you can try to remove them and restart your machine.

shell
unset PYTOHNHOME

unset pythonhome environment variable

If that doesn't help, try reinstalling Python from the official python.org website.

# Conclusion

To solve the "ModuleNotFoundError: No module named 'encodings'" error, make sure:

  1. Your virtual environment is not glitched by recreating it.
  2. Python is set up correctly in your system's PATH environment variable.
  3. Your Python installation is not corrupted.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev