Can't find my PYTHONPATH - python

I'm trying to change my PYTHONPATH. I've tried to change it in "My Computer" etc, but it doesn't exist there. I searched in the registry in some places, and even ran a whole search for the word 'PYTHONPATH', but to no avail.
However, it Python I can easily see it exists. So where is it?

At runtime, you can change it with:
import sys
sys.path.append('...')
In My Computer, right-click Properties (or press Win-Break), System tab, Environment Variables, System. You can add it if it's not already there.
Finally, in the CMD prompt:
set PYTHONPATH C:\Python25\Lib;C:\MyPythonLib
Or in bash:
PYTHONPATH=/usr/share/python/lib:/home/me/python
export PYTHONPATH
Or, more succinctly:
export PYTHONPATH=/home/me/python

Python does some stuff up front when it is started, probably also setting that path in windows. Just set it and see, if it is changed in sys.path.
Setting environment variables in the Python docs say:
My Computer ‣ Properties ‣ Advanced ‣ Environment Variables

You can add it under "My Computer" if it doesn't exist. PYTHONPATH just adds to the default sys.path.
On unix/linux/osx you can:
$ export PYTHONPATH=/to/my/python/libs
You can also use .pth files to point to libraries:
http://docs.python.org/library/site.html#module-site
And of course:
import sys
sys.path.append('/path/to/libs/')
Also, check out virtualenv for managing libraries for multiple projects.

Here's how I solved it.
First, get the current path. There's a lot more there than I expected.
import sys
print ';'.join(sys.path)
Copy that result to the clipboard. Go to My Computer and create the new environment variable PYTHONPATH, and for the value paste from the clipboard. Modify as necessary.

MacOS 10.5.8, Python 2.6, Eclipse+Pydev 1.5.7
Python installation's site-package is, for example:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
create symlinks YOUR LIBRARY inside into site-package, for example:
cd /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
ln -s /path/to/YOUR/LIBRARY/ YOUR_LIBRARY_NAME
Now You can use in commandline: import YOUR_LIBRARY_NAME
run Eclipse with Pydev, go to Preferences->Pydev->Interpreter Python
remove Your Python interpreter record, if exists;
click New and add Python 2.6 interpreter path, for example:
/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6
notice, that Eclipse Pydev display Python System Library, accept that
in Library section click New Folder and write path to YOUR LIBRARY, for example:
/path/to/YOUR/LIBRARY/
click Apply - it is essential, because Eclipse Pydev built now his own "library map", when this operation finish - click [OK]
close Eclipse
run Eclipse again - now You should use in Pydev: import YOUR_LIBRARY_NAME

And, as with all good things in life, you can find it in the documentation:
http://docs.python.org/install/index.html#modifying-python-s-search-path

What's it set to? Have you tried creating a PYTHONPATH environment variable?

You need modify your environment variables. How to do this depends on which version of Windows you have.
If the PYTHONPATH variable doesn't exist, you have to create it. It might not exist if you haven't already created it.

I had same problem and oliver-zehentleitner's answer in github solved my problem.
He said: Maybe You install package with pip for python2 and run with python3, just try to install with pip3 or python3 -m pip install python-binance and then run your script again.
I hope this can solve yours too.

Related

VS Code error when importing Django module

