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.

r0f1
  • 2,717
  • 3
  • 26
  • 39
  • 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 `import`s, rather than hacking the path. – jonrsharpe Jul 11 '16 at 15:37

1 Answers1

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 *
Alden
  • 2,229
  • 1
  • 15
  • 21