开发者

More relative import oddness: .. notation

开发者 https://www.devze.com 2023-03-30 20:28 出处:网络
I noticed I\'ve made about 5 questions asking about relative imports, each with working solutions but with different situations. The more I read the docs and the more I try out different cases the mor

I noticed I've made about 5 questions asking about relative imports, each with working solutions but with different situations. The more I read the docs and the more I try out different cases the more I'm getting confused and questioning whether I'm actually reading the words correctly.

From pep 328:

A single leading dot indicates a relative import,
starting with the current package. Two or more leading dots
give a relative import to the parent(s) of the current package,
one level per dot after the first.

My understanding is that "current package" is the keyword here. What IS the "current package"? Is it the package of the module where execution begins开发者_StackOverflow中文版? (ie: the file with __ name __ == "__ main __").

Anyways, here is a simple situation of me using .. notation.

main/
  lib/
    __init__.py
    myLib.py
  plugin/
    __init__.py
    needLib.py
  run.py

run.py imports needLib: from plugin import needLib

needLib imports myLib: from ..lib import myLib

What's going through my head: (needLib) goes up a package, go into lib, grab myLib

Here is how I execute: python run.py

Result: Attempted relative import beyond toplevel package

But when I changed the import call to from .lib import myLib, it works.

I don't understand why the latter works and the .. notation doesn't. Of course, main isn't a package, so how about I just throw in a init.py there as well, resulting in

main/
  lib/
    __init__.py
    myLib.py
  other/
    __init__.py
    needLib.py
  __init__.py
  run.py

But that didn't make a difference; still attempting to import beyond top-level package

Check out this question: python: forcing relative imports to search from script file

The .. notation actually worked! Now that I think about it, I don't understand why it works. Execution in that scenario started 2 levels up from where the relative import takes place, while execution in this scenario starts 1 level up from where the relative import takes place. Other than where I begin execution, the situation is identical.

Both are me saying "go up one directory, go into the lib package, and grab the module you want".

What is the key difference between this situation and the referenced situation? Why does .. work there, but not here? Does me using 2.6 have anything to do with this? Or perhaps how I am executing it? python run.py


As you found out, from ..lib import myLib does not work as main is not a package. Relative imports only work within a module's own package. In your example, lib and other are two distinct packages.

Putting an __init__.py in main only works if you move run outside of the newly created main package, so that the current directory (part of sys.path) is not within a package.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号