Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

The Module Search Path

In this lesson, you’ll learn about the module search path. Continuing with the example from the previous lesson, take a look at what happens when Python executes the following statement:

>>>
>>> import mod
>>> mod.a
[100, 200, 300]
>>> mod.s
'Computers are useless. They can only give you answers.'

When the interpreter executes the above import statement, it searches for mod.py in a list of directories assembled from the following sources:

  • The directory from which the input script was run, or the current directory if the interpreter is being run interactively
  • The list of directories contained in the PYTHONPATH environment variable, if it is set. (The format for PYTHONPATH is OS-dependent but should mimic the PATH environment variable.)
  • An installation-dependent list of directories configured at the time Python is installed

The resulting search path is accessible in the Python variable sys.path, which is obtained from a module named sys:

>>>
>>> import sys
>>> sys.path
['', '/Library/Frameworks/Python.framework/Versions/3.7/bin', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']

So, to ensure that your module is found, you need to do one of the following:

  • Put mod.py in the directory where the input script is located, or the current directory if interactive
  • Modify the PYTHONPATH environment variable to contain the directory where mod.py is located before starting the interpreter. Or put mod.py in one of the directories already contained in the PYTHONPATH variable.
  • Put mod.py in one of the installation-dependent directories, which you may or may not have write-access to, depending on the OS.

There is also one additional option: You can put the module file in any directory of your choice and then modify sys.path at run-time so that it contains that directory. For example, in this case, you could put mod.py in directory /Users/chris/ModulesAndPackages and then issue the following statements:

>>>
>>> sys.path.append(r'/Users/chris/ModulesAndPackages')
>>> sys.path
['', '/Library/Frameworks/Python.framework/Versions/3.7/bin', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages', '/Users/chris/ModulesAndPackages']
>>> import mod
>>> mod.s
'Computers are useless. They can only give you answers.'

Once you’ve imported a module, you can determine the location where it was found with the module’s __file__ attribute:

>>>
>>> import mod
>>> mod.__file__
'/Users/chris/ModulesAndPackages/mod.py'
>>> import re
>>> re.__file__
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/re.py'

The directory portion of __file__ should be one of the directories in sys.path.

Pygator on Feb. 15, 2020

Is it possible to make the path.append() call permanent on the python path? Very interesting!

Chris Bailey RP Team on Feb. 15, 2020

Hi @Pygator, I guess is depends on what you are looking for. It is possible to make an addition to the PYTHONPATH environment variable. I just did an experiment, where I added a directory on my desktop to the PYTHONPATH and was able to import from it. That would be done inside of the .bash_profile on a mac, and probably Linux. It would probably look a bit different on windows. Here are some of the details. stackoverflow.com/questions/3387695/add-to-python-path-mac-os-x

But I would probably stick to the items mentioned in the original article, first most placing the module into the site-packages directory. Which would be based on your installation. I use virtual environments, and each one should have a directory, venv/lib/<python_ver#>/site-packages. Which is good spot to use.

I’m not sure of a way to have it always call path.append() outside of writing it into particular scripts you want to run.

drewmullen on April 17, 2020

thanks for putting this series together, chris! you should consider updating this video to include some qualifiers on how pythonistas might (and might not) use the pythonpath. i can tell you from personal experience that i spent wayyy to long “installing” my modules by weird frankenstein scripts + sys.path appends.

its important for people to understand what the pythonpath is, so im glad you made this video! but i think its also important for people to know that you shouldnt spend much time attempting to manipulate the pythonpath, short of debugging / development. you can even whet their appetite and elude to setuptools / pyproject.toml, etc

Become a Member to join the conversation.