mercredi 20 février 2019

Python Unittest: Fail due to import from same folder

the question seems pretty trivial, but I could not find a single answer to it online. Heres my setup:

project:
- src:
- - __init__.py (empty)
- - file1.py
- - file2.py
- test:
- - test1.py
- - test2.py
- - __init__.py (empty)

Now, I want to run

python -m unittest discover

from the project folder.

In test1.py I import and import from my first source file:

from src.file1 import class1

In file1.py however, I import from the other one:

from file2 import class2

Running the unittest as mentioned above results in:

[...]
    from file2 import class2
ModuleNotFoundError: No module named 'file2'

What am I doing wrong here?

Thank you and have a nice day!

Aucun commentaire:

Enregistrer un commentaire