Skip to content

mohammadhasananisi/compile_fastapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI Compilation with PyInstaller

A guide to packaging FastAPI applications into standalone executables using PyInstaller.

Overview

This guide demonstrates how to compile a FastAPI application into an executable file using PyInstaller. This approach enables self-contained deployment without relying on external dependencies like web servers or containerization platforms.

Prerequisites

Before proceeding, ensure you have the following installed:

  1. Python 3.6+

  2. PyInstaller (pip install pyinstaller)

  3. FastAPI (pip install fastapi)

Example Code

  1. Create a file named main.py to store your FastAPI application code.

  2. Import the necessary libraries:

from fastapi import FastAPI
from uvicorn import run
import multiprocessing
  1. Define the FastAPI application:
app = FastAPI()

@app.get("/")
def hello():
    return "Hello, World!"
  1. Add the following code to the end of the main.py file to run the application:
if __name__ == '__main__':
    multiprocessing.freeze_support()  # For Windows support
    uvicorn.run(app, host="0.0.0.0", port=8000, reload=False, workers=1)

Compiling with PyInstaller

  1. Open a terminal window and navigate to the directory containing your FastAPI application's files.

  2. Execute the following command to compile the application into an executable file:

pyinstaller -F main.py --clean
  1. The compilation process will create a dist directory containing the compiled executable file. The executable file will have a name based on your application's name and the chosen output format (e.g., main for a single-file executable).

Running the Compiled Executable

  1. To run the compiled executable, simply execute the file. For instance, to run the executable named main, you would type the following command:
./dist/main

The compiled executable will start the FastAPI application and run it as a standalone process. You can then access the application at the specified port (8000 in the example) in your web browser.

Additional Considerations

  1. Dependencies Handling: PyInstaller can automatically embed dependencies into the executable file. However, for larger or more complex dependencies, it may be better to package them separately and distribute them alongside the executable.

  2. Security Implications: Executable files often have security restrictions, such as limited access to system resources. Ensure your application's security requirements are met when deploying in an executable format.

  3. Testing and Validation: Thoroughly test the compiled executable to ensure it functions correctly and behaves as expected in the target environment.

  4. Deployment Strategies: Choose the appropriate deployment strategy for your application, considering factors like scalability, access control, and maintainability.

Authors

Keep In Touch with Mohammad Hasan Anisi Email - github- Telegram - Linkedin.

Please report bugs and suggestions at the Telegram!

Releases

No releases published

Packages

No packages published