Shivankur Sharma

🐍 Setting Up a Python Virtual Environment


πŸš€ Setup Instructions

cd
mkdir project
cd project

Create a Virtual Environment

python -m venv .venv

Activate the Virtual Environment

Windows

.venv\Scripts\Activate.ps1

Linux/macOS

source .venv/bin/activate

Check the Virtual Environment is Active

Windows

Get-Command python

Linux/macOS

which python

Upgrade pip

python -m pip install --upgrade pip

Add .gitignore

Deactivate the Virtual Environment

deactivate

Notes

Where are packages installed

When you install Python, it creates some directories with some files in your computer.

Some of these directories are the ones in charge of having all the packages you install.

Then it will extract all those files and put them in a directory in your computer.

By default, it will put those files downloaded and extracted in the directory that comes with your Python installation, that’s the global environment.

What are virtual environments

The solution to the problems of having all the packages in the global environment is to use a virtual environment for each project you work on.

A virtual environment is a directory, very similar to the global one, where you can install the packages for a project.

This way, each project will have its own virtual environment (.venv directory) with its own packages.