What is virtual environment and Introduction to FastAPI

Anand RavalAnand Raval
4 min read

Python API Development with FastAPI

Master Python API development with FastAPI, SQL integration, testing, and cloud deployment throughout blogs. Build robust APIs, automate testing, and deploy seamlessly with CI/CD pipelines. Everything you need to become a pro API developer.

Let’s start from beginner level.

  • Install VS Code editor link For environment where we will write our code.

  • Install Python link.

  • Now open vs code and make one folder called FASTAPI.

  • In that folder make one file called as main.py (.py is extension for code editor which can identify, this is file or code belongs to python).

  • Open …>terminal (Given below image you can see there is powershell change it to cmd).

  • Now we have to make virtual environment for our project.

What is virtual environment

Assume that we are working on 3 project in same machine and different project required different python version and dependency. so that means python virtual environments tool is just providing isolation for each project. it will create isolate space for project you can see image for more understanding.

3 Ways to Set Up Virtual Environments | by Kay Jan Wong | Python in Plain  English

Execute command given in image

  • py -3: This tells the py (Python Launcher for Windows) to use the Python 3 interpreter installed on your system.

  • venv: This is the name of the Python module used for creating virtual environments.

  • venv (the second one): This is the name you are giving to your new virtual environment directory. In this case, it will create a folder named venv in your current location.

py -3 venv venv

After executing that command you will see in code editor new folder called venv is created for isolated environment for this project, you can see in below image.

In venv folder you'll find a set of directories and files that constitute your isolated Python environment.

Now we will use venv environment for project so first of all we have to choose python interpreter for this project which is created earlier within venv.

  • Click on serarch button in python code which is located on top of middle in your screen.

  • Write >python inter.

  • Click on Python: Select Interpreter.

Select Venv (Python 3.13.3) venv\Scripts\python.ext.

Now to activate that virtual environment run activate file which is located in venv>Scripts just execute below command in terminal (use cmd).

In below image you can see (venv) that means you are into virtual environment which we created earlier. Remember this very carefully whenever we use project this project we will work in this virtual environment, every time you have to execute this command when you are not in virtaul environment

venv\Scripts\activate.bat

Now i want to share a fast api documentation website. which will help you throught journey. for this project i am also using this fast api documentation link

Now open your code editor and install fastapi all modules for this project. execute below command in terminal that will download all require packages or modules for this project.

pip install Fastapi[all]

pip freeze #you can see what packages or modules installed in virtual environment

Now run below code in code editor

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}
unicorn main:app #run this command in terminal

uvicorn main:app starts the Uvicorn ASGI server to run a modern, asynchronous Python web application

It loads the application object named app

from the Python module main and makes it accessible over the network.

Open uvicorn running link on browser you will output like below image

That’s it for today guys in next blog we will understand about that code, hope you got all points which i shared you throughout this blog if you have any question or doubts you can free feel to reach out me

Thankyou for reading this blog !!!!!!!!!!

0
Subscribe to my newsletter

Read articles from Anand Raval directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anand Raval
Anand Raval

"I'm a 3rd-year Computer Engineering student at Marwadi University with skills in C++, web development (MERN stack), and DevOps tools like Kubernetes. I contribute to open-source projects and share tech knowledge on GitHub and LinkedIn. I'm learning cloud technologies and app deployment. As an Internshala Student Partner, I help others find jobs and courses." now currently focusing on #90DaysOfDevops