You can setup FastAPI project in the following ways.
- Using venv
- Using pipenv
- Using pipx and poetry
-
Create virtual environment.
$ python3 -m venv env
-
Activate virtual environment.
$ source env/bin/activate
-
Install dependencies.
$ pip install -r requirements.txt
-
To freeze the dependecnies to requirement.txt.
$ pip freeze > requirement.txt
-
Install pipenv.
$ pip3 install pipenv
-
Create pipenv shell.
$ pipenv shell
This will create a virtual environment in your C:\Users<user-name>.virtualenvs\ folder. For example, -Virtualenv location: C:\Users\Admin\.virtualenvs\fastapi-exp-2-Foeb76tt
. This will create -
Install fastapi.
$ pipenv install fastapi
-
Install uvicorn.
$ pipenv install uvicorn
-
To install dev dependencies.
$ pipenv install pytest --dev
-
To lock environment for dependencies.
$ pipenv lock
-
To install from dependencies from lock file.
$ pipenv install --ignore-pipfile
-
Start uvicorn server.
$ uvicorn main:app --reload
-
In case you want to remove the created virtual environment, run the following
$ pipenv --rm
For further pipenv information, please refer to https://realpython.com/pipenv-guide/.
-
Install pipx (On windows). For other operating systems, please visit https://pypa.github.io/pipx/.
$ pip install --user pipx
-
Install poetry.
$ pipx install poetry
Check the installation using$ poetry --version
. You can update the poetry version using poetry itself like$ poetry self update
or using pip as$ pip install --upgrade poetry
. -
Update PATH variables. Go to
C:\Users\<Username>\.local\bin
and runpipx ensurepath
. Close the terminal and open again. -
Initialize a new project with poetry as below.
$ poetry new <project-name>
Or if the project folder already exists, generate the pyproject.toml interactively using poetry using$ poetry init
. Specify the dependencies during the whole process. -
To add a new package, run
$ poetry add <package-name>
-
To add a new package as a dev dependency, run
$ poetry add <package-name> --dev
-
To remove a package, run
$ poetry remove <package-name>
-
To update poetry.lock from project.toml, run
$ poetry update
This will update/create poetry.lock -
To install from poetry.lock, run
$ poetry install
-
To list all installed packages, run
$ poetry list
-
To export poetry packages to requirements.txt from poetry.lock, run
$ poetry export --output requirements.txt
We are using poetry to setup order management service.
For any new package installation, please use
$ poetry add <package-name>
$ uvicorn main:app --port 12345
- Select debugging button on the left most toolbar.
- Select 'Launch Server' from drop down menu.
- Click on 'Start Debugging' button or press F5.
- Add breakpoints in code wherever needed.
To run the tests from command line, run the following
$ pytest
- Select 'Test' button on the left most toolbar.
- All the tests in the code will be hierarchically displayed.
- Run all the tests in 'Run' Mode or in 'Debug' mode. In debug mode, you can add breakpoints in server-code or in test-code.
- Test results will be displayed in the adjacent panel.
- Create a file named
.env
in the root directory of the project. - Copy the content of
.env.example
to.env
file. - Make modifications to the values of the variables as per your need.
- For MySQL database, please install pymysl using
$ pip install pymysql
. - For PostgreSQL database, please install psycopg2 using
$ pip install psycopg2
.