Can't execute python3 on terminal (PYTHONPATH and PATH complications) - python-3.x

I installed the library ase. Since then I can't execute python3 on terminal neither run its python files. I found out with the command which python3 command that with python3 is located at '/usr/local/bin/python3'. The libraries, including ase, are located at '/usr/local/lib/python3.9/site-packages' apparently.
See:
pip3 install --upgrade git+https://gitlab.com/ase/ase.git#master
WARNING: The scripts ase, ase-build, ase-db, ase-gui, ase-info and ase-run are installed in '/usr/local/Cellar/python#3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
This is the error I got when I try to run python3 on terminal:
Fatal Python error: init_sys_streams: can't initialize sys standard streams
Python runtime state: core initialized
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/ase/io/__init__.py", line 1, in <module>
ModuleNotFoundError: No module named 'ase'
I think that something has to be wrong at my .bash_profile file however, I'm not getting the error. Here is my edited .bash_profile:
export PYTHONPATH=/usr/local/lib/python3.9/site-packages/ase:$PYTHONPATH
export PATH=/usr/local/Cellar/python#3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/bin:$PATH
Hope you can help me, thank you!
Update:
I changed my PYTHONPATH from:
export PYTHONPATH=/usr/local/lib/python3.9/site-packages/ase:$PYTHONPATH
to:
export PYTHONPATH=/usr/local/lib/python3.9/site-packages:$PYTHONPATH
Now I can run python3 on terminal again, however I try to execute ase directly on the terminal it seems that the command is not found.
-bash: ase: command not found
I have already checked out the requires (matplotlib, scipy, numpy) and they are all installed. Seems that everything is wrong with the PATH. I don't know what could be.
The binaries files related to ase package are not anymore into this directory "/usr/local/Cellar/python#3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/bin". I don't know where they are since the installation was successfully done.
According to https://unix.stackexchange.com/questions/240037/why-did-pip-install-a-package-into-local-bin I redefined my paths to:
export PYTHONPATH=/usr/local/lib/python3.9/site-packages:$PYTHONPATH
export PATH=/usr/local/bin:$PATH
and seems to be working, I already ran a program containing that package, however when I enter the folder '/usr/local/bin' I don't find those binary files.
If you can help me again I will be grateful.

Related

Cannot install using pip3 [duplicate]

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 !

Problems with python3 and virtualenvwrapper on Mac Big Sur

I am following [these][1] instructions on setting up a macOS virtual environment. I have followed all the instructions but when I get to the last part where I have to reload the startup file by making the following call in the terminal:
source ~/.bash_profile
I get the error message:
/Applications/Xcode.app/Contents/Developer/usr/bin/python3: Error while finding module specification for 'virtualenvwrapper.hook_loader' (ModuleNotFoundError: No module named 'virtualenvwrapper')
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 and that PATH is
set properly.
I have tried different solutions from the internet but I am unable to get a fix.
Running which python3 and which virtualenvwrapper.sh returns:
/opt/anaconda3/bin/python3
/usr/local/bin/virtualenvwrapper.sh
My initial thoughts are that this may be due to the fact that I am running both anaconda python and regular python on the same computer.
[1]: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/development_environment

Adding a path linking docplex to python3

