PyCharm 2021.3 Help


FastAPI is a web framework for building APIs with Python. It requires the versions of Python 3.6 and later. PyCharm Professional provides the following support for developing FastAPI applications:

Create a FastAPI project

  1. From the main menu, choose File | New Project, or click the New Project button in the Welcome screen. New Project dialog opens.

    Create a FastAPI project
  2. In the New Project dialog, do the following:

    • Specify the project type FastAPI.

    • Location: Keep the suggested project location or specify an alternative directory.

      • New environment using: if this option has been selected, choose the tool to be used to create a virtual environment. To do that, click the list and choose Virtualenv, Pipenv, Poetry, or Conda.

        Next, specify the Location and Base interpreter of the new virtual environment.

        Select the Inherit global site-packages checkbox if you want that all packages installed in the global Python on your machine to be added to the virtual environment you're going to create. This checkbox corresponds to the --system-site-packages option of the virtualenv tool.

        Select the Make available to all projects checkbox if you want to reuse this environment when creating Python interpreters in PyCharm.

        When configuring the base interpreter, you need to specify the path to the Python executable. If PyCharm detects no Python on your machine, it provides two options: to download the latest Python versions from or to specify a path to the Python executable (in case of non-standard installation).

        Downloading Python when creating a new project
      • Previously configured interpreter: if this option has been selected, choose the desired interpreter from the list, or (if the desired interpreter is not found), click Open and choose the interpreter. See Configure a Python interpreter for details.

  3. Once you click Create, PyCharm creates a Python environment and installs the fastapi and uvicorn packages. It also adds the and test_main.http files with some default code constructs.

    Newly created project for developing FastAPI applications

Use coding assistance to develop an application

When developing FastAPI applications, you can benefit from coding assistance available in the IDE.

  • Use inspections to analyze code problems. You can use the inspection widget to briefly preview the summary of the issues. Click the widget to get more information on each detected problem in the Problems tool window.

    Code inspections in FastAPI samples
  • You can use context-aware code completion to speed-up the coding process. Just start typing a code construct and the completion popup appears. You can also press Ctrl+Space to show available items.

    Class completion
    Method code completion

Launch and modify run/debug configurations

  1. Once you have created a new project, PyCharm provides you with a run/debug configuration, so that you can execute your FastAPI application. The created configuration is selected in the list of the available run/debug configurations. Just click Run the selected run/debug
            configuration next to the list of configurations to run your application.

    Default run/debug configuration for FastAPI

    The target application is executed in the Run tool window. You can click the link to preview the application in a browser.

    Running FastAPI application
  2. You can modify the default run/debug configuration created with the project. Select Edit configurations... from the list of the available configurations:

    Edit Configurations

    Select the target configuration in the left pane and modify its parameters, for example, you can add more uvicorn options (in this example: --reload delay 10).

    FastAPI run/debug configuration

    See Run/debug configurations for more details about working with run/debug configurations.

  3. You can also execute the .http file to test GET requests. You can run each request separately, or you can click the Run all tests Run all requests in file link on the .http file toolbar to test all requests at once.

    Select an option to execute requests in the default environment or without an environment.

    Run all tests

    PyCharm executes GET tests in the Services tool window. You can select a particular request to preview the summary of its execution status.

    Executing HTTP requests

    Lean more about HTTP requests in Execute HTTP requests.

With the Endpoints plugin, you can easily preview, modify, create, and test endpoints of your FastAPI application.

Manage Endpoints

  1. Press Ctrl+Alt+S to open the IDE settings and select Plugins.

    Discover the plugin in the Installed tab, and check if it is enabled. If it is not available there, switch to Marketplace and install it.

  2. Select View | Tool Windows | Endpoints to open the Endpoints tool window.

  3. The Endpoints tool window is shown in the lower group of the PyCharm tool windows. Its left pane lists the endpoints defined in the application.

    FastAPI and Flask endpoints; code completion for the @app methods

    The right pane shows documentation and code of the corresponding HTTP client.

    Double-click any item in the list of the endpoints to navigate to the corresponding method in the .py file. You can also right-click any item in the list to get a context menu with the options to navigate to the source code or to find the endpoint's usages.

  4. Use coding assistance to create and modify endpoints:

    • Code completion

      PyCharm completes endpoint names as you are typing them in Python files, HTTP clients, and other project files.

      Code completion in the HTTP client
    • Live templates

      With the live templates available for HTTP clients, you can quickly modify request methods, URLs, and variables.

      Live templates for an HTTP client
    • Refactoring

      If you need to rename an endpoint, use the Rename refactoring. Select an endpoint in the editor, press Shift+F6, and type its new name.

      Renaming an endpoint

      Renaming happens in all occurrences across the project.

  5. You can preview an HTTP client in a separate tab of the editor. Click Open in Editor, and PyCharm will open a temporary scratch file with the HTTP request. You can click the Run icon (Run an HTTP request) in the gutter to test it.

    Open an HTTP request in a separate edit tab

Last modified: 12 April 2022