Create Virtual environment and install libraries for Python
Learn library management in python using Virtual environment, an alternate to Conda. Learn how it works, with example.
┌───(.venv)-[D:\Programming\TESTING]
└─[$] python
Python 3.13.12
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Welcome to Python venv")
Welcome to Python venvfor Linux OS : substitute
pythonbypython3
Create Virtual environment
- check python version :
python --version - create a virtual environment named .venv :
python -m venv .venv
In Linux, we create a hidden file/folder using
.prefix. For example,.venv
- activate virtual env :
.venv\Scripts\activate(Windows),.venv/bin/activate(Linux) - deactivate :
deactivate - check python location :
where.exe python(Windows),which python3(Linux).
Example
See the workflow in PowerShell. (Same workflow for Linux)
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
┌───[D:\Programming\TESTING]
└─[$] python --version
Python 3.13.12
┌───[D:\Programming\TESTING]
└─[$] where.exe python
C:\Users\photon\AppData\Local\Microsoft\WindowsApps\python.exe <--- Global Python location
┌───[D:\Programming\TESTING]
└─[$] python -m venv .venv
┌───[D:\Programming\TESTING]
└─[$] .venv\Scripts\activate
┌───(.venv)-[D:\Programming\TESTING]
└─[$] python --version
Python 3.13.12
┌───(.venv)-[D:\Programming\TESTING]
└─[$] where.exe python <--- detect two python
D:\Programming\TESTING\.venv\Scripts\python.exe <--- Python in Venv
C:\Users\photon\AppData\Local\Microsoft\WindowsApps\python.exe
┌───(.venv)-[D:\Programming\TESTING]
└─[$] python -m pip --version
pip 25.3 from D:\Programming\TESTING\.venv\Lib\site-packages\pip (python 3.13)
┌───(.venv)-[D:\Programming\TESTING]
└─[$] deactivate
┌───[D:\Programming\TESTING]
└─[$] where.exe python
C:\Users\photon\AppData\Local\Microsoft\WindowsApps\python.exe
┌───[D:\Programming\TESTING]
└─[$]
Install pip and packages
- check version :
python -m pip --version - install pip :
python -m pip install --upgrade pip - install single package NumPy via :
python -m pip install numpy - for multiple packages create requirements.txt :
vim requirements.txt - paste (p) inside requirements.txt :
numpyGet Photon’s stories in your inbox
Join Medium for free to get updates from this writer.
matplotlib
scipy
pandas
requests
- install packages inside requirements.txt :
pip install -r requirements.txt - uninstall all :
pip uninstall -r requirements.txt -y - details of package :
pip show numpy - list of packages installed :
pip list - identifying outdated packages:
pip list --outdated
Example
See how venv affects the work flow. Lets install the NumPy library for our project.
┌───[D:\Programming\TESTING]
└─[$] .venv\Scripts\activate
┌───(.venv)-[D:\Programming\TESTING]
└─[$] python -m pip install numpy
Collecting numpy
Using cached numpy-2.4.4-cp313-cp313-win_amd64.whl.metadata (6.6 kB)
Using cached numpy-2.4.4-cp313-cp313-win_amd64.whl (12.3 MB)
Installing collected packages: numpy
┌───(.venv)-[D:\Programming\TESTING]
└─[$] python
Python 3.13.12
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> x = np.array([1,2,3])
>>> np.sum(x)
np.int64(6)
>>> quit
┌───(.venv)-[D:\Programming\TESTING]
└─[$]What happens if you don’t activate venv ?
┌───[D:\Programming\TESTING]
└─[$] python
Python 3.13.12
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "<python-input-0>", line 1, in <module>
import numpy
ModuleNotFoundError: No module named 'numpy'
>>> quit
┌───[D:\Programming\TESTING]
└─[$]It gives ModuleNotFoundError, since NumPy is not globally installed (inside c:\users\photon\appdata\local\packages\pythonsoftwarefoundation.python.3.13_qbz5n2kfra8p0\localcache\local-packages\python313\site-packages).
Update and Upgrade
- update pip :
python -m pip install --upgrade pip - upgrade all packages:
pip install pip-upgrade-allthen executepip-upgrade-all
Notes
Its better to keep the project inside the parent directory where \.venv is installed (say, D:\Programming\TESTING\Project ). The file structure looks like the following.
D:\Programming\TESTING
│
├── .venv <------ Do not alter this folder
| ├── Include
| ├── Lib
| ├── Script
| | ├── activate
| | └── Activate.ps1
| ├── share
| ├── .gitignore
| └── pyvenv.cfg
│
└── Project
├── BoltzmannMachine.py
└── AutoEncoder.pyIf you use VS Code (and ‘open folder’ D:\Programming\TESTING), it will automatically activate the virtual environment for the project as shown below, you do not have apply .venv\bin\activate.
----- VS Code Terminal ------
┌───[D:\Programming\Python]
└─[$] & d:\Programming\Python\.venv\Scripts\Activate.ps1 <--- Run automatically
┌───(.venv)-[D:\Programming\Python]
└─[$] █ you start from hereReference
For detailed knowledge, refer to https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/.







