ImportError: No module named ‘encodings’ in Python

ImportError: No module named ‘encodings’ in Python is a very popular error in Python, the error occurs because of a faulty installation of Python and for some people due to virtual environment variables.

In this blog post I will try to present to you in detail how I solved this error and I will also present to you other possible solution that I discovered along the way and that worked for other people.

Explaining : ImportError: No module named ‘encodings’ in Python

The error occurred when I updated my Linux system, now it seems that i can not use python, even after reinstalling and installing different versions of Python, Python does not work.

Bellow is an example of the error :

                                                                       #
 python
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
Aborted
                                                                       #

After hours of trial and error, I found a solution that solved my problem, bellow I present the solution and other solutions that worked for a lot of developers.

Solution 1 : remove then reset the virtual environment files

In Python 3, after a lot of attempts I finally solved the issue by removing the virtual environment files. And then resetting them up afterwards.

Let us run these lines of codes, Step by Step

                                                                       #
rm -rf venv
                                                                       #

After that, let us run this line

                                                                       #
virtualenv -p /usr/bin/python3 venv/
                                                                       #

Followed by this line

                                                                       #
source venv/bin/activate
                                                                       #

Finally, let us run pip install as seen bellow

                                                                       #
pip install -r requirements.txt
                                                                       #

Now, we have removed the virtual environment files. And successfully reset them. In my case everything works amazing.

If this is not enough and did not solve the issue then please do not give up, we have other solutions that worked for other developers/

If you find this article useful, consider supporting the website by donating to our Kofi account above.

Solution 2 : re-install Python and use the custom python installation

On windows 10, the error bellow appears :

                                                                       #
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
                                                                       #

The solution to the issue is very simple, first start by removing your current Python installation

After you finish, re-install Python again. But Please choose custom installation and check Add Python to environment variables in the Advanced Options.

This method also works for Windows 7 users.

Solution 3 : use dpkg-reconfigure

For some Ubuntu users, In order to solve the error you have to lose your session and reconnect again after running the code bellow :

                                                                       #
sudo dpkg-reconfigure python3
                                                                       #

There is no guarantee this will work for all Linux systems, here it works for Ubuntu

Solution 4 : change the permissions of the anaconda folder

For a small number of developers, the solution that worked was changing the permissions of the anaconda folder, by using the code bellow

                                                                       #
sudo chmod -R u=rwx,g=rx,o=rx /path/to/anaconda 
                                                                       #

Ihope this works for you friends.

Summing-up

Thank you for reading, we are at the end of this article, I did my best to help you solve this error with this guide and included the solution that worked for me and other solutions that I found relevant. Thanks, keep coding and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/