Hello World on FastApi Install Packages | 1

FastAPI is a web framework used to build APIs with Python and allows the use of ASGI (Asynchronous Server Gateway Interface) type servers.

Virtual environment

In Python, virtual environments are development environments isolated from the operating system, therefore, the packages that we install will not be installed in the operating system but in the virtual environment; we can create as many virtual environments as we want, usually we have one virtual environment per project and with this, all the packages and dependencies necessary for a Python project in general are managed independently.

In Python, virtual environments can be created and managed with the venv tool, which is included in the Python standard library, although it is also possible to install it using:

$ pip install virtualenv

This is the only package that needs to be installed at the global level of the operating system.

To create the virtual space, we have the command:

$ python3 -m venv venv

We are going to execute the following command:

$ python3 -m venv vtasks

Which will generate the virtual space, which is nothing more than a folder with several files and folders; to activate it, from the virtual space folder:

$ cd vtasks

We run on MacOS or Linux:

$ source vtasks/bin/activate

On Windows the command would be like:

$ vtasks\Scripts\activate

And with this, we already have everything ready to indicate our project; remember that to execute any Python or pip command you must always have the virtual space active.

Install dependencies and prepare the project

For our first project, we are going to create a task app; to do this, inside the virtual environment folder, we will create the project folder that we will simply call tasks; leaving the project as follows:

  • vtasks
    • include
    • lib
    • Scripts -- On Windows
    • bin -- On MacOS or Linux
    • pyvenv.cfg
    • tasks

This folder called "tasks " (not the vtasks folder, which corresponds to the virtual environment) is the one that I recommend you open in Visual Studio Code (or the editor of your choice); from the VSC terminal remember to activate the virtual environment:

In order to start working with FastApi, we need to install two packages that we will see next.

FastApi

FastAPI is a modern web framework used for creating APIs (application programming interfaces) in Python; to install the package we have:

$ pip install fastapi

Uvicorn

Unicorn is an ASGI (Asynchronous Server Gateway Interface) HTTP web server that we can use in multiple asynchronous Python web frameworks such as Django, Flask and of course FastApi and to install the server we can do it as if it were a package more for Python with:

$ pip install uvicorn

Or we can install both with a single command:

$ pip install fastapi uvicorn

Remember to run the above command with the virtual environment active.

- Andrés Cruz

En español

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.