Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
yet another query (issue ) with module hierarchy in python
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")

over 4 years ago · Santiago Trujillo
1 answers
Answer question

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!