Python Virtual Environment Setup
Managing the different Python projects
Why is Python virtual environment important?
If you program in Python, it is likely you have to invoke some sort of isolated virtual environment to manage specific project dependencies and Python installations. For example, some projects will need some packages that others do not or even certain versions of it.
In modern coding landscape, it is likely you need it.
Setup a virtual environment with venv.
There are at least 8 different ways to create virtual environments.
Why choose venv?
Well, it is the official one that comes with python. If you are a python developer like me who prefers not to overdo it, then venv is a quick and easy one. On another note, if you are particular about the setting up virtual environments i.e. possibly elegantly, then you might want to take a look at the poetry package library.
Here is a YouTube short on setting up venv.
Setting up a virtual environment
Alternatively, learn from W3Schools’s Python Virtual Environment tutorial. Or FastAPI virtual environment tutorial.
On Windows, python and python3 may be launched with py instead. Run the following in the project folder to setup a chosen virtual environment project name of your choice i.e. myenv in below case, type and enter the following line in the prompt or shell:
py -m venv .myenv
To activate, type:
.\.myenv\Scripts\activate
Once you are done and want to quit: deactivate
Note: I use PowerShell, other terminals may use source .\.myenv\Scripts\activate
There are also 2 ways to know if virtual environment is activated.

Get-Command python will reveal the python.exe file in the Scripts directory. Which python for other terminals.If you list the myenv\Scripts directory, you will also see pip.exe, pip3.exe, python.exe. By the way, navigation around folders and files in the terminal or command prompt is much fast with the Tab key for completions.
What is pip?
After setting up a virtual environment, pip is located within. Pip is the Python package manager. Why do we need this? Well, say if we have a project in a virtual environment, pip takes care of all the packages that are installed for that project. It is indispensable really unless your Python does not need to install any additional packages at all.
By the way, pip and pip3 refers to the same thing (soft-linked).
To upgrade pip in Windows: python.exe -m pip install --upgrade pip
Some pip commands that can come in useful for developers:
pip list
pip install package
pip uninstall package
pip freeze > requirements.txt
pip install --requirements requirements.txt
pip install --upgrade package
pip show pip
With pip, useful packages can be installed i.e. tldr. This is help pages for commands. Usually they list the commonly used commands for certain commands without overwhelming the users. For example, tldr pip. Install it with: pip install tldr.
Is there a simple way to deal with python virtual environment?
Yes. In VSCode, virtual environment can be created with Ctrl+Shift+P → Python: Create Environment command. Every time you open that project in VSCode, the virtual environment can be activated automatically.


