Skip to content

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

Notifications You must be signed in to change notification settings

K-RT-Dev/fastapi-pyinstaller

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

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:

  1. Add uvicorn and the .py file that starts FastAPI as "hiddenimports" in the .spec.
  2. Make the generated .exe execute a Python function that then runs a uvicorn process. PyInstaller cannot directly execute a process with uvicorn.
  3. 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.

Instructions

  1. Make sure to have Python 3.9 installed (other versions may cause conflicts).
  2. Install Poetry if you don't have it.
  3. Validate that it is correctly installed using: poetry -v
  4. Create and activate the environment with: poetry shell
  5. Install dependencies with: poetry install
  6. 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
  7. Create the build using: pyinstaller FastApiPyInstaller.spec
  8. 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.

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages