I thought that the sys.path was a complete list of all search paths for Python modules.
However, on my Ubuntu machine, '/usr/local/lib/python2.6/dist-packages/' is where almost all my modules are and that path is not in sys.path. And I can still import any module on that path.
EDIT, NOT TRUE: Even if I set the sys.path to the empty list, I can still import from that path.
Where does this implicit knowledge of the dist-packages path come from? And are there any other paths in this implicit group of search paths, or whatever it is?
EDIT: It seems like the second part of my post is not true. Indeed, "sys.path = []", will mean that I can not import anything, not even from my current working directory. My apologies.
sys. path is a built-in variable within the sys module. It contains a list of directories that the interpreter will search in for the required module. When a module(a module is a python file) is imported within a Python file, the interpreter first searches for the specified module among its built-in modules.
PYTHONPATH is related to sys. path very closely. PYTHONPATH is an environment variable that you set before running the Python interpreter. PYTHONPATH , if it exists, should contain directories that should be searched for modules when using import .
How sys. path gets populated. As the docs explain, sys. path is populated using the current working directory, followed by directories listed in your PYTHONPATH environment variable, followed by installation-dependent default paths, which are controlled by the site module.
With typical Python usage, the PYTHONPATH environment variable (or IRONPYTHONPATH , etc.) provides the default search path for module files. That is, when you use an from <name> import... or import <name> statement, Python searches the following locations in order for a matching name: Python's built-in modules.
Note the mention of an installation-dependent default in the following:
6.1.2. The Module Search Path
When a module named
spam
is imported, the interpreter searches for a file namedspam.py
in the directory containing the input script and then in the list of directories specified by the environment variablePYTHONPATH
. This has the same syntax as the shell variablePATH
, that is, a list of directory names. WhenPYTHONPATH
is not set, or when the file is not found there, the search continues in an installation-dependent default path; on Unix, this is usually.:/usr/local/lib/python
.Actually, modules are searched in the list of directories given by the variable
sys.path
which is initialized from the directory containing the input script (or the current directory),PYTHONPATH
and the installation-dependent default. This allows Python programs that know what they’re doing to modify or replace the module search path. Note that because the directory containing the script being run is on the search path, it is important that the script not have the same name as a standard module, or Python will attempt to load the script as a module when that module is imported. This will generally be an error. See section Standard Modules for more information.
edit On my Ubuntu box, /usr/local/lib/python2.6/dist-packages
is present in sys.path
. If I clear sys.path
and then try to import a module from the above directory, that no longer works. This suggests that the interpreter has no implicit knowledge of that directory and finds it via sys.path
.
edit When you conduct your experiments, make sure that you modify sys.path
right at the start of your Python session. If you import X
, then clear sys.path
, and then import X
again, the latter will not fail even though X
is no longer on sys.path
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With