Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between $PATH, sys.path and os.environ

Tags:

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.

Thanks for the explanation at Advance.

like image 978
Ivan Shelonik Avatar asked Jul 11 '18 14:07

Ivan Shelonik


People also ask

What is the difference between OS and sys module in Python?

The os module contains two sub-modules os. sys (same as sys) and os. path that are dedicated to the system and directories; respectively. Whenever possible, you should use the functions provided by these modules for file, directory, and path manipulations.

Is SYS path same as Pythonpath?

PYTHONPATH is related to sys. path very closely. PYTHONPATH is an environment variable that you set before running the Python interpreter. PYTHONPATH , if it exists, should contain directories that should be searched for modules when using import .

What is SYS path used for?

sys. path is a built-in variable within the sys module. It contains a list of directories that the interpreter will search in for the required module. When a module(a module is a python file) is imported within a Python file, the interpreter first searches for the specified module among its built-in modules.

What is path in Python and environment variable?

The PYTHONPATH environment variable is used by Python to specify a list of directories that modules can be imported from on Windows. When running, you can inspect the sys. path variable to see which directories will be searched when you import something.


2 Answers

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
like image 156
sehafoc Avatar answered Sep 18 '22 13:09

sehafoc


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.

like image 37
himank Avatar answered Sep 22 '22 13:09

himank