I have the following python project setup:
/project
/doc
/config
some_config.json
/src
/folderA
__init__.py
Databaseconnection.py
...
/folderB
__init__.py
Calculator.py
...
/main
__init__.py
Main.py
...
/test
__init__.py
AnImportantTest.py
__init__.py
.gitignore
README.md
requirements.txt
Main.py is the "executable file" (or rather module) that calls all the other modules. All __init__.py files are empty. How are the import statements in Main.py supposed to look like? I also saw this, which was not very helpful however:
# Main.py
import sys
sys.path.insert(0,'../..')
from folderA.Databaseconnection import * # not working
from src.folderA.Databaseconnection import * # not working
Thank you.
asked Jul 11, 2016 at 15:33
r0f1
3,2065 gold badges32 silver badges48 bronze badges
1 Answer 1
Using sys.path.insert() gives python another directory to look at for modules to import. The directory ../../ adds the folder /project, but that is not where the modules exist. Use this instead:
# Main.py
import sys
sys.path.insert(0,'../') # /src
from folderA.Databaseconnection import *
answered Jul 11, 2016 at 15:38
Alden
2,2691 gold badge15 silver badges22 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
Main.py(should be lowercase) up to/src, which will simplify the imports, and put the/testsin a separate directory under/project. As it stands, you need..to go "up" one directory in yourimports, rather than hacking the path.