Skip to main content
 首页 » 编程设计

python之导入您自己的 Python 模块

2024年10月25日10zhoujg

我理解以下四行:

import bpy 
import numpy as np 
from sys import argv 
from os import * 

但我从未见过以下几行:

from . uisun import * 
from . hdr import sunposition 

那个点呢?它是指目录中的位置还是其他? 文件 uisun.py、sunposition.py、hdr.py 位于 __init__.py 的同一目录中其中包含上面的代码。 顺便说一句,这来自 Blender插件。

请您参考如下方法:

IT Intra-package References :

The submodules often need to refer to each other. For example, the surround module might use the echo module. In fact, such references are so common that the import statement first looks in the containing package before looking in the standard module search path. Thus, the surround module can simply use import echo or from echo import echofilter. If the imported module is not found in the current package (the package of which the current module is a submodule), the import statement looks for a top-level module with the given name.

当包被构造成子包时(如示例中的声音包),您可以使用绝对导入来引用兄弟包的子模块。例如sound.filters.vocoder模块需要使用sound.effects包中的echo模块,可以使用from sound.effects import echo。

从 Python 2.5 开始,除了上述隐式相对导入之外,您还可以使用 import 语句的 from module import name 形式编写显式相对导入。这些显式相对导入使用前导点来指示相对导入中涉及的当前包和父包。例如,在环绕模块中,您可以使用:

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