从同名的 Python 模块导入函数/类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2519511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:49:35  来源:igfitidea点击:

Importing a function/class from a Python module of the same name

pythonimportmodule

提问by Brendan

I have a Python package mymodulewith a sub-package utils(i.e. a subdirectory which contains modules each with a function). The functions have the same name as the file/module in which they live.

我有一个mymodule带有子包的 Python 包utils(即一个包含模块的子目录,每个模块都有一个函数)。这些函数与它们所在的文件/模块具有相同的名称。

I would like to be able to access the functions as follows,

我希望能够访问以下功能,

from mymodule.utils import a_function

from mymodule.utils import a_function

Strangely however, sometimes I can import functions using the above notation, however other times I cannot. I have not been able to work out why though (recently, for example, I renamed a function and the file it was in and reflected this rename in the utils.__init__.pyfile but it no longer imported as a functions (rather as a module) in one of my scripts.

然而奇怪的是,有时我可以使用上述符号导入函数,但有时我不能。我一直无法弄清楚为什么(例如,最近,我重命名了一个函数和它所在的文件,并在文件中反映了这个重命名,utils.__init__.py但它不再作为函数(而不是作为模块)导入我的脚本。

The utils.__init__.pyreads something like,

utils.__init__.py读取类似,

__all__ = ['a_function', 'b_function' ...]
from a_function import a_function
from b_function import b_function
...

mymodule.__init__.pyhas no reference to utils

mymodule.__init__.py没有参考 utils

Ideas?

想法?

回答by Daniel Stutzbach

Do your utils functions need to import other utils functions? (or import other modules that import other utils functions). Suppose for example that a_function.py contains contains "from mymodule.utils import b_function". Here's your utils.py with a bunch of extra comments:

您的 utils 函数是否需要导入其他 utils 函数?(或导入导入其他 utils 函数的其他模块)。例如,假设 a_function.py 包含“from mymodule.utils import b_function”。这是您的 utils.py 和一堆额外的评论:

# interpreter is executing utils.py
# Right now, utils.a_function and utils.b_function are modules

# The following line executes the a_function module, 
# then rebinds a_function to a_function.a_function
from a_function import a_function 

# The following line executes the b_function module, 
# then rebinds b_function to b_function.b_function
from b_function import b_function

When the a_function module is first imported by utils.py, utils.b_function is a module not a function. Any module that states "from mymodule.utils import b_function" before the last line is executed will end up with a reference to the b_function module instead of the b_function function.

当 a_function 模块第一次被 utils.py 导入时,utils.b_function 是一个模块而不是一个函数。任何在最后一行执行之前声明“from mymodule.utils import b_function”的模块最终都会引用 b_function 模块而不是 b_function 函数。

In general, I find that the from somemodule import somethingidiom is fraught with peril for any large project. It's great for short scripts, but once you start introducing circular import dependencies you run into problems and need to be careful about where you use it.

总的来说,我发现这个from somemodule import something习语对任何大型项目都充满危险。它非常适合短脚本,但是一旦您开始引入循环导入依赖项,就会遇到问题并且需要小心使用它的位置。

As a compromise between safety and saving on typing, I'd use from mymodule import utilsthen call utils.a_function(). That way, you will always get the object bound to utils.a_functionright nowinstead of whatever happened to be bound to utils.a_functionduring import.

作为安全和节省打字之间的折衷,我会使用from mymodule import utilsthen call utils.a_function()。这样,你总是会得到的对象绑定到utils.a_function现在,而不是无论发生在绑定到utils.a_function导入过程中。