A virtual environment is an isolated Python environment where you can install packages separately for each project.
It helps you avoid conflicts between different project dependencies.
WHY USE VIRTUAL ENVIRONMENTS?
• Prevent version conflicts
• Keep projects independent
• Maintain clean system Python
• Professional development practice
Example:
Project A needs numpy 1.20
Project B needs numpy 1.26
Virtual environments allow both to work without conflict.
CREATE A VIRTUAL ENVIRONMENT
Open Command Prompt or Terminal:
python -m venv myenv
This creates a folder named myenv containing a separate Python environment.
ACTIVATE VIRTUAL ENVIRONMENT
Windows
myenv\Scripts\activate
Mac / Linux
source myenv/bin/activate
After activation, you will see the environment name in the terminal like:
(myenv)
INSTALL PACKAGES INSIDE VIRTUAL ENVIRONMENT
Once activated:
pip install requests
The package will be installed only inside this environment.
DEACTIVATE VIRTUAL ENVIRONMENT
To exit:
deactivate
CHECK INSTALLED PACKAGES
pip list
Shows packages installed in that specific environment.
SAVE PROJECT DEPENDENCIES
pip freeze > requirements.txt
To install dependencies later:
pip install -r requirements.txt
Very important for sharing projects.
DELETE A VIRTUAL ENVIRONMENT
Simply delete the myenv folder.
COMMON VIRTUAL ENVIRONMENT TOOLS
• venv (built-in)
• virtualenv
• conda
Most beginners use venv.
BEST PRACTICES
• Create one environment per project
• Do not install packages globally
• Always activate before installing packages
• Keep requirements.txt updated
KEY TAKEAWAY
Virtual environments keep your Python projects clean, organized, and conflict-free. They are essential for professional Python development.