I'm working on a web app and I use Django as framework.
I'm using VS Code on a macOS.
I get an error when I try to import some Django module. This is a screenshot of my code in error.
The error message is the following:
[pylint] E0401:Unable to import 'django.conf.urls'
I too was facing this error while working with Python virtual environments. In my case, it was happening because I have installed Django on my virtual environment and my base environment didn't contain any module named Django.
Base(Global) environment
and when I use the same command inside my virtual environment (myDjangoEnv)
Fix:
Now what I understood is that pylint uses the python interpreter to analyze our code and mark the error while we write the code.
Since I have three python environments in my system, hence three different python interpreters are available to my VS Code.
So if VS code uses the Python interpreter from the base environment, it will not recognize the Django module (import Error). Therefore, you are required to change the Python interpreter to the one present in your virtual environment.
It sounds complicated but it is pretty simple:
Click on the bottom left of the screen to change python interpreter.
Select from the list of available Python interpreters. Make sure you select the appropriate interpreter with respect to the current project.
Follow steps mentioned in the image. For details, or if that doesn't work, read further!!!
If you can't see your interpreter (installed in the virtual environment) listed in the drop-down list OR selecting interpreters listed don't rectify the error.
Then, you have to give the path of your interpreter (installed in venv) to vs code. Because you might have installed Django only in your venv. Happens when you don't use anaconda to create venv.
Steps to rectify-
To check the path, activate venv and type which python in terminal, this will give path. Copy the path.
Click interpreter on lower left, to pull drop-down, as shown in pic above.
Click enter the interpreter path.
Paste path copied.
This will assign the right interpreter and rectify the error.
Locate your project's virtual environment. In my case, I am working on a Django project and my virtual environment is located on the path below:
C:/Users/abc/Desktop/Virutal36/myLab/Scripts/python.exe
Copy the address of your virtual environment.
On VS Code, Select File > Preferences> Settings to open your User Settings (or use the Ctrl+, shortcut).
Create or modify an entry for python.pythonPath with the full path to your virtual environment and you will be good to go. In my case it is:
C:/Users/abc/Desktop/Virutal36/myLab01/Scripts/python.exe
https://code.visualstudio.com/docs/python/environments#_manually-specify-an-interpreter
Ctrl+Shift+P
Type Python:select interpreter
Now we will get:
Choose Enter interpreter path:
Select Find...
Then
Check correct django version is properly installed and active?
In the active environment, calling this code in python interpreter shouldn't have errors.
from django.conf.urls import url
Check the VS studio python environment
https://code.visualstudio.com/docs/python/environments
You need to select the right environment. So, go to view in tool bar, then select command pallet(ctrl+shift+p), then type "python:select interpreter", then select the right virtual environment where you start you project.
In my case I solved it using the Select Interpreter option from VS Code's Command Palette (Shift + Command + P).
I chose the Python interpreter option which corresponds with the folder in which my virtual environment was and it solved the issue immediately.
Hope it helps :)
Are you using a virtual environment (mkvirtualenv)? In that case you need to make sure you install django and pylint etc., within your virtual environment too, using the following commands.
workon [yourEnvName]
pip install pylint
pip install django
pip install djangorestframework
and so on...for all the modules you want to use.
enter image description here
Choosing a global environment helped me to recover this issue
First check the requirements that you install djangorestframework, second if you work on virtual envs maybe the pylint that you use not check in your virtual env so you can install in local the package.
For me, I opened the nested folder of my Django project. So, Django couldn't find the modules of venv.
Just mentioning, I thought it might be helpful to someone.

How can I make a package from my PyCharm project available to other PyCharm projects on my computer?

I am not sure if this is a Python or a PyCharm question. I think that it is quite simple, but I could not find a solution anywhere.
I have started to write a collection of modules with utility stuff. Now I want to import one of my packages in several of my other projects. What do I have to do to be able to simply write import my_module, so that Python finds the right one automatically?
I would like to set this up on a Mac and a PC, so advice on both platforms is appreciated.
It's not pyCharm issue. You have to give python information of the place your module live, to do that update PYTHONPATH, if you're using bash (OS X,GNU/Linux distro), add this to your ~/.bashrc or ~/.bash_profile
export PYTHONPATH="${PYTHONPATH}:/path/to/my_module"
Also you can checkout that article How do I set PYTHONPATH and SO How to use PYTHONPATH question to get more info.
To avoid duplicating answer adding a link to give info of how to update PYTHONPATH on Windows: How to add to the pythonpath in windows 7?
After you should be able to import your module.

Python, virtualenv: telling IDE to run the debug session in virtualenv

My project's running fine in virtualenv. Unfortunately, I can't yet run it via my IDE (Eric) because of the import troubles. It stands to reason, as I never told the IDE anything about virtualenv.
I know the drill ($ source project/bin/activate etc.), but lack general understanding. What constitutes "running inside virtualenv"? What IDE options might be relevant?
I think the only required setting to run or debug code is path to python interpreter.
Relevant IDE options could be SDK or Interpreter settings.
Note that you should run not default python (eg. /usr/bin/python) but python binary in your virtual environment (eg /path/to/virtualenv/bin/python)
Also there are some environment variables set by activate, but i think they aren't needed when you point to virtualenv python binary directly.
So, again, what activate does is only environment variables setup: at least, it modifies system $PATH in a way that python and pip commands points to executable files under path/to/virtaulenv/bin directiry.
As far as I know it is possible to run your script/project using your virtualenv simply by calling /path/to/your/venv/python your_script.py. To install new packages in your venv for example, you would run /path/to/your/venv/pip install some_package.
I guess the main advantage of "runnnig inside virtualenv" would be not being concerned about having to inform the location of python packages/executable everytime you want to run some script. But I lack general understanding too.
I usually install a virtualenv with the --no-site-packages option in order to have a "clean" installation of python.
--- EDIT ---
The second answer of this discussion has a nice explanation.
Simply look at the project/bin/activate, everything you need is there to set up the appropriate search.
Usually the most important path is the PYTHONPATH which should point to the site-packages.

