Git Product home page Git Product logo

fastapi-pyinstaller's Introduction

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.

fastapi-pyinstaller's People

Contributors

k-rt-dev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.