83

In ruby the library path is provided in $:, in perl it's in @INC - how do you get the list of paths that Python searches for modules when you do an import?

Ken Williams
  • 22,756
  • 10
  • 85
  • 147
Kyle Burton
  • 26,788
  • 9
  • 50
  • 60
  • 2
    In Ruby I think you meant `$:`. `$"` is a list of modules loaded by `require`. – docwhat Oct 09 '11 at 16:57
  • 1
    You might want to take a look at my answer and others to this related question here: https://stackoverflow.com/a/38485710/436794 – Pierz Jan 10 '18 at 16:43

4 Answers4

116

You can also make additions to this path with the PYTHONPATH environment variable at runtime, in addition to:

import sys
sys.path.append('/home/user/python-libs')
Dasvid
  • 43
  • 4
apg
  • 2,611
  • 1
  • 18
  • 19
100

I think you're looking for sys.path

import sys
print (sys.path)
kuilin
  • 119
  • 1
  • 7
Jack M.
  • 30,350
  • 7
  • 55
  • 67
11
import sys
sys.path
John Millikin
  • 197,344
  • 39
  • 212
  • 226
5
python -c "import sys; print('\n'.join(sys.path))"


/usr/local/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python39.zip
/usr/local/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9
/usr/local/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload
/usr/local/lib/python3.9/site-packages
CodeFarmer
  • 2,644
  • 1
  • 23
  • 32