简体   繁体   English

执行导入的 function 本身时,嵌套 python 模块导入的问题

[英]issue with nested python module importing when executing imported function itself

I believe this question must have already been asked but I cannot find an explanation for my problem, sorry if it is a duplicate.我相信这个问题一定已经被问过了,但我找不到我的问题的解释,如果它是重复的,对不起。

Folder
├── Generator.py
└── modules
    ├── Function1.py
    └── Subfunction.py

Generator.py imports Function1, and Function1 imports Subfunction. Generator.py 导入 Function1,Function1 导入 Subfunction。 Function1 must be able to be run as a standalone program and as an imported module of Generator.py Function1 必须能够作为独立程序和 Generator.py 的导入模块运行

It is not a problem itself, as I am using the if __ name__ == "__ main__": to recognize the call type.这本身不是问题,因为我使用if __ name__ == "__ main__":来识别调用类型。
But the program fails on importing Subfunction depending on the code I am executing.但是程序在导入子函数时失败,具体取决于我正在执行的代码。

# Generator.py
    import Function1

# Function1.py
    import Subfunction
    import modules.Subfunction

The first one works if I execute Function1.py, but it fails if I run Generator.py如果我执行 Function1.py,第一个工作,但如果我运行 Generator.py,它会失败

The second one works if I execute Generator.py, but it fails if I run Function1.py如果我执行 Generator.py,第二个有效,但如果我运行 Function1.py,它会失败

I thought imports and relative paths are related to the module where the code is placed, not from a perspective of the top-caller.我认为导入和相对路径与放置代码的模块有关,而不是从顶级调用者的角度来看。 I tried import.modules.Function1 and import.Function1 but the issue remains.我尝试了import.modules.Function1import.Function1但问题仍然存在。 Is there any elegant way to import Subfunction for both uses, or do I need to include import under if name == main or trap it in try/except ?是否有任何优雅的方法可以为两种用途导入 Subfunction,或者我是否需要在 if name == main下包含 import 或将其捕获在try/except中?

Edit: all code for @Bastien B编辑:@Bastien B 的所有代码

In this shape it works if I execute Function1.py itself.如果我执行 Function1.py 本身,它会以这种形式工作。 If I execute Generator.py, I get the ModuleNotFoundError: No module named 'Function1'如果我执行 Generator.py,我得到 ModuleNotFoundError: No module named 'Function1'

# Generator.py
    import Function1
    print(Function1.Function1_return)

# Function1.py
    def Function1_return():
        return Subfunction.Subfunction_return()
    import Subfunction
    if __name__ == '__main__':
        print(Function1_return())

# Subfunction.py
    def Subfunction_return():
        return "this is subfunction"

From what i can see your 'if name ...' is not the problem.从我可以看到你的'如果名字......'不是问题。

If you use a local venv this should work just fine:如果您使用本地 venv,这应该可以正常工作:

generator.py生成器.py

from Folder.modules.function1 import Function1_return
print(Function1_return())

Function1.py函数1.py

from Folder.modules.Subfunction import Subfunction_return

def Function1_return():
    return Subfunction_return()

if __name__ == '__main__':
    print(Function1_return())

Subfunction.py子函数.py

def Subfunction_return():
    return "this is subfunction"

Depending of your files structure you may have to tweak this a beat根据您的文件结构,您可能需要稍微调整一下

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

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