Error loading ASGI app. Could not import module "src.main"

Question:

I saw the solution is changing main -> src.main so I tried that, but the same problem is occuring even if I use cd src to move to src and run uvicorn main:app --reload, errors continue.

What do I need?

I am using a virtual environment but I tried running uvicorn without the vm and I get the same issue.

code structure

.
|__ Alembic/
|__ venv/
|__ src/
    |__ main.py
    |__ services.py
    |__ database.py
    |__ models.py
    |__ __init__.py
Asked By: Bilal Haroon

||

Answers:

I think the problem comes from the VSCode Python interpreter

Try this in this order:

  1. In the VSCode terminal, go to your project root folder
myProjectRootFolder
|__ ...
|__ venv/
|__ src/
    |__ ...
    |__ main.py
    |__ __init__.py
  1. Activate your virtual env named venv
source venv/bin/activate
  1. Install the required dependencies
pip install fastapi uvicorn
  1. Now! Select the Python interpreter Ctrl+Shift+P (macOS: Cmd+Shift+P)

  1. Deactivate your venv (if you don’t deactivate your venv, you would have the same error you mentioned before)
deactivate
  1. Activate it again
source venv/bin/activate
  1. Run your code
# if you are in the root folder
uvicorn src.main:app

# or

# if you are in the src folder
uvicorn main:app
Answered By: prietosanti

So I made it work and I have no idea how. But what I did was change all the imports to module imports.

so instead of

import database

or

from . import database

I did:

from src import database

or

import src.database
Answered By: Bilal Haroon

This was quite annoying for me, so just in case someone finds this question but none of the answers above can help, here is what I did.

Add the following main method to the main file of my app:

if __name__ == "__main__":
    port = os.getenv("PORT")
    if not port:
        port = 8080
    uvicorn.run(app, host="0.0.0.0", port=8080) 

Then run this using python instead of running the uvicorn module:

python -m app.main

The output now was more useful:

Traceback (most recent call last):
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/Users/robert-andreidamian/Workspace/panprices/shelf-analytics-api/app/main.py", line 7, in <module>
    from app.routers import auth, users, availability, data, overview
  File "/Users/robert-andreidamian/Workspace/panprices/shelf-analytics-api/app/routers/overview.py", line 5, in <module>
    from app.main import get_db
  File "/Users/robert-andreidamian/Workspace/panprices/shelf-analytics-api/app/main.py", line 23, in <module>
    app.include_router(overview.router)
AttributeError: partially initialized module 'app.routers.overview' has no attribute 'router' (most likely due to a circular import)

As you can see the problem was generated by a circular dependency issue. I ran into this when following the SQLAlchemy tutorial in the FastAPI documentation: https://fastapi.tiangolo.com/tutorial/sql-databases/ . As suggested, i created the get_db function in my main file.

Before that I had already split the router in multiple files as explained in another section of the documentation: https://fastapi.tiangolo.com/tutorial/bigger-applications/.

This resulted in a circular dependency between the main file, that was importing the routing modules, and the routing modules that were importing the get_db function from the main module.

Hope this saved some of your time and frustration!

Answered By: Damian Robert
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.