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?
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
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.
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".
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:
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
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.
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.
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.
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.
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
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
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
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.
In my case just changing the permissions of anaconda folder worked:
sudo chmod -R u=rwx,g=rx,o=rx /path/to/anaconda
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.
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.
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!
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.
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.
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.
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).
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
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 !
I have 2 python tools that I have to run via the windows cmd line. One is written in python2.7 while the other requires python3.6.
I have installed the newest Anaconda python3.7 version and created two new environments in 'C:\ProgramData\Anaconda3\envs' called 'python27' and 'python36'. For some reason I had to manually install numpy and scipy using conda install -n env_name numpy scipy for each of the new environments.
The reason I have to run both tools using the windows cmd line is that I have integrated them into a workflow environment (RCE by the DLR in case this is relevant), which executes integrated tools in this way. Which means I cannot simply use the Anaconda Prompt instead.
I cannot simply add the python installation to the PATH environment variable because of each tool requiring a different python version (and the file being called 'python.exe' in all versions), so I tried to create aliases for the cmd prompt as suggested by "roryhewitt" in this thread Aliases in Windows command prompt.
my 'python27.bat' file:
#echo off
echo.
C:\ProgramData\Anaconda3\envs\python27\python.exe %*
The problem with this approach is that python encounters an error when trying to import numpy:
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\ProgramData\Anaconda3\envs\python27\lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import core
File "C:\ProgramData\Anaconda3\envs\python27\lib\site-packages\numpy\core\__init__.py", line 71, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
(removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
1. Check that you are using the Python you expect (you're using C:\ProgramData\Anaconda3\envs\python27\python.exe),
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy versions you're trying to use.
2. If (1) looks fine, you can open a new issue at
https://github.com/numpy/numpy/issues. Please include details on:
- how you installed Python
- how you installed numpy
- your operating system
- whether or not you have multiple versions of Python installed
- if you built from source, your compiler versions and ideally a build log
Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.
Original error was: DLL load failed: The specified module could not be found.
Does anyone know a better way to run python scripts with a specific python environment via the windows cmd line, or what is causing the import error when I use the alias?
TLDR: I have 2 python tools that require python2.7 and python3.6 respectively. I have to run these tools using the windows cmd line and using aliases to the 'python.exe' file in the specific anaconda environments results in an import error of numpy. Is there a better way to handle two python environments via the cmd line or an easy fix for the import error?
It seems that the ImportError when using python2.7 via the alias in the cmd line was due to the anaconda environment not being activated. This caused the module to be unable to load properly.
I have added the conda functionality to be used in the cmd line as instructed in the post by "Simba" here: Conda command is not recognized on Windows 10. This has fixed the numpy ImportError.
I can now execute the desired tools from the cmd line using the correct python version by activating the correct anaconda environment first, and then calling the alias
Example:
C:\Users\user>conda activate python27
(python27) C:\Users\user>python27 main.py
Happy October everyone,
I've successfully downloaded modules before using either the pycharm installer or pip through the command screen, but for some reason when installing matplotlib pycharm cannot recognize it. I've uninstalled and reinstalled, I've installed through both methods, I've followed past similar questions asked on this site which make sure that you have the same interpreter and that it was installed in the right folder (pycharm error while importing, even though it works in the terminal).
So, here's the whole problem. Here's is the simple code, submitted into both pycharm and IDLE:
import matplotlib.pyplot as plt
plt.plot([1,2,3],[2,1,3])
plt.show()
When submitted into IDLE, my plot appears. When submitted into pycharm, the following error appears:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/PythonProject/matplotlib.py", line 1, in <module>
import matplotlib.pyplot as plt
File "C:\PythonProject\matplotlib.py", line 1, in <module>
import matplotlib.pyplot as plt
ImportError: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package
I am currently running Python 3.4, PyCharm 2016.2.3, and my matplotlib folders are indeed in my site-packages folder inside my Python34 folder. Also for further verification:
PyCharm installation
Please help I've become frustrated since this is the only module I've run into trouble with. I've scoured StackOverflow and related websites to help, I've made sure I have all the requirements, etc.
I guess if you named your current writing python module as matplotlib.py.That cause the python load your current writing module instead of the actual matplotlib.py, which triggers an error.
I recommend you to use virtualenv. Is not strictly necessary but is good for dividing your project environments.
This is how I tested matplotlib on my Windows 10 installation, hope it helps.
Be sure that you have the python 3 installation folder listed in your Windows PATH environment variable, should be already listed if you checked "Add Python 3.5 to PATH":
You need also to set the Scripts folder in your PATH environment variable usually should be this path:
C:\Users\<your username>\AppData\Local\Programs\Python\Python35\Scripts
If you don't do that you have to prepend python -m to every command below like this: python -m <command>, so the command below would be python -m pip install virtualenv. I prefer the first solution.
To test matplotlib on Pycharm I've used virtualenv, here is how; first install virtualenv:
pip install virtualenv
Then you create your virtual environment in a folder of your choice, in my case I used python_3_env_00:
virtualenv python_3_env_00
After that you can activate you python 3 virtual environment:
python_3_env_00/Scripts/activate.bat
Now you should see in your command line the active virtual environment (python_3_venv_00), like this:
Now you can install matplotlib:
pip install matplotlib
Fire up PyCharm and add your virtual environment as you project interpreter, go to File->Settings search for Project Interpreter click on the gear icon and Add Local and set the path of your virtual environment, should look like something like this:
Test it:
import sys
print(sys.path)
run this code in where the import worked, and run it in the Pycharm project. Compare the lists. Find out which path that is not present in Pycharm sys.path.
Before importing pyplot, append the missing path to sys.path.
import sys
sys.path.append("the path")
import matplotlib.pyplot as plt
Does this work?
Please follow below steps if you are still getting an error:
If you are using PyCharm, it automatically create virtualenv.
Ensure Scripts path is set into PATH
C:\Users\<Username>\AppData\Local\Programs\Python\Python37-32
Then open PyCharm and go to File-> settings. Search for Project Interpreter. You will see window like this
sample image
Click on setting icon -> Existing Environment -> click on ... give below path
C:\Users\Krunal\AppData\Local\Programs\Python\Python37-32\python.exe
Click on Apply -> ok and you are good to go.
After installing matplotlib When I was trying to use matplotlib.pyplot it was giving error module not found.
I browsed some white papers and found out that we also need to install scipy library to use the matplotlib so I used the below in my command prompt
python -mpip install scipy
Restarted my kernel session.
It worked!!!
I was also facing issue while importing matplotlib but it got resolved and now I am able to use it from pycharm as well.
Please make sure you should have visual c++ 14 installed in your system.
2.If you have more than two python version installed on your system then please install matplotlib from both the version.
Eg. pip install matplotlib
pip3 install matplotlib
If matplotlib is working from python idle then please check whether you are using correct interpreter in pycharm or not and try to choose pythonw.exe path from your installed location.
Hope this will help, Please do let me know if you are still facing issue.
I had similar issue but I solved it very easily on pycharm 2019.3.2. In case anyone looking for an easier solution:
I just opened the terminal window on pycharm and typed pip install matplotlib and it was all good to go. Every project has its own virtual environment. Opening terminal window of IDE cds to project directory by default. So the installing command was enough.
I want to use Gtk with python under windows. I already have Anaconda installed on windows. In order not to mess up everything and to have some easiness uninstalling/reinstalling, I would like to have a virtual env created with conda, working with that Gtk installation. But I don't seems to be able to make it work.
Here is my process. I first create a raw Ananconda virtual env with
conda create -n gtk-exporter python
The virtual environment is located at C:\Anaconda3\envs\gtk-exporter.
I then download the latest windows installer for PyGObject at http://sourceforge.net/projects/pygobjectwin32/files/?source=navbar
I extract and execute the installer and tell it to use a portable python install at C:\Anaconda3\envs\gtk-exporter. I only select Base, GTK and Glade for installation. The installation finished in a second and says it's successful.
Then within windows' shell, I activate the new environment with activate gtk-exporter. However when I try to import gtk, it fails, not finding gi.repository.
>>> from gi.repository import Gtk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'gi'
What's wrong here ? C:\Anaconda3\envs\gtk-exporter\Lib\site-package contains a folder gnome with a lot of stuff including *.dlls, *.exe's and unix-looking folders like etc, lib or share, but I don't see a init.py or something pythonic. Am I missing a step.
Thank you for your help !
The problem was that I used python 3.5, whereas it is not supported. The installer should not have allowed me to install with python 3.5. I filed a bug report to signal it.
I solved the problem by uninstalling python 3.5 and installing python 3.4.
I just tried to use pd.HDFStore in IPython Notebook with a Python 3 kernel (Anaconda 2&3 on Ubuntu 14.04)
import pandas as pd
store = pd.HDFStore('/home/Jian/Downloads/test.h5')
but it throws the following error
ImportError: HDFStore requires PyTables, "libhdf5.so.9: cannot open shared object file: No such file or directory" problem importing
I initially thought it's because pytables is somehow missing, but when I check $source activate py34 and $conda list, pytables 3.2.0 is already installed under anaconda python3 environment.
Also, if I switch to Python 2, for example, $source activate py27 and start ipython notebook, it works properly and no import error is thrown.
I guess that I must miss something for configuring pytables under anaconda python 3 env, but I cannot figure it out. Any help is highly appreciated.
Update:
I just tried on a fresh install of Anaconda3-2.3.0-Linux-x86_64 from official website and it ends up with the same error. When I try $locate libhdf5.so.9 in command line, nothing shows up.
This is a known issue that we are working on. When it is fixed, conda update --all will update the libraries and fix the issue.