Python 从包中导入所有函数:“from .* import *”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34855071/
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-08-19 15:37:44  来源:igfitidea点击:

Importing all functions from a package: "from .* import *"

pythonpython-import

提问by CESCO

Goal

目标

I want to be able to import (on the __init__.py) all functions from every single file inside my package.

我希望能够__init__.py从我的包中的每个文件导入(在 上)所有函数。

Usage

用法

For example in this folder structure.

例如在这个文件夹结构中。

manage.py
- scripts/
   -- __init__.py
   -- tests.py
   -- deploy.py

I am currently doing the following:

我目前正在做以下事情:

manage.py:

管理.py:

from scripts import *

script/init.py:

脚本/初始化.py:

from .tests import *
from .deploy import *

But, every time I add another file to the package I have to add an import line on script/__init__.py, which is kind of annoying.

但是,每次我向包中添加另一个文件时,我都必须在 上添加一个导入行script/__init__.py,这有点烦人。

采纳答案by DhiaTN

  1. importliballows you to import any Python module from a string name. You can automate it with going through the list of files in the path.

  2. It's more pythonic to use __all__. Check herefor more details.

  1. importlib允许您从字符串名称导入任何 Python 模块。您可以通过浏览路径中的文件列表来自动化它。

  2. 使用__all__. 查看此处了解更多详情。

回答by Dimitris Fasarakis Hilliard

You can do it, manually, but you shouldn't.

您可以手动执行此操作,但您不应该这样做

Why you really do not want to do this:

为什么你真的不想这样做:

You'll end up with a namespace where understanding what is what and from where it came from will be extremely hard, with difficulty increasing as the size of the overall project does. Appart from being completely unintuitive for Python, think of anybody else that might view your code or even worse, think about yourself re-reading it after 1 month and not remembering what's going on. You don't need that in your life.

您最终会得到一个命名空间,在该命名空间中,理解什么是什么以及它来自何处将非常困难,并且随着整个项目的规模增加,难度会增加。除了对 Python 完全不直观之外,想想其他人可能会查看您的代码,甚至更糟,想想自己在 1 个月后重新阅读它并且不记得发生了什么。你的生活中不需要那个。

In addition to that, any functions you expose to the importer that might overlap with other functions in other modules are going to get shaddowed by the most recent one imported. As an example, think of two scripts that contain the same function foo()and watch what happens.

除此之外,您向导入程序公开的任何可能与其他模块中的其他功能重叠的功能都将被最近导入的功能所掩盖。例如,考虑包含相同函数的两个脚本foo()并观察会发生什么。

>>> from scrpt1 import *
>>> foo()
Script 1
>>> from scrpt2 import *
>>> foo()
Script 2

Don't need that in your life either.Especially when it is so easy to bypass by being explicit.

在你的生活中也不需要那个。尤其是当它很容易通过显式绕过时。



Here are some related lines from the text contained in import this:

以下是 中包含的文本中的一些相关行import this

Explicit is better than implicit.

显式优于隐式。

Be explicit about the place where your functions are defined in. Don't "spaghetti" your code. You'll want to hit yourself in the future if you opt in for a mesh of all stuff in one place.

明确定义您的函数的位置。不要“意大利面条”您的代码。如果您选择在一个地方使用所有东西的网格,您将来会想要打自己。

Special cases aren't special enough to break the rules.

特殊情况不足以打破规则。

Really self explanatory.

真的不言自明。

Namespaces are one honking great idea -- let's do more of those!

命名空间是一个很棒的想法——让我们做更多的事情!

"more of those!", not less; don't miss out on how wonderful namespaces are. Python is based on them; segregating your code in different namespaces is the foundation of organizing code.

“还有这些!” , 不低于; 不要错过命名空间的美妙之处。Python 基于它们;将代码隔离在不同的命名空间中是组织代码的基础。