Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out my PYTHONPATH using Python?

How do I find out which directories are listed in my system’s PYTHONPATH variable, from within a Python script (or the interactive shell)?

like image 213
Paul D. Waite Avatar asked Sep 28 '09 22:09

Paul D. Waite


People also ask

How do I find Python Pythonpath?

Manually Locate Where Python is InstalledType 'Python' in the Windows Search Bar. Right-click on the Python App, and then select “Open file location“ Right-click on the Python shortcut, and then select Properties. Click on “Open File Location“

What is your Pythonpath?

PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. For most installations, you should not set these variables since they are not needed for Python to run. Python knows where to find its standard library.

How do I check environment variables in Python?

# Checking if an Environment Variable Exists in Python import os if 'USER' in os. environ: print('Environment variable exists! ') else: print('Environment variable does not exist. ') # Returns: # Environment variable exists!


2 Answers

You would probably also want this:

import sys print(sys.path) 

Or as a one liner from the terminal:

python -c "import sys; print('\n'.join(sys.path))" 

Caveat: If you have multiple versions of Python installed you should use a corresponding command python2 or python3.

like image 86
Vanuan Avatar answered Sep 22 '22 06:09

Vanuan


sys.path might include items that aren't specifically in your PYTHONPATH environment variable. To query the variable directly, use:

import os try:     user_paths = os.environ['PYTHONPATH'].split(os.pathsep) except KeyError:     user_paths = [] 
like image 37
Mark Ransom Avatar answered Sep 22 '22 06:09

Mark Ransom