You don't set PYTHONPATH, you add entries to sys.path. It's a list of directories that should be searched for Python packages, so you can just append your directories to that list.
import sys
sys.path.append('/path/to/whatever')
In fact, sys.path is initialized by splitting the value of PYTHONPATH on the path separator character (: on Linux-like systems, ; on Windows).
You can also add directories using site.addsitedir, and that method will also take into account .pth files existing within the directories you pass. (That would not be the case with directories you specify in PYTHONPATH.)
You don't set PYTHONPATH, you add entries to sys.path. It's a list of directories that should be searched for Python packages, so you can just append your directories to that list.
import sys
sys.path.append('/path/to/whatever')
In fact, sys.path is initialized by splitting the value of PYTHONPATH on the path separator character (: on Linux-like systems, ; on Windows).
You can also add directories using site.addsitedir, and that method will also take into account .pth files existing within the directories you pass. (That would not be the case with directories you specify in PYTHONPATH.)
You can get and set environment variables via os.environ:
import os
user_home = os.environ["HOME"]
os.environ["PYTHONPATH"] = "..."
But since your interpreter is already running, this will have no effect. You're better off using
import sys
sys.path.append("...")
which is the array that your PYTHONPATH will be transformed into on interpreter startup.
Setting PYTHONPATH
Need to set PYTHONPATH in order to make modules get detected.
Setting the correct PYTHONPATH
python - What exactly should be set in PYTHONPATH? - Stack Overflow
What is Pythonpath used for?
How do I create a Pythonpath variable?
Where is Pythonpath located?
Hello, I built and packaged python from source code for my distro (cause the maintainers still don't have the latest version). The built and install was succeed but I need to set PYTHONPATH in order to make some modules get detected. The PYTHONPATH is "/usr/lib/python3.11/:/usr/lib64/python3.11/:/usr/lib/python3.11/site-packages/" . How the PYTHONPATH isn't configured / setted automatically ? And is that normal ? thanks.
The site module documentation and Modifying Python's Search Path seem to be what you're looking for.
As far as I understand it, those entries are being added to sys.path by:
/usr/lib/python2.6/site.py/usr/lib/python2.6/dist-packages/site.py
(Change 2.6 to your version of Python.)
The easiest way to change it is to add a file /usr/local/lib/python2.6/dist-packages/site-packages.pth containing ../site-packages.
Alternatively, maybe you can teach the package to use site.getsitepackages()?
I'd like to summarize my findings about python's path modification. There are two ways to do it.
- .pth file
PYTHONPATH
Any .pth file which is found on the default path (see bellow) will get its content included into sys.path.
Format of said .pth file is simple: one (folder) path per line. Surprisingly, the paths can be absolute or relative to the .pth file.
Default path is where the interpreter resides and <some-prefix>/lib/python<version>/site-packages where <some-prefix> is usually /usr/.
PYTHONPATH is environmental variable of your operating system. On unix systems you list them by env. Global modification of such variables is done through .sh scripts inside /etc/profile.d/ folder as mentioned by @TestUser16418.
Try appending to PYTHONPATH instead of overwriting it completely.
export PYTHONPATH=$PYTHONPATH:/home/dev/python-files
References:
According to the the Python documentation on PYTHONPATH
Augment the default search path for module files. [...]
The default search path is installation dependent, but generally begins with
prefix/lib/pythonversion(seePYTHONHOMEabove). It is always appended toPYTHONPATH.
meaning that some values exist in PYTHONPATH and the default search path is also only appended.
Additionally, this blog post (Archive.org link) also explains clearly why you need to append to PYTHONPATH and not overwrite it. Scrolling down to the section - Special cases and examining the search path explains it clearly (unfortunately no relative URL to that link so you'll have to scroll). Although the user gives the examples on a mac they are very much relevant to any platform
You can also do as follows:
export PYTHONPATH=$(pwd)
or
export PYTHONPATH=${PWD}
pwd is the present working directory.
I'm trying to set my PYTHONPATH to use the anaconda package I just installed, but whenever I try to import a module it tries to load it from the old path. I see my new path when I print sys.path, but it's behind the old path in the list. I installed anaconda in my home directory on a computer cluster, and so I can't uninstall the original path. Does anyone know why the current path set in the .bashrc file is not the priority for sys.path, and how I could fix this so only the path to anaconda works for loading modules? Thanks for any help or advice.
edit: I have contacted the support for the computer cluster, but haven't heard back yet. I'm looking here to try to get any advice on how to fix this as quickly as possible.
You don't have to set either of them. PYTHONPATH can be set to point to additional directories with private libraries in them. PYTHONHOME sets the location of default libraries.
Documentation:
PYTHONHOME
Change the location of the standard Python libraries. By default, the libraries are searched in prefix/lib/pythonversion and exec_prefix/lib/pythonversion, where prefix and exec_prefix are installation-dependent directories, both defaulting to /usr/local.When
PYTHONHOMEis set to a single directory, its value replaces both prefix and exec_prefix. To specify different values for these, setPYTHONHOMEto prefix:exec_prefix.PYTHONPATH
Augment the default search path for module files. The format is the same as the shell’sPATH: one or more directory pathnames separated byos.pathsep(e.g. colons on Unix or semicolons on Windows). Non-existent directories are silently ignored.In addition to normal directories, individual PYTHONPATH entries may refer to zipfiles containing pure Python modules (in either source or compiled form). Extension modules cannot be imported from zipfiles.
The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see
PYTHONHOMEabove). It is always appended toPYTHONPATH.An additional directory will be inserted in the search path in front of
PYTHONPATHas described above underInterface options. The search path can be manipulated from within a Python program as the variablesys.path.
For most installations, you should not set these variables since they are not needed for Python to run. Python knows where to find its standard library.
The only reason to set PYTHONPATH is to maintain directories of custom Python libraries that you do not want to install in the global default location (i.e., the site-packages directory).
Make sure to read: http://docs.python.org/using/cmdline.html#environment-variables
This will add a path to your Python process / instance (i.e. the running executable). The path will not be modified for any other Python processes. Another running Python program will not have its path modified, and if you exit your program and run again the path will not include what you added before. What are you are doing is generally correct.
set.py:
import sys
sys.path.append("/tmp/TEST")
loop.py
import sys
import time
while True:
print sys.path
time.sleep(1)
run: python loop.py &
This will run loop.py, connected to your STDOUT, and it will continue to run in the background. You can then run python set.py. Each has a different set of environment variables. Observe that the output from loop.py does not change because set.py does not change loop.py's environment.
A note on importing
Python imports are dynamic, like the rest of the language. There is no static linking going on. The import is an executable line, just like sys.path.append....
As also noted in the docs here.
Go to Python X.Y/Lib (Windows) or /usr/lib/pythonX.Y (Linux) and add these lines to the site.py there,
import sys
sys.path.append("yourpathstring")
This changes your sys.path so that on every load, it will have that value in it..
As stated here about site.py,
This module is automatically imported during initialization. Importing this module will append site-specific paths to the module search path and add a few builtins.
For other possible methods of adding some path to sys.path see these docs