Basic example of using PyInstaller to compile a server based on FastAPI. For use cases where we need to create a local server simply by running an .exe.
The project uses Poetry to create a Python environment.
The resulting server generates two GET endpoints on localhost:8000. The first one is "/" which returns a "Hello World". The second one is "/get_jpg" which returns a static file embedded in the compiled server.
The trick to be able to use a compiled server using PyInstaller is:
- Add uvicorn and the .py file that starts FastAPI as "hiddenimports" in the .spec.
- Make the generated .exe execute a Python function that then runs a uvicorn process. PyInstaller cannot directly execute a process with uvicorn.
- Use freeze_support() to avoid falling into recursive process problems.
The example includes adding a static file to the compiled server in the .spec file.
Solution based on this repository and this Stackoverflow thread.
- Make sure to have Python 3.9 installed (other versions may cause conflicts).
- Install Poetry if you don't have it.
- Validate that it is correctly installed using:
poetry -v
- Create and activate the environment with:
poetry shell
- Install dependencies with:
poetry install
- Run the program with Python to validate its functionality:
python main.py
. Make sure there is a response from localhost:8000 and localhost:8000/get_jpg - Create the build using:
pyinstaller FastApiPyInstaller.spec
- The build can be found in directory /dist. To run the program, click on "FastApiPyInstaller.exe"
This compilation will result in a folder with an approximate weight of 20mb. If we compress the folder using WinRar, we can achieve a weight of 9.5mb.