I am installing Cplex python API into my MacBook. So I followed the tutorial given in YouTube (https://www.youtube.com/watch?v=oINxanpv_2M) and have installed the script & library for Cplex using the Terminal instructions:
cd /Users/my_name/IBM_CPLEX/CPLEX_Studio1210/python
python3 setup.py install
this part works alright with everything installed, just a note from the Terminal at the beginning saying 'python wrapper is not found'.
Then I tested an existing Cplex problem using
cd IBM_CPLEX/CPLEX_Studio1210/python/examples/mp/modeling
python3 production.py
, which returned:
Model: production
- number of variables: 6
- binary=0, integer=0, continuous=6
- number of constraints: 5
- linear=5
- parameters: defaults
Traceback (most recent call last):
File "production.py", line 101, in <module>
if model.solve():
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/docplex/mp/model.py", line 4152, in solve
return self.fatal("CPLEX runtime not found: please install CPLEX or solve this model on DOcplexcloud")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/docplex/mp/model.py", line 884, in fatal
self._error_handler.fatal(msg, args)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/docplex/mp/error_handler.py", line 210, in fatal
raise DOcplexException(resolved_message)
docplex.mp.utils.DOcplexException: CPLEX runtime not found: please install CPLEX or solve this model on DOcplexcloud
So my initial guess is that my python 3 has been installed in another directory under the Library folder, and a path will be needed to link the Cplex engine to the python solver (if correct). Inside the YouTube tutorial, a PATH is actually provided, so in my case it's written as:
PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}"
export PATH
export PYTHONPATH=/Users/my_name/IBM_CPLEX/CPLEX_Studio1210/cplex/python/3.7/x86-64_osx
I have actually installed Cplex python API into a Windows PC previously. The path-adding step was done by visiting:
Control Panel -> System and Security -> System -> Advanced system settings -> Environment Variables. Visit (https://www.youtube.com/watch?v=hqGZzRh00y0) for specific further steps.
However, I tried quite a lot of ways adding this path to my Mac, and so far I've used those to write the above path but none worked:
nano .bash_profile.pysave
nano .bash_profile
sudo nano /etc/paths
That's where I got stuck. Any help and comment would be highly appreciated. I am new to Mac Terminal. Anaconda is also installed in my Mac if that's helpful.
Sorry for the massive details - was trying to put down everything in case there was something unnoticed that I have done wrong. Thanks.
Added:
A small issue in the process above was that my Python was actually 3.8. So after adding the path export PYTHONPATH=/Users/my_name/IBM_CPLEX/CPLEX_Studio1210/cplex/python/3.7/x86-64_osx and running a cplex example, a message popped up inside the Terminal:
Exception: CPLEX 12.10.0.0 is not compatible with this version of Python.
Need to download an older 3.7 version then did the $ python3.7 setup.py install process again after changing my directory to IBM_CPLEX/CPLEX_Studio1210/python.
Some similar issues about Python version have been discussed in the post: CPLEX and Python 3.7.

Packages install but not found

I have packages installed under /usr/local/lib and I added that in my PATH as well, but then I try to import it in any of my python scripts I get an error saying module not found.
-bash-4.2$ pip2 list | grep pytest
pytest-mock 2.0.0
My PATH:
echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/bin
ERROR:
-bash-4.2$ python2
>>> import pytest
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pytest
Only if the packages is installed under my /users/user-name/.local/bin folder, it is reflected else it is not.
My usecase is to use this machine as a slave for my Jenkins setup. I tried injecting this PATH directly to the job during build process as well. Didn't work for me.
I have been stuck on this for quite some while, any help on this is greatly appreciated.
First thing, it's generally a good idea to use virtualenv to create Python environments - installing Python packages system-wide is asking for trouble.
Second, your path may not work because you are setting PATH in the way that Jenkins ignores. The simplest solution is to provide full path to file: /usr/local/bin/pytest.
The safest way is to combine two above - create virtualenv, install pytest in it and rovide full path when using (note: you don't need to activate virtualenv to use it).

Unable to import sqlite3 using Anaconda Python

I am trying to do the following in Python 3.7.1 on Windows
import sqlite3
but I get the following error message
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "c:\programdata\anaconda3\lib\sqlite3\__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "c:\programdata\anaconda3\lib\sqlite3\dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: DLL load failed: The specified module could not be found.
I have searched for a solution to the problem for quite a while now to no avail. I have also successfully run pip install pysqlite3 on the Anaconda prompt, but the import still fails. What do?
I got this working on windows by downloading: the sqlite3 dll (find your system version)
And placing it into the folder: C:\Users\YOURUSER\Anaconda3\DLLs
(Depending on how you installed Anaconda, this may have to be placed into
the following folder: C:\ProgramData\Anaconda3\DLLs)
According to #alireza-taghdisian, you can locate the exact path of
your conda environments (where you need to copy the sqlite3 dll) by typing:
conda info --envs on your anaconda prompt.
Locate the sqlite3.dll file. In my case it was in following folder
C:\Users\Admin\anaconda3\Library\bin
where C:\Users\Admin\anaconda3 is the folder where Anaconda was installed
Add this to PATH in environment variables, and it should work then.
Try copying the sqlite3.dll from the
C:\Users\YOURUSER\anaconda3\Library\bin
folder to
C:\Users\YOURUSER\Anaconda3\DLLs
Please check https://github.com/jupyter/notebook/issues/4332
I added anaconda root/Library/bin to my PATH and now it works!
Add CONDA_DLL_SEARCH_MODIFICATION_ENABLE=1 to your environment variables.
before executing the program, enter conda activate in your shell.
I had tried all above solutions But for me and my system I got to know that
I downloaded Python in C:\Python27 hence there is dll folder in python C:\Python27\DLLs
I installed Sqlite3.dll in my above dll folder
May be this solution will help you because it completely depends on where do you install your python
Happy coding :)
I put the sqlite3.dll in the path folder of my Python venv and still wont work. I suspected it is a path problem.
(In my case: E:\Virtual_Env\mini_zinc\env\Scripts)
I found in my case I messed up installation in a virtual evn, somehow using an anaconda python kernel within a Python venv.
I reinstall the Python Venv and check the python version after installed Env is correct (not the Anaconda python), then proceed with Jupyter Notebook (or Juyterlab) and works fine.
I was able to resolve this issue by putting sqlite3.dll file in the C:\Users<USERID>\AppData\Local\conda\conda\envs<ENV NAME>\DLLs.
Download sqlite3.dll file from https://www.sqlite.org/download.html
or copy it from C:\ProgramData\Anaconda3\DLLs\
I found the #elgsantos useful. But for those who are new to Python and Conda like me, I would like to add a little bit of details.
1- I use miniconda3 for creating new environment.
2- interestingly, I got two installation path on my computer for conda: the first one (the obvious) is located on "C:\Users\taghdisian\miniconda3". The second one is on "C:\Users\taghdisian\AppData\Local\r-miniconda". The latter is the primary path that you need to copy your sqlite3 files into the envs folder. I copy them in the "C:\Users\taghdisian\AppData\Local\r-miniconda\envs\sdr3.9\DLLs" in which the sdr3.9 is one of my virtual Condo environment.
you can locate the exact path of your conda environments (where you need to copy sqlite3) by typing the conda info --envs on your anaconda prompt.
I hope this help.
got the same error while loading the jupyter notebook from other conda prompt than "base" environment.
[1]: https://i.stack.imgur.com/2DW7E.png
Resolved by installing sqlite package
(nlpenv) C:\Users\arunk>conda install sqlite
launching
*
(nlpenv) C:\Users\arunk>jupyter notebook

Resources