Skip to content

module import precedence mistake in the Python imports article #2

Closed
@etene

Description

@etene

Hello,

First of all excuse me if this isn't the right place to post this, i didn't really know how to get in touch so I figured a Github issue would be the best way to do it.

I was reading your post regarding imports (https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html), which I found instructive & well written, but there's a small point I think is wrong.

Here's the excerpt:

The Python documentation goes on to mention the following:

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 seems to be a mistake. For example, calling import math in start.py will import the standard math module, NOT my own math.py file in the same directory.

The documentation is correct: the described behavior applies to modules in the standard library directory. The math module is a builtin module (like the sys module), guaranteed to be always available, and thus not implemented as a separate Python file in the standard library.

If you name your own module argparse for example, it will indeed have precedence over the argparse module in the standard library.

Hope I was clear, and thanks again for your excellent post. Cheers !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions