Difference between $PATH, sys.path and os.environ - python-3.x

What is the difference between $PATH variable, sys.path and os.environ? I understand that they both serve as paths where python searches packages. But it'd be nice to have more elaborate response.
Just a working case from my practice is when I used the script with only os.environ before import on Ubuntu 16.04 I got ImportError: No module named XXX. At the same time on MacOS it worked well. After I added sys.path on Ubuntu I could get import module well.

This is actually more complicated than it would seem. It's unclear by the question if you understand the Linux/MacOS $PATH environment variable. Lets start there. The $PATH variable (in Python you're able to access the system environement variables from os.environ) denotes the current users $PATH variable as defined in various shell profile and environment files. It typically contains things like "/usr/bin" and other places where programs are installed. For example when you type "ls" into the system shell, the underlying system searches the $PATH for programs named "ls". So what actually gets executed is probably something like "/usr/bin/ls" I've included additional reading below.
sys.path on the other hand is constructed by Python when the interpreter is started, based on a number of things. The first sentence in the help page is as follows. "A list of strings that specifies the search path for modules. Initialized from the environment variable $PYTHONPATH, plus an installation-dependent default." The installation-dependent portion typically defines the installation location of Python site packages. $PYTHONPATH is another environment variable (like $PATH) which can be added to facilitate the module search location and can be set the same way the system $PATH can
Typically if you have non-installed sources (ie you have Python files that you want to run outside the site-packages directory) you typically need to manipulate sys.path either directly in your scripts or add the location to the $PYTHONPATH environment variable so the interpreter knows where to find your modules. Alternatively, you could use .pth files to manipulate the module search path as well
This is just a basic overview, I hope you read the docs for better understanding
Sources
Linux $PATH variable information
Python sys.path
Python site.py

sys.path
Is a list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.
os.environ
Is a mapping object representing the string environment. For example, environ['HOME'] is the pathname of your home directory (on some platforms), and is equivalent to getenv("HOME") in C.
Environment variable PATH
Specifies a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting.

Related

Why does cmd and powershell recognize "python" but not "py"?

I seem to be having the opposite issue as this user in this question. I can run python successfully, but py does not work for me. Overall, this is not a big deal but when I try to run code in VS Code, I get the error below:
py: The term 'py' is not recognized as a name of a cmdlet, function, script file, or executable program.
How can I either make VScode run python or change my system to accept py?
When I first downloaded Python, it was not placed into the path but I was able to fix that. I also confirmed that VS Code has selected the correct interpreter for Python in the same place.
EDIT: IMAGES
Path directory
Path used in enviroment
When you write "python" on CMD or Powershell, it checks the given keyword("python" here) in all the directories in Environment Variables.
In the case of python, the directory mentioned for python in Environment Variables > Path, you will find "python.exe" and not "py.exe".
So your cmd or PowerShell recognizes python and not py.

How to set environment path to folder in my documents directory on mac

Hello I am working on a hyper ledger fabric project using python
I am running into an error on my program which requires me to set an environment path
below is the error given :
configtxgen not in PATH.
Configtx is empty
now I know the fix for this, it requires me setting an environment path to a file called
configtx.yaml
its location on my Mac is
documents/blockchain/fabric-sdk-py/test/fixtures/e2e_cli/configtx.yaml
please how do I set an environment path towards this file
Thanks
How you set an env var depends on your shell and whether you are running the program from a shell prompt or some other way (e.g., double-clicking on an application icon). Assuming you're using a POSIX shell like bash or zsh you would type something like this:
export VAR_NAME="$HOME/documents/blockchain/fabric-sdk-py/test/fixtures/e2e_cli/configtx.yaml"
You would normally put that statement in a file like ~/.bashrc so it is set each time you start an interactive shell.
P.S., If the documentation for the application you're using doesn't tell you how to do this I would open an issue suggesting such instructions be added.

Edit System Environment Variable Using Python

I was trying to add python path to the system evironment path by python.
I've tried
os.eviron['Path'] += ';C:/Anaconda3/'
However, I found this only make a difference in the script. I checked the system and didn't find ;C:/Anaconda3/ in the environment variable 'Path', which makes command python xxx.py can not be recognized by the system.
Is there any function or method to edit the system evironment path by python?
Could it be the spelling from environ to eviron?
or...
Maybe a quick batch script could help you do the trick as advised here. In the same question, you can find a way to do in python which would not survive a reboot here, but could do the trick.

LD_LIBRARY_PATH affects executable path

I have two versions of python installed on centos 6.4. It came with python 2.6.6. I installed 2.6.2 in /home/python.
My PATH variable has 2.6.6 in the path(/usr/bin/python). So in the console when I type 'python' it loads 2.6.6.
but, if I change the LD_LIBRARY_PATH to add /home/python/lib and no change to PATH variable, when I type 'python' at the console, it loads 2.6.2.
My understanding of executable search order in linux is 'looks at PATH' to find it. So how come the change in LD_LIBRARY_PATH loads 2.6.2? May be understanding is wrong. Could somebody help me understand this behavior?
"python" is a stub executable which loads the Python interpreter from its library. You've told it to look at a different set of libraries first, and so it uses the one it finds there rather than its normal library.

Error accessing mercurial with non-root user

I am trying to give a non-root user the ability to run mercurial commands from the shell. When I log in as the user and type "hg", I get this message:
abort: couldn't find mercurial libraries in [/usr/local/bin /usr/lib/python24.zip /usr/lib/python2.4 /usr/lib/python2.4/plat-linux2 /usr/lib/python2.4/lib-tk /usr/lib/python2.4/lib-dynload /usr/lib/python2.4/site-packages /usr/lib/python2.4/site-packages/Numeric /usr/lib/python2.4/site-packages/gtk-2.0]
(check your install and PYTHONPATH)
I do not have this problem as the root. I can run mercurial commands from any directory.
My problem is that I'm not very familiar with Linux at all, and so I don't know exactly how I'm supposed to change my PYTHONPATH variable (if indeed that's what I'm trying to do). I don't even know where the PYTHONPATH variable is being stored to see what's written there now.
Can someone tell me where the PYTHONPATH (or even regular PATH) environment variable is stored in Linux, and what steps I might take to remove the error method I'm getting above? If it helps, I'm using Putty and SSH to access the server.
Thanks! :)
The PYTHONPATH is just an environment variable that gets prepended to the python's internal search path. To see what is in there, do the following in python shell:
>>> import sys
>>> sys.path
It should print something like:
['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/PIL', '/usr/lib64/python2.7/site-packages/gst-0.10', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib64/python2.7/site-packages/webkit-1.0', '/usr/lib64/python2.7/site-packages/wx-2.8-gtk2-unicode', '/usr/lib/python2.7/site-packages', '/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
In practice, I would guess your shell is bash so the places where the environment variables can be set are:
/etc/profile, /etc/bashrc, ~/.profile and ~/.bashrc - the first 2 being system wide and the latter per user.
For further explanation, see this blog article abour bashrc and profile
EDIT
To fix this, the probably the easiest way is to install Mercurial via pip (I am assuming that you do not have Mercurial in the official repository for your Linux distribution, but usually python-setuptools or similar, that provides easy_install is). See this question for instructions.

Resources