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

288
Views
I can not run fastapi and pytest at the same time due to an import module error

I'm having a circular problem that I'm not able to solve. I started my project as follows:

.
├── conf
│   ├── __init__.py
│   └── server.py
├── __init__.py
├── main.py
├── requirements.txt
└── tests
    ├── __init__.py
    └── v1
        ├── __init__.py
        └── test_item.py

In main.py I have:

from fastapi import FastAPI
import uvicorn

from conf.server import HOST, PORT

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

if __name__ == '__main__':
    uvicorn.run("main:app", host=HOST, port=PORT, reload=True)

In test_main.py

from fastapi.testclient import TestClient

from main import app

client = TestClient(app)


def test_read_main():
    response = client.get("/")
    assert response.status_code == 200
    assert response.json() == {"message": "Hello World"}

The error I get when I run the script from the same level as main.py with pytest:

C:\Python39\lib\importlib\__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests\v1\test_item.py:3: in <module>
    from main import app
E   ModuleNotFoundError: No module named 'main'

If I write the name of the folder before main:

from api.main import app

I get an error with the con/server.py module:

Hint: make sure your test modules/packages have valid Python names.
Traceback:
C:\Python39\lib\importlib\__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests\v1\test_item.py:3: in <module>
    from api.main import app
main.py:4: in <module>
    from conf.server import HOST, PORT
E   ModuleNotFoundError: No module named 'conf'

If I now change in main to include the name of the folder:

from api.conf.server import HOST, PORT

The test finally works, but now If I try to run the server wiht python main.py it will not work anymore:

    from api.conf.server import HOST, PORT
ModuleNotFoundError: No module named 'api'

What I'm doing wrong to get into this loop?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your should import your modules using absolute imports, as you already described in the final part of your question:

from api.main import app
from api.conf.server import HOST, PORT

and make sure to include the directory containing your package directory in PYTHONPATH, as shown here (Option 3). Then, you should be able to run your script. Other options listed in the above link might help you as well.

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!