1

I would imagine that this has been answered before, but I could not find a thread for that describes the search process exactly. Apologies if I am wrong. I

What is exactly the search process Python follows for finding a module? What paths are considered in what order? (e.g. sys.path vs PYTHONPATH definitions, site-packages etc.)

I copied below the information from the link provided in the comments, the following, but I have the following questions:

  • What is the installation-dependent default exactly?
  • What happens if you have a virtual environment from virtualenv or from conda activated?

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • The directory containing the input script (or the current directory).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • The installation-dependent default.

After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.

Josh
  • 11,979
  • 17
  • 60
  • 96
  • From [this question](http://stackoverflow.com/questions/9586630/python-paths-and-import-order), I found [this link](https://docs.python.org/2/tutorial/modules.html#the-module-search-path) that may help you. – Celeo Sep 16 '14 at 17:57
  • 1
    There's a [good video](http://pyvideo.org/video/1707/how-import-works) on how import works from pycon a few years ago. – Silfheed Sep 16 '14 at 18:10

2 Answers2

1

The answer your looking for is here: https://docs.python.org/2/tutorial/modules.html at section 6.1.2.

According to the site it is

the directory containing the input script (or the current directory).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
the installation-dependent default.
Donkyhotay
  • 73
  • 6
  • Thanks @Donkyhotay +1. I have updated the OP with that info and questions based on that answer. – Josh Sep 16 '14 at 18:01
1

I believe the docs you are looking for are https://docs.python.org/2/library/site.html. Note that two other things to look out for are .pth files and user site directories (~/.local/lib/python2.7).

I don't know how virtualenv works. Conda environments should be thought of as independent installations of Python, so they have their own independent lib/python2.7 and lib/python2.7/site-packages directories. However, things like PYTHONPATH, PYTHONHOME, and ~/.local/lib/python2.7 are global, so all conda environments will use them (which is why I personally recommend against using them unless you really know what you are doing).

asmeurer
  • 86,894
  • 26
  • 169
  • 240