FastAPI
Available only in PyCharm Professional: download to try or compare editions
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:
In the main menu, go to File | New Project, or click the New Project button in the Welcome screen.
In the New Project dialog, do the following:
Select FastAPI as the project type.
Specify the project location.
Select Create Git repository to put the project under Git version control.
If you want to proceed with the Project venv or Base conda interpreter, select the corresponding option and click Create.
To configure an interpreter of another type or to use an existing environment, select Custom environment.
The following steps depend on your choice:
Generate new environmentSelect existing environmentcondaVirtualenvPipenvPoetryuvSelect the Python version from the list.
Specify the environment name.
Normally, PyCharm will detect conda installation.
Otherwise, specify the location of the conda executable, or click to browse for it.
Select the base interpreter from the list, or click and find the Python executable in your file system.
note
If you do not have the Python version in your system, you can download and install it.
This feature is available only on Windows and macOS.
Specify the location of the new virtual environment in the Location field, or click and browse for the location in your file system. The directory for the new virtual environment should be empty.
Select the Inherit global site-packages checkbox if you want 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.
Select the base interpreter from the list, or click and find the Python executable in your file system.
note
If you do not have the Python version in your system, you can download and install it.
This feature is available only on Windows and macOS.
If you have added the base binary directory to your
PATH
environmental variable, you do not need to set any additional options: the path to the pipenv executable will be autodetected.If PyCharm does not detect the pipenv executable, click Install pipenv via pip to allow PyCharm to install it for you automatically.
Alternatively, follow the pipenv installation procedure to discover the executable path and then specify it in the dialog.
Select the base interpreter from the list or click and find the Python executable in your file system.
note
If you do not have the Python version in your system, you can download and install it.
This feature is available only on Windows and macOS.
If PyCharm does not detect the poetry executable, click Install poetry via pip to allow PyCharm to install poetry for you automatically.
Alternatively, click Select path and choose the required file manually or specify the following path in the dialog, replacing
jetbrains
with your username:macOSWindowsLinux/Users/jetbrains/Library/Application Support/pypoetry/venv/bin/poetry
C:\Users\jetbrains\AppData\Roaming\pypoetry\venv\Scripts\poetry.exe
/home/jetbrains/.local/bin/poetry
To create a virtual environment within the project directory, select the Create an in-project environment checkbox.
Select the base interpreter from the list, or click and find the Python executable in your file system.
note
If you do not have the Python version in your system, you can download and install it.
This feature is available only on Windows and macOS.
Normally, PyCharm will detect uv installation.
Otherwise, specify the location of the uv executable, or click to browse for it.
condaOther environmentsTo reuse an existing conda environment:
Switch Type to Conda.
Normally, PyCharm will detect conda installation.
Otherwise, specify the location of the conda executable, or click to browse for it.
Select the environment from the list. If you specified the path to conda manually, you may need to reload environments.
To reuse a Virtualenv, Pipenv, or Poetry environment:
Switch Type to Python.
Select the Python executable from the list or click to browse for it.
Click Create.
PyCharm creates a Python environment (or reuses the existing one) and installs the fastapi and uvicorn packages. It also adds the main.py and test_main.http files with some default code constructs.
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.
You can use context-aware code completion to speed up the coding process. Start typing a code construct and the completion popup appears. You can also press CtrlSpace to show available items.
Class completionMethod and function completion
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. Click in the Run widget to launch your application.
The target application is executed in the Run tool window. You can click the link to preview the application in a browser.
You can modify the default run/debug configuration created with the project. Select Edit configurations... from the list of the available 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
).For more information about working with run/debug configurations, refer to Run/debug configurations.
You can also execute the .http file to test GET requests. You can run each request separately, or you can click on the .http file toolbar to test all requests at once.
Select an option to execute requests without an environment or create an environment in either a public or a private file.
PyCharm executes GET tests in the Services tool window. You can select a particular request to preview the summary of its execution status.
Lean more about HTTP requests in Execute HTTP requests.
note
Make sure the Endpoints plugin is enabled in the settings
You can preview, modify, create, and test endpoints of your FastAPI application.
Select View | Tool Windows | Endpoints to open the Endpoints tool window.
Alternatively, click More tool windows on the left, and then select Endpoints.
The Endpoints tool window contains the list of endpoints defined in the application.
The dedicated pane shows the details of the selected endpoint including the documentation, the generated HTTP requests, examples, and the generated OpenAPI specification.
Double-click any endpoint in the list to navigate to its declaration. You can also right-click any item in the list to open the context menu.
Code completion
PyCharm completes endpoint names as you are typing them in Python files, HTTP clients, and other project files.
Live templates
With the live templates available for HTTP clients, you can quickly modify request methods, URLs, and variables.
Refactoring
If you need to rename an endpoint, use the Rename refactoring. Select an endpoint in the editor, press ShiftF6, and type its new name.
Renaming happens in all occurrences across the project.
To open the HTTP client in a separate tab, click Open in Editor on the HTTP Client tab. PyCharm opens a temporary scratch file with the HTTP request. Use in the gutter to run the request.
Click the icon in the gutter to manage an endpoint.
You can find usages, test an endpoint by running the request in the HTTP Client, view all lower-level endpoints, navigate one level up, or copy the URL to the clipboard.
For more information, refer to Endpoints tool window.
Thanks for your feedback!