I am running python FastAPI with UVICORN with multiple processors (5 processes),It is running smoothly from the code, but when I tried make the exe from pyinstaller and try to run the file, it is showing error.
filename: main.py
JavaScript
x
20
20
1
import multiprocessing
2
import os
3
4
import uvicorn
5
from fastapi import FastAPI
6
7
app = FastAPI()
8
9
10
@app.get("/")
11
async def root():
12
return {"message": "Hello World"}
13
14
15
if __name__ == "__main__":
16
multiprocessing.freeze_support()
17
print("Running the instance")
18
uvicorn.run("main:app", host="0.0.0.0", port=9000, workers=5)
19
20
Output code from source
JavaScript
1
12
12
1
python3 main.py
2
3
Running the instance
4
INFO: Uvicorn running on http://0.0.0.0:9000 (Press CTRL+C to quit)
5
INFO: Started parent process [17828]
6
INFO: Started server process [17869]
7
INFO: Waiting for application startup.
8
INFO: Application startup complete.
9
INFO: Started server process [17870]
10
INFO: Waiting for application startup.
11
INFO: Application startup complete.
12
I make a single file using pyinstaller with the following command
JavaScript
1
2
1
pyinstaller --onefile main.py
2
and while running the main file using
JavaScript
1
2
1
./main
2
get the following error
JavaScript
1
6
1
Running the instance
2
INFO: Uvicorn running on http://0.0.0.0:9000 (Press CTRL+C to quit)
3
INFO: Started parent process [18964]
4
ERROR: Error loading ASGI app. Could not import module "main".
5
ERROR: Error loading ASGI app. Could not import module "main".
6
How to refer the main:app, what is the actual class name after installer is created? I read somewhere that we need to use like foldername.main:app , but that also not working
Advertisement
Answer
I tried your program and installing with
JavaScript
1
2
1
pyinstaller --onefile --hidden-import=main main.py
2
solved it for me.