简体   繁体   中英

Python - Import module which is in a subfolder of a two level higher directory

Is there a simple and robust way to import a module which is in a subfolder ( Folder B ) of a two level higher directory ( Root ) in relation to the directory of the running script ( Folder A_2 )?

Root
--- __init__.py
--- Folder A_1
    --- __init__.py
    --- Folder A_2
        --- __init__.py
        --- Running_script.py
--- Folder B
    --- __init__.py
    --- target_module.py

I think all the directories with modules that you want to import must have an empty __init__.py file. If you do so, Python will find it and you shouldn't need to specify any path. But check the link, it will explain it better.

You can check this out from this link here for everything about imports.

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