1
mt-kart
     |
      --> src/data_kart
     |        |
     |         --> apis
     |        |       |
     |        |        --> __init__.py
     |        |        --> endpoints.py
     |        |
     |         --> models
     |        |       |
     |        |        --> __init__.py
     |        |        --> modelx.py
     |        |
     |         --> defaults.py
     |         --> main.py
      --> tests
             |
              --> __init__.py
             |
              --> conftest.py
             |
              --> test_others.py

In main.py I get module not found error when I try to refer

from defaults import func1

But this works

from .defaults import func1

It should work without "." right?

2.

From endpoints.py how should I refer the modelx.py? My prev. developer had it like below.

from data_kart.models.modelx import model1

But it complains when I run some tests using pytest from mt-kart directory. Then the below worked for pytest. But i am afraid it may give error in some different scenario.

from src.data_kart.models.modelx import model1

The project uses FastApi and OpenApI Code generators

Edit-------------------

In conftest.py I added src to sys.path and the paths are working fine for my issue 2. sys.path.append(os.getcwd()+"/src")

Chris
  • 18,724
  • 6
  • 46
  • 80
sjd
  • 1,329
  • 4
  • 28
  • 48

1 Answers1

0

You could instead add the directory including your package in PYTHONPATH, as shown here. For example, if you are on Windows, the directory of your package should look something like this: C:/users/.../mt-kart/src. After adding the path to the environment variable, please make sure to start a new terminal to lunch pytest.

By adding C:/users/.../mt-kart/src in PYTHONPATH, it means that you can import your modules like this: from data_kart.models.modelx import model1 (as your previous developer did) and from defaults import func1 should work as expected. Every time you import a module from the data_kart package, it will point to the directory given in the PYTHONPATH variable. Hence, there would be no need to add sys.path.append(... to the top of each file in your project (or test files) to import modules.

Chris
  • 18,724
  • 6
  • 46
  • 80