Adam_G
Adam_G

Reputation: 7879

Understanding $PYTHONPATH in OSX

When I enter echo $PYTHONPATH, it returns an empty string.

However, when I enter python -c 'import sys;print sys.path' I receive the following:

['', '/Users/adamg/src/lasagne/examples/src/lasagne', 
'/Users/adamg/anaconda/lib/python2.7/site-packages/six-1.10.0-py2.7.egg', 
'/Users/adamg/anaconda/lib/python27.zip', '/Users/adamg/anaconda/lib/python2.7',
'/Users/adamg/anaconda/lib/python2.7/plat-darwin',
'/Users/adamg/anaconda/lib/python2.7/plat-mac', '/Users/adamg/anaconda/lib/python2.7/plat-mac/lib-scriptpackages', 
'/Users/adamg/anaconda/lib/python2.7/lib-tk', 
'/Users/adamg/anaconda/lib/python2.7/lib-old', 
'/Users/adamg/anaconda/lib/python2.7/lib-dynload', 
'/Users/adamg/anaconda/lib/python2.7/site-packages', 
'/Users/adamg/anaconda/lib/python2.7/site-packages/Sphinx-1.2.3-py2.7.egg', 
'/Users/adamg/anaconda/lib/python2.7/site-packages/aeosa', 
'/Users/adamg/anaconda/lib/python2.7/site-packages/cryptography-0.8-py2.7-macosx-10.5-x86_64.egg', 
'/Users/adamg/anaconda/lib/python2.7/site-packages/setuptools-18.4-py2.7.egg'] 

and python3 python3 -c 'import sys;print(sys.path)'

['', 
'/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python34.zip', 
'/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4', 
'/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin', 
'/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload', 
'/usr/local/lib/python3.4/site-packages']

Should I delete some of these? Further, what should I set $PYTHONPATH to so I can, by default, do scientific computing in Python 3.x?

Upvotes: 1

Views: 1965

Answers (2)

danielb78
danielb78

Reputation: 31

Module search path:

  • First it searches for a built-in module
  • Next it searches for the module in a list of directories given by sys.path, which is initialized from these locations:

    • The directory containing the input script (or current directory)
    • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH)
    • The installation-dependent default

How to alter sys.path:

  • sys.path.insert(_number_, '/whateverfilepath/')
  • Use 1 instead of 0 for number so that Python will look in the current working directory first

Upvotes: 3

Paul
Paul

Reputation: 3361

You're just seeing your current working directory plus the path defaults from your site (Anaconda) installation. From the docs:

sys.path: A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.

As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.

A program is free to modify this list for its own purposes.

Changed in version 2.3: Unicode strings are no longer ignored.

See also Module site This describes how to use .pth files to extend sys.path.

Upvotes: 1

Related Questions