FastAPI
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
From the main menu, choose New Project button in the Welcome screen. New Project dialog opens.
, or click theIn the New Project dialog, do the following:
Specify the project type FastAPI.
Location: Keep the suggested project location or specify an alternative directory.
Next, choose whether you want to create a new environment or use an existing interpreter, by clicking the corresponding radio-button.
Specify the location of the new conda environment in the Location field, or click and browse for the desired location in your file system. The directory for the new conda environment should be empty.
Select the Python version from the list.
Normally, PyCharm will detect conda installation.
Otherwise, specify the location of the conda executable, or click to browse for it.
Select the Make available to all projects checkbox if you want to reuse this environment when creating Python interpreters in PyCharm.
Specify the location of the new virtual environment in the Location field, or click and browse for the desired location in your file system. The directory for the new virtual environment should be empty.
Choose the base interpreter from the list, or click and find the desired Python executable in your file system.
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.
Choose the base interpreter from the list, or click and find the desired Python executable in your file system.
If you have added the base binary directory to your
PATH
environmental variable, you don't need to set any additional options: the path to the pipenv executable will be autodetected.If the pipenv executable is not found, follow the pipenv installation procedure to discover the executable path, and then paste it in the Pipenv executable field.
Choose the base interpreter from the list, or click and find the desired Python executable in your file system.
If PyCharm doesn't detect the poetry executable, specify the following path in the Poetry executable field, replacing
jetbrains
with your username:/Users/jetbrains/Library/Application Support/pypoetry/venv/bin/poetryC:\Users\jetbrains\AppData\Roaming\pypoetry\venv\Scripts\poetry.exe/home/jetbrains/.local/bin/poetryChoose the desired interpreter from the list, or (if the desired interpreter is not found), click Add Interpreter and configure an existing interpreter as described on the following pages:
Once you click Create, PyCharm creates a Python environment and installs the fastapi and uvicorn packages. It also adds the main.py and test_main.http files with some default code constructs.
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.
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 Control+Space to show available items.
Launch and modify run/debug configurations
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 next to the list of configurations to run 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
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
).See Run/debug configurations for more details about working with run/debug configurations.
You can also execute the .http file to test GET requests. You can run each request separately, or you can click the 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.
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.
With the Endpoints plugin, you can easily preview, modify, create, and test endpoints of your FastAPI application.
Manage Endpoints
Press Control+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.
Select Endpoints tool window.
to open theThe Endpoints tool window is shown in the lower group of the PyCharm tool windows. Its left pane lists the endpoints defined in the application.
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.
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.
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 Shift+F6, and type its new name.
Renaming happens in all occurrences across the project.
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 () in the gutter to test it.