简体   繁体   English

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

[英]Importing all functions from a package: “from .* import *”

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: manage.py:

from scripts import *

script/ init .py: script / init .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上添加一个导入行,这有点烦人。

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. 对于完全不直观的Python,想想任何可能会查看你的代码的人,或者更糟糕的是,想想你自己在1个月后重读它而不记得发生了什么。 You don't need that in your life. 你生命中不需要这样。

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; Python基于它们; segregating your code in different namespaces is the foundation of organizing code. 将代码隔离在不同的命名空间中是组织代码的基础。

  1. importlib allows you to import any Python module from a string name. importlib允许您从字符串名称导入任何Python模块。 You can automate it with going through the list of files in the path. 您可以通过路径中的文件列表自动执行它。

  2. It's more pythonic to use __all__ . 使用__all__更加pythonic。 Check here for more details. 点击此处了解更多详情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM