1

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
1
  • 2
    I would move Main.py (should be lowercase) up to /src, which will simplify the imports, and put the /tests in a separate directory under /project. As it stands, you need .. to go "up" one directory in your imports, rather than hacking the path. Commented Jul 11, 2016 at 15:37

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.