简体   繁体   中英

Import own files in python

Imagine a main.py. I have the following structure

├── main.py
├── moduleX
│   ├── setup.py
│   ├── submoduleA
│   │   └── fileA.py
│   │   └── fileB.py
│   ├── submoduleC
│   │   └── fileC.py

Main calls moduleX.setup and setup needs to call functions from submodules A and B.

However moduleX.setup is unable to find the submodules and I don't know how to import them

So it goes like this: in main.py

import moduleX.setup

in setup.py

from submoduleA import fileA
from submoduleA import fileB
import submoduleC

and all submodules and files are not found.

All subfolders have empty init .py files. I am not sure how to fill them, seems like a recursive problem.

I tried adding moduleX to sys.path I tried prpending moduleX everywhere I tried using.. and.

I don't know what I am doing wrong.

Python always uses the relative path to the file you are executing.

from moduleX.submoduleA import fileA
from moduleX.submoduleA import fileB
import moduleX.submoduleC

should work

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