Python 3, sys.path doesn't work, how to permenantly append a path? [duplicate]

Whenever I use sys.path.append, the new directory will be added. However, once I close python, the list will revert to the previous (default?) values. How do I permanently add a directory to PYTHONPATH?
If you're using bash (on a Mac or GNU/Linux distro), add this to your ~/.bashrc
export PYTHONPATH="${PYTHONPATH}:/my/other/path"
You need to add your new directory to the environment variable PYTHONPATH, separated by a colon from previous contents thereof. In any form of Unix, you can do that in a startup script appropriate to whatever shell you're using (.profile or whatever, depending on your favorite shell) with a command which, again, depends on the shell in question; in Windows, you can do it through the system GUI for the purpose.
superuser.com may be a better place to ask further, i.e. for more details if you need specifics about how to enrich an environment variable in your chosen platform and shell, since it's not really a programming question per se.
Instead of manipulating PYTHONPATH you can also create a path configuration file. First find out in which directory Python searches for this information:
python -m site --user-site
For some reason this doesn't seem to work in Python 2.7. There you can use:
python -c 'import site; site._script()' --user-site
Then create a .pth file in that directory containing the path you want to add (create the directory if it doesn't exist).
For example:
# find directory
SITEDIR=$(python -m site --user-site)
# create if it doesn't exist
mkdir -p "$SITEDIR"
# create new .pth file with our path
echo "$HOME/foo/bar" > "$SITEDIR/somelib.pth"
This works on Windows
On Windows, with Python 2.7 go to the Python setup folder.
Open Lib/site-packages.
Add an example.pth empty file to this folder.
Add the required path to the file, one per each line.
Then you'll be able to see all modules within those paths from your scripts.
In case anyone is still confused - if you are on a Mac, do the following:
Open up Terminal
Type open .bash_profile
In the text file that pops up, add this line at the end:
export PYTHONPATH=$PYTHONPATH:foo/bar
Save the file, restart the Terminal, and you're done
You could add the path via your pythonrc file, which defaults to ~/.pythonrc on linux. ie.
import sys
sys.path.append('/path/to/dir')
You could also set the PYTHONPATH environment variable, in a global rc file, such ~/.profile on mac or linux, or via Control Panel -> System -> Advanced tab -> Environment Variables on windows.
To give a bit more explanation, Python will automatically construct its search paths (as mentioned above and here) using the site.py script (typically located in sys.prefix + lib/python<version>/site-packages as well as lib/site-python). One can obtain the value of sys.prefix:
python -c 'import sys; print(sys.prefix)'
The site.py script then adds a number of directories, dependent upon the platform, such as /usr/{lib,share}/python<version>/dist-packages, /usr/local/lib/python<version>/dist-packages to the search path and also searches these paths for <package>.pth config files which contain specific additional search paths. For example easy-install maintains its collection of installed packages which are added to a system specific file e.g on Ubuntu it's /usr/local/lib/python2.7/dist-packages/easy-install.pth. On a typical system there are a bunch of these .pth files around which can explain some unexpected paths in sys.path:
python -c 'import sys; print(sys.path)'
So one can create a .pth file and put in any of these directories (including the sitedir as mentioned above). This seems to be the way most packages get added to the sys.path as opposed to using the PYTHONPATH.
Note: On OSX there's a special additional search path added by site.py for 'framework builds' (but seems to work for normal command line use of python): /Library/Python/<version>/site-packages (e.g. for Python2.7: /Library/Python/2.7/site-packages/) which is where 3rd party packages are supposed to be installed (see the README in that dir). So one can add a path configuration file in there containing additional search paths e.g. create a file called /Library/Python/2.7/site-packages/pip-usr-local.pth which contains /usr/local/lib/python2.7/site-packages/ and then the system python will add that search path.
On MacOS, Instead of giving path to a specific library. Giving full path to the root project folder in
~/.bash_profile
made my day, for example:
export PYTHONPATH="${PYTHONPATH}:/Users/<myuser>/project_root_folder_path"
after this do:
source ~/.bash_profile
On linux you can create a symbolic link from your package to a directory of the PYTHONPATH without having to deal with the environment variables. Something like:
ln -s /your/path /usr/lib/pymodules/python2.7/
For me it worked when I changed the .bash_profile file. Just changing .bashrc file worked only till I restarted the shell.
For python 2.7 it should look like:
export PYTHONPATH="$PYTHONPATH:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python"
at the end of the .bash_profile file.
Adding export PYTHONPATH="${PYTHONPATH}:/my/other/path" to the ~/.bashrc might not work if PYTHONPATH does not currently exist (because of the :).
export PYTHONPATH="/my/other/path1"
export PYTHONPATH="${PYTHONPATH}:/my/other/path2"
Adding the above to my ~/.bashrc did the trick for me on Ubuntu 16.04
This is an update to this thread which has some old answers.
For those using MAC-OS Catalina or some newer (>= 10.15), it was introduced a new Terminal named zsh (a substitute to the old bash).
I had some problems with the answers above due to this change, and I somewhat did a workaround by creating the file ~/.zshrc and pasting the file directory to the $PATH and $PYTHONPATH
So, first I did:
nano ~/.zshrc
When the editor opened I pasted the following content:
export PATH="${PATH}:/Users/caio.hc.oliveira/Library/Python/3.7/bin"
export PYTHONPATH="${PYTHONPATH}:/Users/caio.hc.oliveira/Library/Python/3.7/bin"
saved it, and restarted the terminal.
IMPORTANT: The path above is set to my computer's path, you would have to adapt it to your python.
The script below works on all platforms as it's pure Python. It makes use of the pathlib Path, documented here https://docs.python.org/3/library/pathlib.html, to make it work cross-platform. You run it once, restart the kernel and that's it. Inspired by https://medium.com/#arnaud.bertrand/modifying-python-s-search-path-with-pth-files-2a41a4143574. In order to run it it requires administrator privileges since you modify some system files.
from pathlib import Path
to_add=Path(path_of_directory_to_add)
from sys import path
if str(to_add) not in path:
minLen=999999
for index,directory in enumerate(path):
if 'site-packages' in directory and len(directory)<=minLen:
minLen=len(directory)
stpi=index
pathSitePckgs=Path(path[stpi])
with open(str(pathSitePckgs/'current_machine_paths.pth'),'w') as pth_file:
pth_file.write(str(to_add))
Just to add on awesomo's answer, you can also add that line into your ~/.bash_profile or ~/.profile
The add a new path to PYTHONPATH is doing in manually by:
adding the path to your ~/.bashrc profile, in terminal by:
vim ~/.bashrc
paste the following to your profile
export PYTHONPATH="${PYTHONPATH}:/User/johndoe/pythonModule"
then, make sure to source your bashrc profile when ever you run your code in terminal:
source ~/.bashrc
Hope this helps.
I added permanently in Windows Vista, Python 3.5
System > Control Panel > Advanced system settings > Advanced (tap) Environment Variables > System variables > (if you don't see PYTHONPATH in Variable column) (click) New > Variable name: PYTHONPATH > Variable value:
Please, write the directory in the Variable value. It is details of Blue Peppers' answer.
Fix Python Path issues when you switch from bash to zsh
I ran into Python Path problems when I switched to zsh from bash.
The solution was simple, but I failed to notice.
Pip was showing me, that the scripts blah blah or package blah blah is installed in ~/.local/bin which is not in path.
After reading some solutions to this question, I opened my .zshrc to find that the solution already existed.
I had to simply uncomment a line:
Take a look
I found a solution to do this in a anaconda environment here: https://datacomy.com/python/anaconda/add_folder_to_path/
Just:
conda develop /your_path
In Python 3.6.4 you can persist sys.path across python sessions like this:
import sys
import os
print(str(sys.path))
dir_path = os.path.dirname(os.path.realpath(__file__))
print(f"current working dir: {dir_path}")
root_dir = dir_path.replace("/util", '', 1)
print(f"root dir: {root_dir}")
sys.path.insert(0, root_dir)
print(str(sys.path))
I strongly suggest you use virtualenv and virtualenvwrapper otherwise you will clutter your path
Inspired by andrei-deusteanu answer, here is my version. This allows you to create a number of additional paths in your site-packages directory.
import os
# Add paths here. Then Run this block of code once and restart kernel. Paths should now be set.
paths_of_directories_to_add = [r'C:\GIT\project1', r'C:\GIT\project2', r'C:\GIT\project3']
# Find your site-packages directory
pathSitePckgs = os.path.join(os.path.dirname(os.__file__), 'site-packages')
# Write a .pth file in your site-packages directory
pthFile = os.path.join(pathSitePckgs,'current_machine_paths.pth')
with open(pthFile,'w') as pth_file:
pth_file.write('\n'.join(paths_of_directories_to_add))
print(pthFile)
After multiple bashing into wall. Finally resolved, in my CentOS 8 the pip3 was old, which was showing error to install the recent packages.
Now, I had downloaded the Python source package, which is Python-3.10.4 and installed the usual way, however the post-installation check generated errors in bash.
And I could not remove the existing Python, because that would break the CentOS desktop features.
Solution:
For building
./configure //don't not add --prefix=/usr, which you need to set proper care
make -j8
sudo make install
Now, as you have multiple Python installed, you can set alias python=python3
And for setting PYTHONPATH
export PYTHONPATH="/usr/local/bin/python3.10:/usr/local/lib/python3.10/lib-dynload:/usr/local/lib/python3.10/site-packages"
Don't add PYTHONHOME
For those who (like me) don't want to get too deeply involved in Python file management (which seems hopelessly overcomplicated), creating a .pth file works perfectly on my Windows 11 laptop (I'm using Visual Studio Code in Windows). So just go to the folder for your virtual environment site packages - there's mine:
Create a text file with a .pth extension - I called mine wheal.pth:
Add paths to it:
The best thing about this in VS Code is that import statements recognise this path (I had to exit VS Code and go back in), so now more typing # type: ignore to suppress linting warning messages!
on Mac :
user#terminal$ env PYTHONPATH=module_path python3
>>> import sys
>>> sys.path
['module_path', 'plus_other_python3_paths',...]
Shortest path between A <-> B is a straight line;
import sys
if not 'NEW_PATH' in sys.path:
sys.path += ['NEW_PATH']

How do you correctly set the PYTHONPATH variable on Windows?

Whenever I try to expirement with Python on Windows, I always run into a wall with the import statements. Python simply can't find anything on Windows -- every import, even for something as core as timezone fails.
I know this has something to do with the PYTHONPATH environment variable. In my case, Python is installed to "C:\Python27". My PYTHONPATH looks like this:
C:\Python27;C:\Python27\DLLs;C:\Python27\Lib
Still, nothing will import. I get errors like this:
File "D:\Code\Django\polls\models.py", line 3, in <module>
from django.utils import timezone
ImportError: cannot import name timezone
What's wrong with my situation?
Take a look at the official docs on using Python on Windows, in particular the section on finding modules.
You have to add the directory where you installed 3rd party modules to your PYTHONPATH if you didn't install them to your Python27\Libs\site-packages directory.
Another option is to get acquainted with pip and virtualenv. These tools make installing 3rd party modules a breeze. Although I don't know how well they are supported on Windows (I mainly do Python development on Linux).
PYTHONPATH = If this variable exists in your environment, Python will add it to the normal search path for modules when you use any import statement; you normally do not modify this as well behaved Python scripts will install themselves in the site-packages directory, and Python searches this by default.
PATH = this is the global file system path. Your operating system will search the directories listed in this variable (from left to right), to find commands when you type something at a command prompt.
In order for Python to work correctly only Windows, the C:\Python27 directory should be listed in PATH. If you ran the installer as an Administrator, the installer will modify the global PATH and add this for you. If you installed it as a normal user, you need to modify the PATH manually.
To add this manually, right click on My Computer and select Properties. Click on Advanced, then Environment Variables. You'll see two boxes - User Variables and System Variables. You can only edit user variables - system variables need administrative access.
Simply add a new variable (or modify the existing PATH) You should also add C:\Python27\Scripts to your PATH as most commands installed by Python scripts (like django-admin.py) are installed here. Directories are separated by ;
Once you have done this; python should work properly for you on Windows.
virtualenv is good option.
else simply add site-packages in path.

Categories