简体   繁体   English

模块命名空间中提供的Python Import Statement和Recursion-need函数

[英]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: 一些解决方案

  1. Move the shared code to a separate module, as suggested above. 如上所述,将共享代码移动到单独的模块。 Imports are only done once, and Python caches compiled byte code in PYC files, so it's not really that costly. 导入只进行一次,Python在PYC文件中缓存已编译的字节代码,因此实际上并不是那么昂贵。
  2. Move all code into functions, and use 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. 这样,在执行任何其他操作之前,所有模块都将完成导入。
  3. Move the 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.

相关问题 使 python3 子包模块可用于在主包命名空间中导入 - Make python3 subpackage module available for import in main package namespace 如果没有在Python中导入,全局命名空间中的内置模块的内容如何可用? - How are the contents of the builtins module available in the global namespace without import in Python? python import pandas- import 可以,但模块在 function 中不可用 - python import pandas- import is OK, but module not available in function 质数递归 - 它是如何工作的? (蟒蛇) - Prime Number Recursion- How Does It Work? (Python) 如何构造一个 python 导入语句来检查调用命名空间中是否存在模块别名? - How do I construct a python import statement to check for the presence of a module alias in the calling namespace? 导入语句后包内的模块不可用 - module within package not available after import statement python导入模块全局本地名称空间 - python import module global local namespace 如何将函数导入模块的主命名空间? - How to import function to main namespace of my module? 导入 Python 模块而不将其添加到本地命名空间 - Import a Python module without adding it to the local namespace 使eyeD3模块可以在python中导入 - Making the eyeD3-module available for import in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM