how to set up environment variables for python - python

On WinXP sp2 I'd like to have a directory of modules that other python scripts will be using called "SharedPython" in the same directory as my python scripts. so, basically:
/pythonScripts
/pythonScripts/SharedPython
as well as other python scripts on the same level as the SharedPython directory.
when I run
print sys.path
I get the following output:
C:\WINDOWS\system32\python25.zip
C:\Python25\DLLs
C:\Python25\lib
C:\Python25\lib\plat-win
C:\Python25\lib\lib-tk
C:\Python25
C:\Python25\lib\site-packages
I don't know what environment variable controls this and, in fact, I don't see one that contains all these directories.
So,
a.)how do I determine which environment variable contains this list of dirs?
and
b.)can I just add the aforementioned SharedPython dir to that list?
I've tried setting PYTHONPATH to the following: %PYTHONPATH%C:\PythonScripts\SharedPython

You need the PYTHONPATH env var. The dirs listed in it are prepended to sys.path.
The correct way to setup PYTHONPATH in your case is:
set PYTHONPATH=%PYTHONPATH%;C:\PythonScripts\SharedPython
Note the semicolon between the second % and the C:\

Those paths are added by the site module; do not change this module, but rather create a batch file that adds your paths in %PYTHONPATH% and then runs the script.

Related

which python vs PYTHONPATH

If I type in which python I get: /home/USER/anaconda3/bin/python
If I type in echo $PYTHONPATH I get: /home/USER/terrain_planning/devel/lib/python2.7/dist-packages:/opt/ros/melodic/lib/python2.7/dist-packages
Should that not be the same? And is it not better to set it: usr/lib/python/
How would I do that? Add it to the PYTHONPATH or set the PYTHONPATH to that? But how to set which python?
You're mixing 2 environment variables:
PATH where which looks up for executables when they're accessed by name only. This variable is a list (colon/semi-colon separated depending on the platform) of directories containing executables. Not python specific. which python just looks in this variable and prints the full path
PYTHONPATH is python-specific list of directories (colon/semi-colon separated like PATH) where python looks for packages that aren't installed directly in the python distribution. The name & format is very close to system/shell PATH variable on purpose, but it's not used by the operating system at all, just by python.
which python is the path to your python interpreter. PYTHONPATH is an environment variable where your Python program can search for modules to import.
See section 1.2
Should that not be the same? And is it not better to set it: usr/lib/python/ How would I do that? Add it to the PYTHONPATH or set the PYTHONPATH to that? But how to set which python?
No they are not the same. You don't really need to modify the path to your Python interpreter. To modify the PYTHONPATH, you can set it in a shell, or from within a Python program by using sys.path
import sys
print(sys.path)
sys.path.append("another/path/to/search")

Python adding lots of things to PATH. How do I stop?

I'm finding that python is modifying my path in problematic ways -- in particular, it's pre-pending the path to my github development folder, which results in the wrong libraries being loaded.
In my terminal session, if I run echo $PATH I get:
~$echo $PATH
/Users/Nick/anaconda/bin:/usr/local/bin:/usr/bin:
/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/texbin
Which is perfect. But when I launch python from that same terminal session (either as python or ipython) and check my PATH from within python, I'm getting:
>>> print(sys.path)
['', '/Users/Nick/anaconda/lib/python3.4/site-packages/Cython-0.22.1-py3.4-
macosx-10.5-x86_64.egg', '/Users/Nick/github/pandas',
'/Users/Nick/anaconda/lib/python34.zip', '/Users/Nick/anaconda/lib/python3.4',
'/Users/Nick/anaconda/lib/python3.4/plat-darwin',
'/Users/Nick/anaconda/lib/python3.4/lib-dynload',
'/Users/Nick/anaconda/lib/python3.4/site-packages',
'/Users/Nick/anaconda/lib/python3.4/site-packages/Sphinx-1.3.1-py3.4.egg',
'/Users/Nick/anaconda/lib/python3.4/site-packages/aeosa',
'/Users/Nick/anaconda/lib/python3.4/site-packages/setuptools-18.0.1-py3.4.egg']
Where are these coming from and how do I stop them?
Thanks!
PATH has nothing to do with the Python module search path; that environment variable is used by your shell to find executables, instead.
You need to look at the PYTHONPATH variable here.
If that variable doesn't contain your extra elements, start Python with the -S command line switch to disable loading site.py; it may be that the extra elements are set by a .pth file. Also see the site module documentation:
A path configuration file is a file whose name has the form name.pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path.
While $PATH seems like it may be used by Python, what you actually want to look at is your $PYTHONPATH -- which is used by the import machinery and logic.
You should look into using virtualenvironments to control the pathing of Python module lookups.

