Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there more search paths than in sys.path?

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.

like image 909
Lucy Brennan Avatar asked Jul 14 '11 17:07

Lucy Brennan


People also ask

What does Sys path contain?

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.

Is Pythonpath same as SYS path?

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 is SYS path populated?

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.

What is Python search path?

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.


1 Answers

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 named spam.py in the directory containing the input script and then in the list of directories specified by the environment variable PYTHONPATH. This has the same syntax as the shell variable PATH, that is, a list of directory names. When PYTHONPATH 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.

like image 139
NPE Avatar answered Sep 28 '22 02:09

NPE