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 forPYTHONPATH
is OS-dependent but should mimic thePATH
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']
Note: The exact contents of sys.path
are installation-dependent. The above code block will almost certainly look slightly different on your computer. The operating system used in this lesson is macOS. If you would like to see what the path structure looks like in a Windows environment, check out the original article that this course is based on.
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 wheremod.py
is located before starting the interpreter. Or putmod.py
in one of the directories already contained in thePYTHONPATH
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!