简体   繁体   English

如何从不同的子目录导入模块

[英]How to import modules from different subdirectories

I just want to be able to execute the file func2B.py, both by executing func2B.py and by executing main.py (main.py executes func2B.py). 我只希望能够通过执行func2B.py和执行main.py(main.py执行func2B.py)来执行文件func2B.py。

When I try to do so, I get several errors such as "no module named ...", or "func3 is not defined", depending on what i insert in the different init .py files. 当我尝试这样做时,我会遇到一些错误,例如“没有模块命名为...”或“未定义func3”,具体取决于我在不同的init .py文件中插入的内容。 I tried many combinations of different imports but I can't figure out how to properly set the imports. 我尝试了许多不同导入的组合,但是我不知道如何正确设置导入。

I work with python 3.6 and a Win10 machine. 我使用python 3.6和Win10机器。

I have the following file structure: 我具有以下文件结构:

folder1\
    __init__.py # empty file
    main.py
    func1.py 
    folder2\
        __init__.py # empty file
        func2.py
        func2B.py
        folder3\
            __init__.py # empty file
            func3.py

content of main.py: main.py的内容:

import func1
func1.main()

# [works] execute function (func2) stored in another folder (folder2)
import folder2.func2
folder2.func2.main()

# [works] execute function (func2) stored in another folder (folder3)
import folder2.folder3.func3
folder2.folder3.func3.main()

# [doesn't work] execute function (func2B) stored in another folder (folder2)
# [doesn't work] the function (func2B) calls another function (func3)
import folder2.func2B
folder2.func2B.main()

content of func1.py func1.py的内容

def main():
    print('executing func1')

if __name__ == '__main__':
    main()

content of func2.py func2.py的内容

def main():
    print('executing func2')

if __name__ == '__main__':
    main()

content of func2B.py func2B.py的内容

def main():
    print('executing func2B, which executes func3')
    func3.main()

if __name__ == '__main__':
    main()

content of func3.py func3.py的内容

def main():
    print('executing func3')

if __name__ == '__main__':
    main()

In your file func2B.py you should import the func3 在文件func2B.py中,您应该导入func3

import folder3.func3

def main():
    print('executing func2B, which executes func3')
    func3.main()

if __name__ == '__main__':
    main()

Without that your script doesn't know the function func3 and throw the error. 否则,您的脚本将不知道函数func3并抛出错误。

EDIT 编辑

Try to add folder2 to your sys.path in your main.py 尝试在main.py中将folder2添加到sys.path中

import sys
sys.path.append('folder2')

import func1
func1.main()
...
...

See also the answer here: Importing files from different folder 另请参见此处的答案: 从其他文件夹导入文件

Thank you all for the replies. 谢谢大家的答复。 I'd like to pose once again my problem in a more summarized way 我想以更概括的方式再次提出我的问题

I got this folder structure 我有这个文件夹结构

folder1\
    __init__.py # empty file
    main.py
    func1.py 
    folder2\
        __init__.py # empty file
        func2.py
        func2B.py
        folder3\
            __init__.py # empty file
            func3.py

My goal is to be able to execute both func2B.py, and main.py (which contains func2B.py), without having to change the code every time I want to execute one file or the other. 我的目标是能够同时执行func2B.py和main.py(其中包含func2B.py),而不必每次我想执行一个文件或另一个文件时都更改代码。 In short, I'm attempting to execute func2B.py from two different files placed in different locations without changing the code. 简而言之,我试图从放置在不同位置的两个不同文件中执行func2B.py而不更改代码。

The way the code is displayed, I'm able to execute main.py. 代码显示的方式,我可以执行main.py。 I don't know if this is even possible, maybe I need to modify the path, or maybe I could fill the init .py files with proper inputs (tried that unsuccessfully) 我不知道这是否可能,也许我需要修改路径,或者我可以用适当的输入填充init .py文件(尝试失败)

content of main.py: main.py的内容:

import folder2.func2B # uncomment to be able to execute main.py (1/2)
folder2.func2B.main() # uncomment to be able to execute main.py (2/2)

content of func2B.py func2B.py的内容

import folder2.folder3.func3 # uncomment to be able to execute main.py (1/2)
#import folder3.func3 # uncomment to be able to execute func2B.py (1/2)

def main():
    print('executing func2B, which executes func3')
    # folder2.folder3.func3.main() # uncomment to be able to execute main.py (2/2)
    # folder3.func3.main() # uncomment to be able to execute func2B.py (2/2)

if __name__ == '__main__':
    main()

content of func3.py (irrelevant): func3.py的内容(无关):

def main():
    print('executing func3')

if __name__ == '__main__':
    main()

Thanks again 再次感谢

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

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