VSCode setup for FastAPI

Snir Orlanczyk
2 min readOct 5, 2023
Photo by Gabriel Heinzer on Unsplash

I personally love VSCode way better than pyCharm, which seems to be the de-facto IDE for Python for a lot of developers, one of the things that it gives are sensible defaults for python that makes it easier to just start working, which is quite nice but you are giving up a flexible UI that has a lot of plugins, and with some simple configuration can be as good pyCharm and better!

Creating a Venv

from https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/#creating-a-virtual-environment

venv allow you to manage separate package installations for different projects. They essentially allow you to create a “virtual” isolated Python installation and install packages into that virtual installation

Having a venv is a very common practice for many languages as different project require different packages in different versions, managing it all on a global level is a recipe for disaster.

A venv can be created with the binaries of a specific python version, which can allow you to run different versions of python regardless of the system version

# To install a venv in your project, go to your project directory and run the following
python3 -m venv .venv

# This command will create a .venv folder which will contain the python
# binaries and packages for you project

# to install a specifc python version run the following
virtualenv .venv --python=python3.9

#now the venv needs to be activated, to do so run
source .venv/bin/activate

Setting up a debugger

To setup a debugger which is setup to run your FastAPI app add the following file at .vscode/launch.json

# .vscode/launch.json
# assuming that the path to your FastAPI app file is at src/app/main.py.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "src.app.main",
"env": { "PYTHONPATH": "${workspaceFolder}/src/" }
}
]
}

Now when you go to the debugger there will an option to start the server with a debugger attached to it, which will allow the use of breakpoints.

Setting up default settings

# .vscode/settings.json
{
"[python]": {
"editor.defaultFormatter": "ms-python.autopep8"
},
"python.testing.pytestArgs": ["."],
"python.testing.pytestEnabled": true,
"python.terminal.activateEnvironment": true
}

These settings will

  • setup pytest as the testing framework
  • Attempt to discover all test files, and will allow you to run tests from the tests tab
  • will auto activate the venv in the VSCode terminal
  • Will setup a autopep8 as a formatter (to make it work you need to install the autopep8 extension)

Settings up common extension for you team

To make sure you are all using the same tool you can add the extensions you are using to .vscode/extensions.json

# .vscode/extensions.json
{
"recommendations": ["ms-python.autopep8"]
}

Hope you enjoyed!

--

--

Snir Orlanczyk

iOS developer by day, iOS developer by night (one does not simply stop developing an app)