# from .module import，模块前面的点是什么意思？

2019年2月21日 / 320次阅读
Python

Python的很多代码中，都能看到，在import模块的时候，模块前面有个点（.），这是什么意思？

sound/                          Top-level package
__init__.py               Initialize the sound package
formats/                  Subpackage for file format conversions
__init__.py
wavwrite.py
aiffwrite.py
auwrite.py
...
effects/                  Subpackage for sound effects
__init__.py
echo.py
surround.py
reverse.py
...
filters/                  Subpackage for filters
__init__.py
equalizer.py
vocoder.py
karaoke.py
...


Intra-package References

When packages are structured into subpackages (as with the sound package in the example), you can use absolute imports to refer to submodules of siblings packages. For example, if the module sound.filters.vocoder needs to use the echo module in the sound.effects package, it can use from sound.effects import echo.

You can also write relative imports, with the from module import name form of import statement. These imports use leading dots to indicate the current and parent packages involved in the relative import. From the surround module for example, you might use:

from . import echo
from .. import formats
from ..filters import equalizer

Note that relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application must always use absolute imports.

### 留言区

《from .module import，模块前面的点是什么意思？》有1条留言

• 麦新杰

一个点（.）和两个点（..），所代表的含义，与Linux系统一致。 []

Ctrl+D 收藏本页