2

I have a project that I run from root_dir> python ./src/main_file.py. When I want to test it, I use root_dir> python -m pytest and it will search for any files of the format test*.py or *test.py.

I have a file structure similar to the following:

root_dir
├── src
│   ├── __init__.py
│   ├── main_file.py
│   ├── file1.py
│   ├── file2.py 
└── tests
    ├── __init__.py
    └── file1_test.py

What I'm wanting to do is create a test file to test file1.py, but I'm getting a ModuleNotFoundError for what I believe is the following reason and I'm not fully sure what the proper way to address it is.

In file1_test.py:

./file1_test.py/
import src.file1 as file1

def test_file1():
  <do stuff>

The problem occurs inside file1.py, where:

./file1.py/
import file2.py

<blah blah blah>

pytest fails with ModuleNotFoundError: No module named 'file2'. If I change file1.py to:

./file1.py/ (EDITED)
import src.file2.py

<blah blah blah>

then pytest will run successfully. However, if my main_file.py imports file1.py from within the same module (src),

./main_file.py/
import file1

<blah blah blah>

Then python ./src/main_file.py will fail with ModuleNotFoundError: No module named 'src.file2' due to that change. Changing file1 back to the original will work for main_file.py, but not pytest, and I'm stuck in a loop.

I can't figure out what I should be doing differently to get the normal import structure of main_file.py and pytest to cooperate. Should I be changing the directory? Should main_file.py be outside of src? I think I would just run into a similar issue where if file1 is importing file2, the import path a file outside of src expects and one inside of src will expect are always going to conflict.

3
  • import src.file2.py is this typo? Also, what did you set in your PYTHONPATH? Aug 14, 2021 at 0:20
  • You need to either configure your sys.path, or use import src.file1 If you rename src to something like my_module it would make more sense.
    – tchar
    Aug 14, 2021 at 0:27
  • @tchar is right. In your file1_test.py, it has no way of knowing where file1 is or where src is. The src directory would need to be added to python path then you should use import src.file1
    – William
    Aug 14, 2021 at 0:33

1 Answer 1

1

There are three ways to do what you want.

  1. Prefered way
root_dir
├── my_module
│   ├── __init__.py
│   ├── main_file.py
│   ├── file1.py
│   ├── file2.py 
└── tests
    ├── __init__.py
    └── file1_test.py

All files in my_module shoud import like

from my_module.file1 import some_function

Then you don't need to do any hacks

  1. Keepin src and assuming statements like
from src.file1 import some_function

are not what you want you need to modify your tests like

import os
import sys
sys.path.insert(0, os.path.abspath('src')
  1. Not modifying src, not modifying tests: Run tests with
PYTHONPATH=path/to/src pytest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.