Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sys.path vs. $PATH

I would like to access the $PATH variable from inside a python program. My understanding so far is that sys.path gives the Python module search path, but what I want is $PATH the environment variable. Is there a way to access that from within Python?

To give a little more background, what I ultimately want to do is find out where a user has Package_X/ installed, so that I can find the absolute path of an html file in Package_X/. If this is a bad practice or if there is a better way to accomplish this, I would appreciate any suggestions. Thanks!

like image 603
J Jones Avatar asked Oct 20 '22 03:10

J Jones


2 Answers

you can read environment variables accessing to the os.environdictionary

import os

my_path = os.environ['PATH']

about searching where a Package is installed, it depends if is installed in PATH

like image 107
Massimo Costa Avatar answered Nov 04 '22 00:11

Massimo Costa


sys.path and PATH are two entirely different variables. The PATH environment variable specifies to your shell (or more precisely, the operating system's exec() family of system calls) where to look for binaries, whereas sys.path is a Python-internal variable which specifies where Python looks for installable modules.

The environment variable PYTHONPATH can be used to influence the value of sys.path if you set it before you start Python.

Conversely, os.environ['PATH']can be used to examine the value of PATH from within Python (or any environment variable, really; just put its name inside the quotes instead of PATH).

like image 44
tripleee Avatar answered Nov 04 '22 01:11

tripleee