[英]Python Import Statement and Recursion- need function available in module namespace
I have function foo() in main.py. 我在main.py中有函数foo()。 In main.py, I import create.py.
在main.py中,我导入create.py。 but there is a function in create.py that needs foo() from main.
但是create.py中有一个函数需要来自main的foo()。 I can't import main.py into create.py because main.py errors out...I assume this is some kind of race condition.
我无法将main.py导入到create.py中,因为main.py出错...我认为这是某种竞争条件。
How can I make foo() available in create.py namespace? 如何在create.py命名空间中使foo()可用? It seems kind of inefficient to make foo() a module and imported by both main.py and create.py just for one function.
将foo()作为模块并由main.py和create.py导入仅用于一个函数似乎效率低下。
The easy answer is move foo() to foo.py and import from there or move it to create.py and import it from there into main.py - if there are things in main.py that it needs the move the too. 简单的答案是将foo()移动到foo.py并从那里导入或将其移动到create.py并将其从那里导入main.py - 如果main.py中有东西它也需要移动。 Your other option is to pass foo from main into create as a function parameter where it is needed.
您的另一个选择是将foo从main传递到create作为需要它的函数参数。
Just a simple hack, but this would not help in general situation. 只是一个简单的黑客,但这在一般情况下无济于事。
import create.py in main.py when it is NOT called by importing. 在main.py进口create.py当它不通过导入调用。
# in main.py
if __name__ == '__main__':
from create import *
So this will import create when you excute main.py by python main.py
and that will import create and it will import main again but this time, it will see it is being imported so __name__ == '__main__'
would return False
. 因此,当你通过
python main.py
将导入create并且将导入create并且它将再次导入main,但这次,它将看到它被导入,因此__name__ == '__main__'
将返回False
。 So circular chain of importing will stop. 因此循环的进口链将停止。
Remember, it will not work when you will try to import main.py in some other script, because create.py won't get imported then. 请记住,当您尝试在其他脚本中导入main.py时, 它将无法工作 ,因为create.py将不会导入。
So to make this thing work, you have to execute main.py, you can not import it. 所以要使这个东西工作,你必须执行main.py,你不能导入它。
When you import a module, Python's import
machinery executes the code for that module to populate it, but only once. 导入模块时,Python的
import
机制会执行该模块的代码来填充它,但只能执行一次。 If you import the same module multiple times, it's fetched from the sys.modules
cache instead. 如果多次导入同一模块,则会从
sys.modules
缓存中获取它。 There's no way to end up with an infinite import loop. 没有办法结束无限导入循环。
However, Python adds the module to sys.modules
before executing the code for it, so it won't be fully populated until the entire module has been executed. 但是,Python在为其执行代码之前将模块添加到
sys.modules
,因此在整个模块执行之前,它不会完全填充。 So if module A imports module B, and module B imports module A again, it will see a partially initialized module. 因此,如果模块A导入模块B,模块B再次导入模块A,它将看到部分初始化的模块。 Which is what seems to be happening here;
这似乎是在这里发生的事情;
main
imports create
before it's fully populated, so when create
tries to access something in main
, it doesn't yet exist. main
导入在完全填充之前create
,因此当create
尝试访问main
某些内容时,它还不存在。
Some solutions: 一些解决方案
import name
everywhere (no from name import
), and call your main function from the bottom of your script. import name
(不from name import
),并从脚本底部调用main函数。 This way, all modules will finish importing before you execute anything else. import create
statement down below the function you need access to, so it's defined before the code in create.py
is executed (this is kind of silly, but can be used as a last resort). import create
语句向下移动到您需要访问的函数下方,因此在执行create.py
的代码之前定义它(这有点傻,但可以作为最后的手段使用)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.