Python Virtual Environments
Python Virtual Environments:
- Isolated python environment for development where you can have your own packages without disturbing other projects or applications as these packages or settings are native to your isolated development environment.
- It means you can do development for webpage through FastAPI in one environment, A.I in another environment and Internet of Things on another environment.
- It helps to make all applications independent and non interfering with each other.
Installation of Dependencies:
- Pip is the Python package manager.
- Pip is used to install and update packages.
- You will want to make sure you have the latest version of pip installed.
- commands
- python -m pip --version -> will provide version of pip present with your python version in your machine.
Setting up virtual environment:
- Create a brand new folder for your project as empty location.
- check pip for dependencies already installed.
- Use python as it already have dependencies to use/create virtual environments called venv.
- Create new FastAPI environment as a virtual environment.
- Activating our virtual environment to install or dependencies in.
Commands and Virtual environment setup for Fast API:(Windows steps)
step 1:
C:\Users\punee>pip list
Package Version
----------------- --------
annotated-types 0.7.0
anyio 4.9.0
fastapi 0.115.12
idna 3.10
mangum 0.19.0
pip 25.0.1
pydantic 2.10.6
pydantic_core 2.27.2
sniffio 1.3.1
starlette 0.46.1
typing_extensions 4.12.2
step 2:
C:\Users\punee\OneDrive\Desktop>mkdir virtualenvFastapi
step 3:
C:\Users\punee\OneDrive\Desktop>cd virtualenvFastapi
step 4:
C:\Users\punee\OneDrive\Desktop\virtualenvFastapi>python -m venv fastapienv
It will create the virtual ennvironment directory with necessary files with name of directory as fastapienv.
step 5:
C:\Users\punee\OneDrive\Desktop\virtualenvFastapi>fastapienv\Scripts\activate.bat
It will activate the virtual environment over the empty folder where you have created the virtual environment.
Verification of activation:
(fastapienv) C:\Users\punee\OneDrive\Desktop\virtualenvFastapi>
As in your directory structure you can see at start their is fastapienv written, it is proof of activation.
step 6:
(fastapienv) C:\Users\punee\OneDrive\Desktop\virtualenvFastapi>pip list
Package Version
------- -------
pip 24.3.1
It will show list of tools installed in virtual environment.
step 7:
Install fastapi and "uvicorn[standard]" in virtual environment.
or use : pip install -r .\requirement.txt
(fastapienv) C:\Users\punee\OneDrive\Desktop\virtualenvFastapi>pip install fastapi
It will install fastapi packages in your python local venv as library packages.
To verify installation.
(fastapienv) C:\Users\punee\OneDrive\Desktop\virtualenvFastapi>pip list
Package Version
----------------- --------
annotated-types 0.7.0
anyio 4.9.0
fastapi 0.115.12
idna 3.10
pip 25.1.1
pydantic 2.11.5
pydantic_core 2.33.2
sniffio 1.3.1
starlette 0.46.2
typing_extensions 4.13.2
typing-inspection 0.4.1
(fastapienv) C:\Users\punee\OneDrive\Desktop\virtualenvFastapi>pip install "uvicorn[standard]"
To verify installation.
(fastapienv) C:\Users\punee\OneDrive\Desktop\virtualenvFastapi>pip list
Package Version
----------------- --------
annotated-types 0.7.0
anyio 4.9.0
click 8.2.1
colorama 0.4.6
fastapi 0.115.12
h11 0.16.0
httptools 0.6.4
idna 3.10
pip 25.1.1
pydantic 2.11.5
pydantic_core 2.33.2
python-dotenv 1.1.0
PyYAML 6.0.2
sniffio 1.3.1
starlette 0.46.2
typing_extensions 4.13.2
typing-inspection 0.4.1
uvicorn 0.34.2
watchfiles 1.0.5
websockets 15.0.1
step 8: to deactivate the virtual environment use.
(fastapienv) C:\Users\punee\OneDrive\Desktop\virtualenvFastapi>deactivate
Validation step:
C:\Users\punee\OneDrive\Desktop\virtualenvFastapi>pip list
Package Version
----------------- --------
annotated-types 0.7.0
anyio 4.9.0
fastapi 0.115.12
idna 3.10
mangum 0.19.0
pip 25.0.1
pydantic 2.10.6
pydantic_core 2.27.2
sniffio 1.3.1
starlette 0.46.1
typing_extensions 4.12.2
C:\Users\punee\OneDrive\Desktop\virtualenvFastapi>
You will not be able to see uvicorn installed in your global packages and others which you have installed only in your virtual environment.
If you are having issue with power-shell terminal in windows (VSCode) and not able to execute activate command then follow below commands they will help:
PS C:\Users\punee\OneDrive\Desktop\virtualenvFastapi> GET-ExecutionPolicy
Restricted
PS C:\Users\punee\OneDrive\Desktop\virtualenvFastapi> GET-ExecutionPolicy -List
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine Undefined
PS C:\Users\punee\OneDrive\Desktop\virtualenvFastapi> SET-ExecutionPolicy RemoteSigned -Scope CurrentUser
PS C:\Users\punee\OneDrive\Desktop\virtualenvFastapi> GET-ExecutionPolicy
RemoteSigned
PS C:\Users\punee\OneDrive\Desktop\virtualenvFastapi> .\fastapienv\Scripts\activate
(fastapienv) PS C:\Users\punee\OneDrive\Desktop\virtualenvFastapi> deactivate
PS C:\Users\punee\OneDrive\Desktop\virtualenvFastapi>
Comments
Post a Comment