158

I recently reinstalled ubuntu and did upgrade to 16.04 and cannot use python:

$ python manage.py runserver
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

At this point, python itself doesn't work

$ 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

Even this suggestion is no longer working:

unset PYTHONHOME
unset PYTHONPATH

Every every I fix it one way, it comes back again. Several answers help to fix it temporarily, but not for good. I have reinstalled python and python3 several times. What can I do from here?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
codyc4321
  • 9,014
  • 22
  • 92
  • 165
  • Is this related to Django or just Python? You may want to edit your question with the appropriate tag. – Moses Koledoye Jun 30 '16 at 20:40
  • 2
    just python, i happened to be using django – codyc4321 Jun 30 '16 at 20:44
  • It seems like that the python installation is incomplete. Have you installed python via apt or have you compiled and installed it from source? Run `which python` to check which python binary you have first in $PATH. – HAL Jun 30 '16 at 21:27
  • Have you tried uninstalling and reinstalled both python2 and python3 via `apt`? –  Aug 12 '16 at 03:25
  • I ran into this using the Bitnami Django stack when trying to pip3 install -r requirments.txt int a virtual env. It worked fine without using the virtual env. I haven't taken time to figure out why this was an issue. – Brad Rhoads Mar 07 '18 at 21:42
  • 1
    This is not a `venv` nor a `django` problem, it is a general python problem. – Timo Sep 12 '18 at 05:29
  • **MAC OSX ANACONDA** I have faced this issue recently and took me a lot of posts to figure out the right solution to fix the problem. And a Lot of them are temporary hacks. To fix this once and for all, I wrote a brief solution to this [here](https://stackoverflow.com/a/56803287/9087851). – Pradeep Vasamsetty Jun 28 '19 at 08:20

19 Answers19

119

For Python-3 try removing virtual environment files. And resetting it up.

rm -rf venv
virtualenv -p /usr/bin/python3 venv/
source venv/bin/activate
pip install -r requirements.txt

https://wiki.ubuntu.com/XenialXerus/ReleaseNotes#Python_3 edit fo

Community
  • 1
  • 1
Anoop Malav
  • 1,397
  • 1
  • 10
  • 6
  • 2
    Great! Thanks for teaching me how to install python3 into an env using the `virtualenv` command! For some reason, it took me about 2 months to find it. Amazing ... – MadPhysicist Apr 16 '17 at 01:14
  • 4
    Hi I tried doing this on my Amazon AMI device(based on RHEL) but it doesn't work. Any other way it can be solved? – Divij Sehgal Apr 21 '17 at 09:24
  • 1
    when I entered the last one command it gives me below error can you please solve... Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt' – Deepak Chawla Aug 19 '17 at 16:19
  • 1
    @DeepakChawla requirements.txt should be present in the pwd from where you are issuing this command and it has nothing to do with creating enviornment, its a way to install packages in batch mode. – Amar Nov 01 '17 at 13:41
  • it's basically default version for python3 in Ubuntu 16.04 is 3.5.2 which is different in earlier Ubuntu versions. So if you have created virtual environments with different python3 versions, you will face this issue and this solution is correct. – Savitoj Cheema Apr 09 '18 at 12:34
  • I wonder if there some imp libraries in virtual env and delete solution is indeed the workaround? – CKM Jun 06 '18 at 06:08
  • 1
    Yes! `source venv/bin/activate` – Jay Modi Mar 26 '19 at 18:15
  • Why is this a solution instead of activating the venv again? Or using the python in the venv folder `./venv/bin/python3`? I'm asking why this solves it rather than just activating the venv? – Jerinaw Jan 15 '20 at 19:11
  • I am getting the 'encodings' error after cloning and activating an Anaconda environment, so virtualenv isn't involved here. How to fix it in that case? I don't have a requirements.txt file. – aviator Mar 10 '21 at 22:08
  • This error is coming in real environment rather than virtual environments. How to fix in real environment? – Dr. Essen Sep 29 '21 at 20:12
26

For Windows10 User.

I was using python3.4 on Windows10. I installed python3.5. I couldn't find PYTHONPATH, PYTHONHOME env variable. If I command python in CMD console, It kept using python3.4. I deleted python3.4. Whenever I command python in CMD console, it starts showing an error like below.

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

I searched to figure out my problem. Solution was simple. When you install python3.5, you can custom install and check Add Python to environment variables in Advanced Options.

I just leave here for case that someone have similar issues visit here so that they don't waste their precious time much to figure out.

Community
  • 1
  • 1
Jayground
  • 1,797
  • 1
  • 17
  • 32
  • 1
    Just to add my experience to this - if you've previously installed a different version of Python and made an env then uninstall Python, go to where it was installed and delete it, then go and remove any references to python from the path, then go to C:\Users\%USERNAME%\ and delete .virtualenvs. THEN install the version of Python you want to use, adding it to path – Kelly Bang Sep 11 '20 at 19:45
13

For the same issue on Windows7

You will see an error like this if your environment variables/ system variables are incorrectly set:

Fatal Python error: Py_Initialize: unable to load the file system codec
ImportError: No module named 'encodings'

Current thread 0x00001db4 (most recent call first):

Fixing this is really simple:

  1. When you download Python3.x version, and run the .exe file, it gives you an option to customize where in your system you want to install Python. For example, I chose this location: C:\Program Files\Python36

  2. Then open system properties and go to "Advanced" tab (Or you can simply do this: Go to Start > Search for "environment variables" > Click on "Edit the system environment variables".) Under the "Advanced" tab, look for "Environment Variables" and click it. Another window with name "Environment Variables" will pop up.

  3. Now make sure your user variables have the correct Python path listed in "Path Variable". In my example here, you should see C:\Program Files\Python36. If you do not find it there, add it, by selecting Path Variable field and clicking Edit.

  4. Last step is to double-check PYTHONHOME and PYTHONPATH fields under System Variables in the same window. You should see the same path as described above. If not add it there too.

Then click OK and go back to CMD terminal, and try checking for python. The issue should now be resolved. It worked for me.

dotpy_novice
  • 335
  • 2
  • 4
  • 16
  • This is exactly the same on win10. In my case it was just the PYTHONHOME and PYTHONPATH being set to an older install. – Andrés Marafioti Nov 11 '17 at 18:35
  • 2
    @dotpy_novice Could you please highlight the step 4 in detail? I had no any path listed in my user variables and I added as you said in step 3. Now I am a bit hesitant to proceed. There are no **PYTHONHOME** and **PYTHONPATH** in System variables. Should I create them myself, if yes, what should I insert for _Variable name_ and _Variable value_? – bit_scientist May 08 '18 at 07:21
13

I was facing the same problem under Windows7. The error message looks like that:

Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

Current thread 0x000011f4 (most recent call first):

I have installed python 2.7(uninstalled now), and I checked "Add Python to environment variables in Advanced Options" while installing python 3.6. It comes out that the Environment Variable "PYTHONHOME" and "PYTHONPATH" is still python2.7.

Finally I solved it by modify "PYTHONHOME" to python3.6 install path and remove variable "PYTHONPATH".

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Li Qiao
  • 131
  • 1
  • 3
9

I had this error during migration to Ubuntu 17.10, and this solved the problem :

sudo dpkg-reconfigure python3

Maybe you will have to close your session and reconnect.

numahell
  • 131
  • 1
  • 2
  • 1
    I have 16.04, this did not solve my issue. I am just trying to understand, why did this happen – Savitoj Cheema Apr 09 '18 at 12:28
  • Same problem on ubuntu 16.4.. Should I reinstall Ubuntu? `/usr/sbin/dpkg-reconfigure: python3 ist kaputt oder nicht komplett installiert` – Timo Sep 10 '18 at 20:02
  • On `18.04` `Lubuntu`, it says `Fatal Python error: Py_Initialize: Unable to get the locale encoding` – Timo Sep 14 '18 at 07:15
5

I had deleted rm -r /usr/lib/python*

dpkg -S '/usr/lib/python3*' | grep encodings

And then found I needed libpython3.10-minimal

sudo apt-get install --reinstall libpython3.10-minimal

I finally ran

sudo apt-get install --reinstall $(dpkg -S '/usr/lib/python3*' | cut -d ':' -f1 | cut -d ',' -f1 | sort | uniq | tr '\n' ' ')
sudo apt-get install --reinstall $(dpkg -S '/usr/lib/python2*' | cut -d ':' -f1 | cut -d ',' -f1 | sort | uniq | tr '\n' ' ')

To re-install all the packages I deleted files for

William Desportes
  • 1,412
  • 1
  • 22
  • 31
2

Look at /lib/python3.5 and you will see broken links to python libraries. Recreate it to working directory.

Next error -

./script/bin/pip3
Failed to import the site module
Traceback (most recent call last):
  File "/home/script/script/lib/python3.5/site.py", line 703, in <module>
    main()
  File "/home/script/script/lib/python3.5/site.py", line 683, in main
    paths_in_sys = addsitepackages(paths_in_sys)
  File "/home/script/script/lib/python3.5/site.py", line 282, in addsitepackages
    addsitedir(sitedir, known_paths)
  File "/home/script/script/lib/python3.5/site.py", line 204, in addsitedir
    addpackage(sitedir, name, known_paths)
  File "/home/script/script/lib/python3.5/site.py", line 173, in addpackage
    exec(line)
  File "<string>", line 1, in <module>
  File "/home/script/script/lib/python3.5/types.py", line 166, in <module>
    import functools as _functools
  File "/home/script/script/lib/python3.5/functools.py", line 23, in <module>
    from weakref import WeakKeyDictionary
  File "/home/script/script/lib/python3.5/weakref.py", line 12, in <module>
    from _weakref import (
ImportError: cannot import name '_remove_dead_weakref'

fixed like this - https://askubuntu.com/questions/907035/importerror-cannot-import-name-remove-dead-weakref

cd my-virtualenv-directory
virtualenv . --system-site-packages
IvanTheFirst
  • 101
  • 1
  • 2
2

I tried everything, deleted PYTHONPATH and PYTHONHOME - the error didn't go away. I had overlooked that I had specified a python-home path under WSGIDaemonProcess in the wsgi configuration of a VirtualHost. I removed that and the error was gone.

Stephan
  • 31
  • 2
1

Had the same problem when updating my mac to macOS Catalina, while using pipenv. Pipenv creates and manages a virtualenv for you, so the earlier suggestion from @Anoop-Malav is the same, just using pipenv to remove the virtual environment based on the current dir and reset it:

pipenv --rm
pipenv shell  # recreate a virtual env with your current Pipfile
qix
  • 7,228
  • 1
  • 55
  • 65
  • This worked for me but i cannot still understand why. Kindly explain the source of the error thanks. – Laban Sep 16 '20 at 05:25
  • This also worked for me. The likely reason is: I probably created a virtualenv using pipenv in a previous Ubuntu instalation. – Nicolay77 Aug 23 '22 at 13:55
1

I was facing this issue "ModuleNotFoundError: No module named 'encodings" after updating to macOS Catalina.

I was having multiple versions of Python installed in my system.

Removing all the python versions(2.7 and 3.7.4) from macOS system and reinstalling the latest python 3.8 worked for me.

To remove a python from macOS, I've followed the instructions from here How to uninstall Python 2.7 on a Mac OS X 10.6.4?

The above link is for python 2.7 and but you can use the same for 3.7 also.

Kondal Kolipaka
  • 3,471
  • 22
  • 28
1

In my case just changing the permissions of anaconda folder worked:

sudo chmod -R u=rwx,g=rx,o=rx /path/to/anaconda   
Apogentus
  • 6,371
  • 6
  • 32
  • 33
1

I got this error when try to launch MySql Workbench 8.0 on my macOS Catalina 10.15.3.

I solved this issue by installing Python 3.7 on my system.

I guess in future, when Workbench will have version greater than 8, it will require newer version of Python. Just look at the library path in the error and you will find required version.

0

I had a similar issue. I had both anaconda and python installed on my computer and my python dependencies were from the Anaconda directory. When I uninstalled Anaconda, this error started popping. I added PYTHONPATH but it still didn't go. I checked with python -version and go to know that it was still taking the anaconda path. I had to manually delete Anaconda3 directory and after that python started taking dependencies from PYTHONPATH.
Issue Solved!

Satyam Gaba
  • 23
  • 2
  • 8
0

Because this is the first result in google I just want to add the following information for anybody else having problems with jails:

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
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f079b16d740 (most recent call first):
Aborted (core dumped)

When attempting to import python into your jail you both need to link the dependencies and /usr/lib/pythonX.Y to [JAIL]/usr/lib/. Hope this helps.

Matt
  • 501
  • 3
  • 13
  • Dear @matt can u please provide the steps to 'jail' pythonx.y? – Pranav May 14 '20 at 05:14
  • @Pranav I'm using this script: https://gist.github.com/williamhaley/5a499cd7c83aa0e01eaf you can then run copy_binary pythonX.Y then find the libraries under your real root /usr/lib/pythonX.Y where X.Y is your python version (For example python3.7). Copy them to your jails /usr/lib/ and you're set. Cheers – Matt May 17 '20 at 05:20
  • Thanks, that helped me! Directory /jail/usr/lib needed files from /usr/lib/pythonX/ – paskl Aug 14 '22 at 21:09
0

I was facing same issue on a Debian server and for me the problem was that I've put my project inside the /root/ folder for which mod_wsgi didn't have the rights to access. So I simply moved my project into /var/www/html/ and update my VirtualHost configuration.

Benbb96
  • 1,973
  • 1
  • 12
  • 21
0

In my case, what worked was the following: I changed the env path in the conf from /home/vinzee/.pyenv/versions/envname/bin/ to /home/vinzee/.pyenv/versions/envname/ and it worked.

It seems the environment path needs to be the path to the root folder of the virtual environment (and not the path to ROOT_VENV/bin/).

You can additionally try without the trailing slash as others have suggested, but in my case, it worked both with and without it.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
0

It seems this error can happen for a large variety of reasons (given the number of different answers on here).

For me, the issue was related to the fact that my app was deployed in a virtualenv, and the pyvenv.cfg file had the wrong permissions. It must be writable by the user uwsgi is running as (which in my case was http).

Ben Davis
  • 13,112
  • 10
  • 50
  • 65
-2

Just go to File -> Settings -> select Project Interpreter under Project tab -> click on the small gear icon -> Add -> System Interpreter -> select the python version you want in the drop down menu

this seemed to work for me

Balu B
  • 13
  • 1
  • 2
-14

I could also fix this. PYTHONPATH and PYTHONHOME were in cause.

run this in a terminal

   touch ~/.bash_profile
   open ~/.bash_profile

and then delete all useless parts of this file, and save. I do not know how recommended it is to do that !