Now i'm working on Python 3.6.8, Now i'm got stuck about import file. It's can't work
├── db
│ ├── commit.py
│ ├── config.py
│ ├── database.ini
│ └── __init__.py
└── main.py
This my code structure. for each file ,
#commit.py
from config import config
class Commit():
#many function
#some process. Which use package from config
#config.py
class config():
#some process.
#database.ini
#It's text file
#__init__.py
import commit
import config
#main.py
from db import commit
from db import config
class Main():
#many function
#include calling commit function
if __name__=="__main__":
#work with main function
But my program still error. When i run main.py and This below is error message.
db/__init__.py", line 1, in <module>
import commit
ModuleNotFoundError: No module named 'commit'
Why it can't work? because is Python 3+ ? how to fix this.
The following structure should works:
# main.py
from db.commit import *
from db.config import *
class Main():
#many function
#include calling commit function
if __name__== "__main__":
#work with main function
I suppose that you need to import everything from commit and config file. But if you need just the Commit class or the config class you can modify code like the following:
from db.commit import Commit
from db.config import config
Then other files
# init.py
# no imports
Remove imports from init.py
# config.py
class config():
#some process.
config.py it's fine like your version
# commit.py
from .config import *
class Commit():
#many function
#some process. Which use package from config
In this way should works correctly.
edit after comment
If from main.py, you import functions contained in commit.py module and in commit.py module you import functions contained in main.py module you will have circular imports.
In order to fix it you should organized the project structure in another way.
For example you can move Main class in another file and then import it in both, main.py and commit.py.
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.