0

On ubuntu, I am working with the following structure:

.
├── src
│   ├── __init__.py
│   └── main.py
└── test
    ├── __init__.py
    └── test.py

The main.py contains the following function:

def fun(x,y):
    return x+y

And the test.py has

from src.main import fun
print(fun(2,3))

I am working in the directory daddy. So pwd prints daddy.

When I try calling test.py,

$ python3 test/test.py
Traceback (most recent call last):
  File "test/test.py", line 1, in <module>
    from src.main import fun
ModuleNotFoundError: No module named 'src'

What options do I have, to get this right ?


I have gone through other answers related to this and they don't work for me... chiefly because they don't deal with working directory unlike my case.

Pe Dro
  • 2,651
  • 3
  • 24
  • 44
  • See [this](https://stackoverflow.com/questions/10272879/how-do-i-import-a-python-script-from-a-sibling-directory) – namgold Sep 15 '20 at 10:19
  • Typically the folder where the modules that you run is situated will be added to the `PYTHONPATH`. So, it is a good idea to have a properly structured project and a test runner that discovers tests. Which testing framework do you use? – Klaus D. Sep 15 '20 at 10:23
  • I am using `unittest` and I use `python -W ignore -m unittest discover -v` to automatically detect all the tests but this is a project small enough to ignore writing all the tests. I am trying to discover alternatives that dont requires writing tests everytime. – Pe Dro Sep 15 '20 at 10:26

0 Answers0