How to find all paths being searched by Python

Add my projects paths to Python

in Terminal:

  1. Confirm shell used
echo $SHELL
  1. Open shell profile file in a text editor

Assuming zsh:

nano ~/.zshrc
  1. Add the following lines to the end of the file:

Use your arrow keys to navigate to the end of the file, then add the following line:

export PYTHONPATH="${PYTHONPATH}:/path/to/your/directory"
  1. Save and exit

Control+O to save the file
Enter to confirm the filename
Control+X to exit nano

  1. Reload shell profile:
source ~/.zshrc

How to find all paths being searched by Python

15 Sep 2022

run this to see all paths being searched by python:

import sys

print(f"Paths:")
for path in sys.path:
    print(f"{path}")

prints:

/Users/xxxx/Python/homee
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python39.zip
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/lib-dynload
/Users/xxxx/Python/indeXee
/Users/xxxx/Python/Scrapee

or from terminal:

GLOBAL:

python -m site

prints:

sys.path = [
    '/Users/xxxx/Python/homee/notes',
    '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python39.zip',
    '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9',
    '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/lib-dynload',
]
USER_BASE: '/Users/xxxx/Library/Python/3.9' (exists)
USER_SITE: '/Users/xxxx/Library/Python/3.9/lib/python/site-packages' (exists)
ENABLE_USER_SITE: False

getting site packages, running from venv:

python -c 'import site; print(site.getsitepackages())'

prints:

['/Users/xxxx/Python/homee/venv/lib/python3.9/site-packages']

USER:

python -m site --user-site

prints:

/Users/nic/Library/Python/3.9/lib/python/site-packages

Get site packages used within venv:

from distutils.sysconfig import get_python_lib
python_lib = get_python_lib()
print(f"\nPython lib:")
print(f"{python_lib}")

prints:

python_lib='/Users/xxxx/Python/homee/venv/lib/python3.9/site-packages'

Python may be installed in multiple places in your computer. When you get a new Mac, the default python directory may be

'usr/bin/python2.7'

You may also have a directory

'System/Library/Frameworks/Python.framework/Versions/2.7/bin/python'

The first one is the symlink of the second one. NOTE: not my case - usr/bin is not a symlink, but an executable 🤔

If you use HomeBrew to install python, you may get a directory in

'usr/local/bin/python2.7'

You may also have a directory as

'Library/Frameworks/Python.framework/Versions/2.7/bin/python'

from: https://stackoverflow/questions/23329034/python-osx-which-python-gives-library-frameworks-python-framework-versions-2

Resources

links

social