from . import XXXX

前端 未结 2 423
别那么骄傲
别那么骄傲 2020-12-11 06:03

In one of my Python packages the __init__.py file contains the statement

from . import XXXX

What does the \".\" mean here? I g

相关标签:
2条回答
  • 2020-12-11 06:09

    Its a relative import. From: http://docs.python.org/py3k/reference/simple_stmts.html#the-import-statement

    When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using leading dots in the specified module or package after from you can specify how high to traverse up the current package hierarchy without specifying exact names.

    One leading dot means the current package where the module making the import exists. Two dots means up one package level. Three dots is up two levels, etc. So if you execute from . import mod from a module in the pkg package then you will end up importing pkg.mod. If you execute from ..subpkg2 import mod from within pkg.subpkg1 you will import pkg.subpkg2.mod. The specification for relative imports is contained within PEP 328.

    0 讨论(0)
  • 2020-12-11 06:30

    It's a relative import.

    0 讨论(0)
提交回复
热议问题