Why sys.path doesn't contain cwd()?

I use python3 with emacs (editor and shell) under Linux OS. Why the cwd is not in the sys.path ?? How can we put it, for all sessions !!
I Thank you.
You do not want to add cwd() to the sys.path. Always adding cwd() would be a terrible idea as you can no longer control what files are available for import.
Python adds the directory of the script being executed instead.
E.g. when you run:
python.exe path/to/script.py
then path/to is automatically added to the sys.path.
Only if you run a script from the current directory is '' added to the start of the path, meaning the current working directory is searched for imports. E.g. when you run python.exe localfile.py then Python does add the current working directory, in the assumption you wont't change the current working directory while importing.
See Interface options in the Command line and environment documentation:
If the script name refers directly to a Python file, the directory containing that file is added to the start of sys.path, and the file is executed as the __main__ module.
and the sys.path documentation:
As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.
You can always add the current working directory to sys.path explicitly:
import sys
if sys.path[0] != '':
sys.path.insert(0, '')
Be careful, any python file or package in that working directory with a name matching a module you are already using in your code will mask that module, easily leading to breakage.
sys.path isn't the System Path, it's the path that python checks when looking for modules (when you execute import statements, for example).
To change it permanently, change the environment variable PYTHONPATH.
You can change your PYTHONPATH environmental variable.
This prepends locations to the default module search path.
The docs say that, when running a script from the command line,
the first item in sys.path will be the path to the script.
The docs say that sys.path is then
“initialized from the environment variable PYTHONPATH, plus an installation-dependent default.”
For example, if PYTHONPATH is empty (PowerShell: $env:PYTHONPATH = ""),
only the installation-dependent default is added.
However, if PYTHONPATH starts with an empty string,
then the current working directory (that the script is run from) is added to PYTHONPATH.
(Even just an initial separator will add this—for example a semicolon in PowerShell: $env:PYTHONPATH = ";".)
This is not always the desired behavior,
so you may wish to hesitate before making permanent changes in your PYTHONPATH.

Why does << export PYTHONPATH=$PYTHONPATH >> put the current working directory on sys.path?

Put that line in .bashrc (.profile on Mac), the working directory is on the path. Leave it out, it isn't.
export PYTHONPATH=$PYTHONPATH
I've noticed that shells with the line have a PYTHONPATH= line in their env output, those without don't, so perhaps that matters. Python also adds the current working directory if PYTHONPATH has some valid directory but begins with a ':' character. (Simply setting PYTHONPATH to some valid directory breaks the working directory inclusion.)
Why is this so?
What is best practice for ensuring the current working directory is on sys.path?
If you want to ensure it, then explicitly add . to the pythonpath:
export PYTHONPATH=.:$OTHER_DIRS:$PYTHONPATH
If you also want to have the current working directory in your sys.path with a PYTHONPATH set to something else, then you could try to set it up in your sitecustomize.py or usercustomize.py by inserting something like:
sys.path.append(os.getcwd())
Additional note:
Since the introduction of per user site-packages it's best to put your user packages there (you can get the folder name with site.USER_SITE). pip also makes it easy to install packages thera using the --user option, so I've found that at least for me ther rarely is the necessity to mess around with PYTHONPATH.
As per the docs,(this should clear your doubts)
The PYTHONPATH IS AN ENV. VARIABLE THAT augments the default search
path for module files. The format is the same as the shell’s PATH:
one or more directory pathnames separated by os.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 PYTHONHOME above). It is
always appended to PYTHONPATH.
An additional directory will be inserted in the search path in front
of PYTHONPATH as described above under Interface options. The search
path can be manipulated from within a Python program as the variable
sys.path.

How do I configure my sys.path variable in linux?

I want to automatically add entries to python's sys.path variable when run by my user in linux.
Is there something I can tweak in my home directory to get it done?
The environment variable PYTHONPATH sets the initial sys.path value.
You can set that it your shell initialization script (e.g. .bashrc or .cshrc)

Categories