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.
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
.