Skip to content

Instantly share code, notes, and snippets.

@jackiekazil
Last active June 29, 2021 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jackiekazil/6202057 to your computer and use it in GitHub Desktop.
Save jackiekazil/6202057 to your computer and use it in GitHub Desktop.
How do I identify what is on my python path (where python looks to import modules)?

What is on my python path?

Knowning where python is trying to find files is important.

Let's say try to import a module and you get an Import Error.

Like this:

>>>  import foo
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-34d390fb3acc> in <module>()
----> 1 import foo

ImportError: No module named foo

We tried to import a module called 'foo'. Python could not find a module named 'foo' on python path, so Python returned an Import Error.

Let's check what is on our "Python path". The python path is where python is looking for modules to import.

import sys
import pprint

pprint.pprint(sys.path)

Example output:

['',
 '/Users/jkazil/Projects/envs/test/bin',
 '/Users/jkazil/Projects/code/test',
 '/Users/jkazil/Projects/envs/test/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
 '/Users/jkazil/Projects/envs/test/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg',
 '/Users/jkazil/Projects/envs/test/lib/python27.zip',
 '/Users/jkazil/Projects/envs/test/lib/python2.7',
 '/Users/jkazil/Projects/envs/test/lib/python2.7/plat-darwin',
 '/Users/jkazil/Projects/envs/test/lib/python2.7/plat-mac',
 '/Users/jkazil/Projects/envs/test/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/jkazil/Projects/envs/test/lib/python2.7/lib-tk',
 '/Users/jkazil/Projects/envs/test/lib/python2.7/lib-old',
 '/Users/jkazil/Projects/envs/test/lib/python2.7/lib-dynload',
 '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/jkazil/Projects/envs/test/lib/python2.7/site-packages',
 '/Users/jkazil/Projects/envs/test/lib/python2.7/site-packages/IPython/extensions']

Looking at all these locations on my computer, I know that there is no module in any of those locations called 'foo' meaning there is no folder called 'foo' or a file called 'foo.py'

Solution: Add the location of 'foo' to your python path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment