YouTip LogoYouTip

Fastapi Install

FastAPI depends on Python 3.8 and later versions. This section explains how to install FastAPI and set up the development environment. * * * ## Check Your Python Version Before installing FastAPI, confirm your Python version: $ python --version Python 3.11.0# Or $ python3 --version Python 3.11.0 If your Python version is earlier than 3.8, please upgrade Python first. * * * ## Create a Virtual Environment It's recommended to install FastAPI in a virtual environment to avoid conflicts with system Python packages: # Create a virtual environment $ python -m venv venv # Activate the virtual environment (macOS/Linux) $ source venv/bin/activate # Activate the virtual environment (Windows) $ venvScriptsactivate > Using a virtual environment is a best practice for Python development. Each project should use an independent virtual environment to prevent dependency conflicts between different projects. * * * ## Install FastAPI Use pip to install FastAPI: $ pip install fastapi This command installs only the core FastAPI package. If you need to install FastAPI along with all its optional dependencies at once, use: $ pip install "fastapi" `fastapi` will install the following components: | Package Name | Purpose | | --- | --- | | `uvicorn` | ASGI server for running FastAPI applications | | `python-multipart` | Support for form data and file uploads | | `jinja2` | HTML template engine | | `python-jose` | JWT token support | | `passlib` | Password hashing and encryption | | `python-dotenv` | Environment variable management | * * * ## Install Uvicorn FastAPI is an ASGI framework and requires an ASGI server to run. The most commonly used one is Uvicorn: $ pip install "uvicorn" `` will install uvloop (a high-performance event loop) and httptools (a high-performance HTTP parser), significantly improving performance. > ASGI (Asynchronous Server Gateway Interface) is a standard interface between Python asynchronous web servers and applications. It’s the asynchronous version of WSGI. Traditional frameworks like Flask use WSGI (synchronous), while FastAPI uses ASGI (asynchronous), enabling it to handle concurrent requests more efficiently. * * * ## Run Your First FastAPI Application After installation, create a file named main.py: ## Example from fastapi import FastAPI # Create a FastAPI application instance app = FastAPI() # Define a GET route for the root path @app.get("/") async def root(): return {"message": "Hello World"} Start the application from the command line: $ uvicorn main:app --reload Parameter description: | Parameter | Description | | --- | --- | | `main:app` | `main` refers to the filename main.py, and `app` refers to the name of the FastAPI instance variable created in the file | | `--reload` | Development modeβ€”automatically reloads the server when code changes are detected (only for development environments) | After successful startup, the terminal will display: INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: Started reloader process INFO: Started server process INFO: Waiting for application startup. INFO: Application startup complete. Open your browser and visit **http://127.0.0.1:8000**, and you’ll see the following JSON response: {"message": "Hello World"} !(#) Meanwhile, FastAPI automatically generates interactive API documentation: | Address | Document Type | Description | | --- | --- | --- | | **http://127.0.0.1:8000/docs** | Swagger UI | Interactive API testing documentation interface | | **http://127.0.0.1:8000/redoc** | ReDoc | Documentation interface with a focus on readability | * * * ## Use the FastAPI CLI The new version of FastAPI provides a fastapi command-line tool that makes it easier to run applications: # Development mode (auto-reload) $ fastapi dev # Production mode $ fastapi run `fastapi dev` automatically finds the FastAPI application in your project and starts the development server, equivalent to `uvicorn main:app --reload`. * * * ## Recommended Development Tools | Tool | Description | | --- | --- | | (https://code.visualstudio.com/) | Free and lightweight, offers excellent FastAPI development experience when paired with the Python and Pylance plugins | | (https://www.jetbrains.com/pycharm/) | Professional Python IDE, comes with built-in FastAPI project templates and powerful debugging features | | (https://qoder.com/users/sign-up?referral_code=whhACoCj9WryAtAh2HAqjvE2ppbzwWtz) | Alibaba’s AI-powered programming IDE, deeply customized based on VS Code | | ( | ByteDance’s AI-driven development tool, with built-in Python support | > We recommend using VS Code + Python pluginβ€”it provides the best support for FastAPI type annotations and auto-completion, and it’s completely free.
← Fastapi Api DocPython Stock Line Chart β†’