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.
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 the following options:
Specify a path to the Python executable (in case of non-standard installation)
Download and install the latest Python versions from python.org
Install Python using the Command-Line Developer Tools (macOS only).
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 Add Interpreter and choose the interpreter. See Configure a Python interpreter for details.
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 Ctrl+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 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.
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.