简体   繁体   中英

Python 3.6 import methods from different subdirectories

I know there are a lot of readings for this topic available, but I can't figure out how to get it work for my specific case.

I have a project structure like this

root
    folder1
          __init__.py
          file1.py
          file2.py
    folder2
          __init__.py
          file3.py
          file4.py
          folder3
                 __init__.py
                 file5.py

In file3.py I have a function getNumbers() which i want to import in file2.py like this:

from folder2.file3 import getNumbers()

The __init__.py files are all empty.

I do get Error if I run file2.py from folder1 -directory

No module named 'folder2'

How can i get the import from different subdirectories work?

maybe use this

# some_file.py 
import sys 
sys.path.insert(0, '/path/to/application/app/folder') 

you may also use path.append(...) , for full answer see this link: Importing files from different folder

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