简体   繁体   中英

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).

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. 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.

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:

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

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

if __name__ == '__main__':
    main()

content of func2.py

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

if __name__ == '__main__':
    main()

content of func2B.py

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

if __name__ == '__main__':
    main()

content of func3.py

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

if __name__ == '__main__':
    main()

In your file func2B.py you should import the 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.

EDIT

Try to add folder2 to your sys.path in your main.py

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. In short, I'm attempting to execute func2B.py from two different files placed in different locations without changing the code.

The way the code is displayed, I'm able to execute 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)

content of 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

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):

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

if __name__ == '__main__':
    main()

Thanks again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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