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

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)

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")

Why does PyCharm sometimes not use system environment variable for PATH with a virtual environment?

Using PyCharm, I have configured a virtual environment. This results in some really confusing interactions with PATH. When I look at the environment variable for PATH that a file has:
print '##### PATH'
print os.environ['PATH']
I see the following output:
##### PATH
/usr/bin:/bin:/usr/sbin:/sbin:/Users/username/.virtualenvs/venv/bin
This confuses me because I have explicitly told PyCharm to use system environment variables through the following dialog:
When clicking "Show" I see that PATH is defined differently:
What I do not understand is:
Running PyCharm from a shell using charm results in PATH being successfully set:
##### PATH
/Users/username/.virtualenvs/username/bin:/Users/username/go/dependencies/bin:/usr/local/go/bin:/usr/local/go/bin:/usr/local/bin:.... etc
This is without any other configuration taking place in PyCharm - simply relaunching it from a shell causes PATH to behave set differently
I can also manually define the PATH variable in PyCharm (to the value given in the parent environment variables tab) and it works fine, too
But for some reason PyCharm is not pulling the PATH variable from my parent environment variables even though it is present and appears to be included properly (without either of the above two workarounds).
Why is PyCharm doing this and what do I need to do to actually "include parent environment variables" correctly?
I am using PyCharm 5.0.4 on OS:X with a bash shell.
It is a known bug in pycharm and OS X: https://youtrack.jetbrains.com/issue/PY-17816
You need to start python from the command line if you want to inherit those variables.
To create a shortcut select "Tools> Create Command-line launcher".

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.

Where is the python path set when I don't have a .bash_profile?

I am running ubuntu, and I don't have a .bash_profile.
So my question is, where exactly is my python path set then?
How can I see what the current python path is, doing:
$PYTHON_PATH
doesn't return anything?
It's set by the site module, and the interpreter executable itself. sys.path contains the current value.
You can see your python path in python like so:
>> import sys
>> print sys.path
you can create a .bash_profile with your favorite editor, and put into it:
export PYTHONPATH=$HOME/lib/python
or whatever, that's one example.
echo $PYTHONPATH /etc/profile and /etc/bashrc are the global setting files bash scans before looking in your home directory at start up. It's also safe to create a .bash_profile if one doesn't exist.
Normally PYTHONPATH is empty anyways.

how to set up environment variables for 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.

Categories