What exactly should be installed in PYTHONPATH?

I am viewing and recording a setup document for other developers while working on a python project, and I am reading the PYTHONPATH environment variable. I am looking at my current development system and I think that I have a few wrong things that are causing my IDE (IntelliJ) to behave incorrectly when searching for python libraries.

I looked at the documentation here and here , and I'm still not sure what should be in the PYTHONPATH environment variable.

I have PYTHONHOME pointed to `C: \ Python27 '.

My current PYTHONPATH set to PYTHONHOME . Should I also add directories from sys.path ?

UPDATE:

Based on the information below, PYTHONPATH need not be installed if there are non-standard libraries that you want python to find by default. For example, when I install wxPython from the installer, it will add its libraries to PYTHONPATH . I install PYTHONHOME in the python root installation to add it to my PATH environment variable so that I can run python from anywhere.

+59
python
Oct 21 2018-11-11T00:
source share
3 answers

You do not need to install any of them. PYTHONPATH can be configured to specify additional directories with private libraries in them. If PYTHONHOME is not installed, Python by default uses the directory in which python.exe is found, so dir should be in PATH.

+38
Oct 21 '11 at 14:31
source share

For most installations, you should not set these variables since they are not needed to run Python. Python knows where to find the standard library.

The only reason to install PYTHONPATH is to maintain Python user library directories that you do not want to install in the default global location (i.e. in the site-packages directory).

Be sure to read: http://docs.python.org/using/cmdline.html#environment-variables

+20
Oct 21 '11 at 3:21 a.m.
source share

Here's what I found out: PYTHONPATH is the directory to add to the Python import search path "sys.path", which consists of the current directory. CWD, PYTHONPATH, a standard and shared library, as well as a client library. For example:

 % python3 -c "import sys;print(sys.path)" ['', '/home/username/Documents/DjangoTutorial/mySite', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages'] 

where the first path '' denotes the current directory, the 2nd path through

 %export PYTHONPATH=/home/username/Documents/DjangoTutorial/mySite 

which can be added to ~ / .bashrc to make it permanent, and the rest are standard Python and a dynamic shared library , as well as third-party libraries like django.

As said, donโ€™t mess with PYTHONHOME, even if you set it to '' or 'None', the python3 shell will stop working:

 % export PYTHONHOME='' % python3 Fatal Python error: Py_Initialize: Unable to get the locale encoding ModuleNotFoundError: No module named 'encodings' Current thread 0x00007f18a44ff740 (most recent call first): Aborted (core dumped) 

Note that if you run the Python script, CWD will be the script directory. For example:

 username@bud:~/Documents/DjangoTutorial% python3 mySite/manage.py runserver ==== Printing sys.path ==== /home/username/Documents/DjangoTutorial/mySite # CWD is where manage.py resides /usr/lib/python3.6 /usr/lib/python3.6/lib-dynload /usr/local/lib/python3.6/dist-packages /usr/lib/python3/dist-packages 

You can also add the path to sys.path at runtime: Suppose you have a Fibonacci.py file in the ~ / Documents / Python directory:

 username@bud:~/Documents/DjangoTutorial% python3 >>> sys.path.append("/home/username/Documents") >>> print(sys.path) ['', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages', '/home/username/Documents'] >>> from Python import Fibonacci as fibo 

or through

 % PYTHONPATH=/home/username/Documents:$PYTHONPATH % python3 >>> print(sys.path) ['', '/home/username/Documents', '/home/username/Documents/DjangoTutorial/mySite', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages'] >>> from Python import Fibonacci as fibo 
0
07 Sep '19 at 22:53 on
source share



All Articles