1

I understand that there are many resources available and this question is asked many times. However, I am still wondering why I am getting this error.

I have a project structure like this:

PROJECT-MAIN-DIR
    src
        __init__.py
        driver.py
        app
             __init__.py
             app.py
    docs
    tests
        app
            test_app.py
    venv
    .gitignore
    requirements.txt

And here is my code:

# driver.py
class MyDriverClass:
    pass
# app.py
from src.driver import MyDriverClass, 

class Application(MyDriverClass):
    def foo():
         print("foo")

if I create a file PROJECT-MAIN-DIR\test_app.py

from src.app.app import Application
def test_app():
    my_app = Application()
    my_app.foo()

test_app()

It will work

but if I write a test in PROJECT-MAIN-DIR\tests\app\unit\test_app.py

from src.app.app import Application
def test_app():
    my_app = Application()
    my_app.foo()
    assert True

and run pytest \tests\app

Then I have

tests\app\unit\test_app.py:1: in <module>
    from src.app.app import Application
E   ModuleNotFoundError: No module named 'src'

How to fix it?

Note1: I am not looking for adding src to path using sys.path.append

Note2: It is a simplified version of the code and I cannot move things to root. Also I want to keep everything under src.

Note3: I do want to keep scripts in my package but I need to at least be able to test my classes with pytest


SOLUTION: I decided to add this line to tests\app\unit\test_app.py

sys.path.append(os.getcwd())

It worked for me

Amin Ba
  • 1,603
  • 1
  • 13
  • 38
  • Your problem primarily is that your main python file shouldn't be the most deeply nested thing in the project. If you don't want to change the search path then anything it needs to import has to be in site packages or in the same folder as it. You said "I understand that there are many resources available and this question is asked many times", did you read and understand those resources and the answers to those questions? – Kemp Jan 26 '22 at 12:42
  • 2
    You should restructure your app, so that the `.py` file you are executing is in the root of your code. Its folder will become the import base. – Klaus D. Jan 26 '22 at 12:43
  • It is a simplified version of the code and I cannot move things to root. Also I want to keep everything under `src`. – Amin Ba Jan 26 '22 at 13:25
  • @mkrieger1 What if I do not want to have scripts in my package but want to be able to `test classes my package with pytest`? – Amin Ba Jan 26 '22 at 13:36
  • 2
    The solution is explained in [Can't import my own modules in Python](https://stackoverflow.com/questions/9383014/cant-import-my-own-modules-in-python) (there are 3 solutions, 2 of them you have rejected, so use the 3rd one) – mkrieger1 Jan 26 '22 at 13:43
  • The reason is explained in https://stackoverflow.com/questions/24435697/python-is-the-current-directory-automatically-included-in-path – mkrieger1 Jan 26 '22 at 13:43
  • question edited – Amin Ba Jan 26 '22 at 13:44

0 Answers0