Search code examples
pythonmodulepython-moduleproject-structure

Why my test files from a test folder can't import the source files from a source folder?


I have a problem where this is my project structure:

.
├── Resources/
├── src/
│   ├── __init__.py
│   ├── main.py
│   ├── utils/
│   │   ├── __init__.py
│   │   ├── util.py
│   │   └── otherUtil.py
│   └── calculations/
│       ├── __init__.py
│       └── financials.py
└── tests/
    ├── __init__.py
    └── test.py

My problem is that I can't reach the classes from the src/ folder from the tests, although the code in src/ can reach the Resources folder, through the first shown method.

I have tried:

  1. To append the home library path this way:

    enter image description here

    Here I used the from src import util after these lines, I even tried from .src import util.

  2. Then this way:

    enter image description here

    Here I used the from src import util after these lines, I even tried from .src import util.

  3. Than without the sys.path.append() with no use.

I have tried every combination I know, but for no use, and I don't want to install them as individual packages. Does someone have an idea, witch will solve my problem?

Clarification edit:

I don't want to put the tests in the source folder, i want to keep them separate.


Solution

  • You can use this code found here:

    # test.py
    import sys
    # insert at 1, 0 is the script path (or '' in REPL)
    sys.path.insert(1, '/path/to/utils/')
    
    import utils