Are there more search paths than in sys.path?

I thought sys.path was a complete list of all the search paths for Python modules.

However, on my Ubuntu machine '/usr/local/lib/python2.6/dist-packages/' there are almost all of my modules and this path is not in sys.path. And I can import any module along the way.

EDIT, NOT TRUE: even if I set sys.path to an empty list, I can still import from this path.

Where does this implicit knowledge of the dist-packages path come from? Are there other paths in this implicit search path group or something else?

EDIT: It seems that the second part of my post is not true. Indeed, "sys.path = []" means that I cannot import anything, even from my current working directory. My apologies.

+5
source share
2 answers

Note the mention of the installation-dependent default in the below :

6.1.2. Module search path

When a module with a name is imported spam, the interpreter searches for a file with a name spam.pyin the directory containing the script input and then in the list of directories specified by the environment variable PYTHONPATH. This has the same syntax as the shell variable PATHthat is, a list of directory names. If the parameter is PYTHONPATHnot set or when the file is not found there, the search continues into the default path associated with the installation ; on Unix, this is common .:/usr/local/lib/python.

, , sys.path, , script ( ), PYTHONPATH . Python . , , script, , , script , , Python script , . . . " ".

edit Ubuntu /usr/local/lib/python2.6/dist-packages sys.path. sys.path, , . , sys.path.

. , , sys.path Python. import X, sys.path, import X , , X sys.path.

+4

, , sys.path, , script ( ), PYTHONPATH .

.

+1

All Articles