Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
marcoscortina opened this issue Mar 30, 2020 · 1 comment
Closed

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

marcoscortina opened this issue Mar 30, 2020 · 1 comment

Comments

@marcoscortina
Copy link

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 added a commit that referenced this issue Apr 6, 2020
@chrisyeh96
Copy link
Owner

Thanks! This has been addressed in commit 3a6b3ee.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants