chrisyeh96 / chrisyeh96.github.io

Personal website
https://chrisyeh96.github.io
MIT License
22 stars 22 forks source link

Recap order in which Python searches for modules to import #10

Closed marcoscortina closed 4 years ago

marcoscortina commented 4 years ago

Hi,

I was reading carefully your great post https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html because I discovered in it insights that I was for long wanted to clarify.

I think that once the built-in modules are mention above in the post, the recap paragraph:

Let’s recap the order in which Python searches for modules to import:

1. modules in the Python Standard Library (e.g. math, os)
2. modules or packages in a directory specified by sys.path:
     1. If the Python interpreter is run interactively:
           - sys.path[0] is the empty string ''. This tells Python to search the current working directory from which you launched the interpreter, i.e. the output of pwd on Unix systems.

        If we run a script with python <script>.py:
          - sys.path[0] is the path to <script>.py
     2. directories in the PYTHONPATH environment variable
     3. default sys.path locations

could be expressed something like:

Let’s recap the order in which Python searches for modules to import:

1. modules built-in in the Python Standard Library (e.g. math, os)

2. modules or packages in a directory specified by sys.path:
     1. If the Python interpreter is run interactively:
           - sys.path[0] is the empty string ''. This tells Python to search the current working directory from which you launched the interpreter, i.e. the output of pwd on Unix systems.

        If we run a script with python <script>.py:
          - sys.path[0] is the path to <script>.py
     2. directories in the PYTHONPATH environment variable
     3. default sys.path locations.
        That is, rest of the modules in the Python Standard Library that are not built-in (e.g. random).
          - sys.path[1:], or sys.path[2:] on case PYTHONPATH environment variable exists.

Hope you find it helpful and thanks a lot for your clear and tidy post.

chrisyeh96 commented 4 years ago

Thanks! This has been addressed in commit 3a6b3ee.