Skip to content

QusaiALBahri/python-venv-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Virtual Environment (venv) Tutorial

A comprehensive guide to creating and using Python virtual environments. This tutorial covers everything from setup to managing dependencies.

Table of Contents

  1. What is a Virtual Environment?
  2. Why Use Virtual Environments?
  3. Prerequisites
  4. Creating a Virtual Environment
  5. Activating the Virtual Environment
  6. Installing Packages
  7. Managing Dependencies
  8. Deactivating the Virtual Environment
  9. Common Commands
  10. Best Practices
  11. Example Projects
  12. Troubleshooting

What is a Virtual Environment?

A virtual environment is an isolated Python environment on your machine where you can install packages and dependencies without affecting your system-wide Python installation. Each virtual environment has its own Python interpreter and set of libraries.

Key Benefits

  • Isolation: Package versions in one project don't affect other projects
  • Consistency: Ensures your project works the same on different machines
  • Clean System: Keeps your system Python clean and uncluttered
  • Easy Cleanup: Simply delete the folder to remove everything

Why Use Virtual Environments?

Imagine you have two projects:

  • Project A requires Django 3.0
  • Project B requires Django 4.0

Without virtual environments, you can't have both versions installed simultaneously. Virtual environments solve this by creating isolated spaces for each project.


Prerequisites

You'll need:

  • Python 3.3+ installed on your system
  • Terminal/Command Prompt access
  • Basic command-line knowledge

Check Your Python Version

python --version

Or on some systems:

python3 --version

Creating a Virtual Environment

On Windows

python -m venv venv

On macOS/Linux

python3 -m venv venv

Explanation:

  • python -m venv: Runs the venv module
  • venv: The name of your virtual environment folder (you can use any name)

What Gets Created?

The command creates a folder structure like this:

venv/
├── Include/
├── Lib/
├── Scripts/          (Windows) or bin/ (macOS/Linux)
└── pyvenv.cfg

Activating the Virtual Environment

Windows (Command Prompt)

venv\Scripts\activate

Windows (PowerShell)

venv\Scripts\Activate.ps1

Note: If you get an execution policy error on PowerShell, run:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

macOS/Linux

source venv/bin/activate

How to Know It's Activated

Your terminal prompt will change:

(venv) C:\Users\yourname\project>

The (venv) prefix indicates your virtual environment is active.


Installing Packages

Once your virtual environment is activated, install packages using pip:

pip install package-name

Examples

Install single package:

pip install requests

Install specific version:

pip install Django==4.0

Install multiple packages:

pip install requests Flask numpy

Verify Installation

pip list

This shows all installed packages in your virtual environment.


Managing Dependencies

Creating a Requirements File

Save all your project dependencies to a file:

pip freeze > requirements.txt

The requirements.txt Format

Django==4.0
requests==2.28.0
numpy==1.23.0
pandas==1.4.0

Installing from requirements.txt

When setting up the project on another machine:

pip install -r requirements.txt

This ensures everyone uses the exact same versions.


Deactivating the Virtual Environment

Simply type:

deactivate

Your terminal prompt will return to normal, and you'll use the system Python again.

C:\Users\yourname\project>

Common Commands

Command Description
python -m venv venv Create a virtual environment
venv\Scripts\activate (Windows) Activate on Windows
source venv/bin/activate (Mac/Linux) Activate on Mac/Linux
deactivate Deactivate virtual environment
pip install package Install a package
pip install -r requirements.txt Install from requirements file
pip list List installed packages
pip freeze > requirements.txt Export dependencies
pip uninstall package Remove a package
pip show package Show package info

Best Practices

1. Always Use Virtual Environments

Every Python project should have its own virtual environment.

2. Add venv to .gitignore

Never commit the venv/ folder to version control:

# .gitignore
venv/
__pycache__/
*.pyc
*.egg-info/

3. Use requirements.txt

Always track dependencies:

pip freeze > requirements.txt

4. Activate Before Working

Always activate your virtual environment before installing packages or running code.

5. Use Descriptive Names

While you can name it anything, venv or env are common conventions.

6. Document Python Version

Specify the Python version needed in your README:

Python 3.9.0 or higher

Example Projects

This repository includes example projects in the examples/ directory:

Example 1: Simple Script (hello_world.py)

A basic script demonstrating package usage.

Example 2: Web App (web_app.py)

A Flask web application example.

Example 3: Data Analysis (data_analysis.py)

Using pandas and numpy for data processing.


Troubleshooting

Issue: "python command not found"

Solution:

  • On macOS/Linux, try python3 instead of python
  • Ensure Python is in your system PATH
  • Check installation: python --version

Issue: "Scripts/activate is not recognized"

Solution:

  • Ensure you're in the correct directory
  • Use the full command with correct path
  • Try python -m venv --help for more info

Issue: Packages appear to be installed but can't import them

Solution:

  • Verify virtual environment is activated (look for (venv) in prompt)
  • Use pip list to confirm installation
  • Try restarting your terminal

Issue: Can't activate on Windows PowerShell

Solution:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Issue: want to remove virtual environment

Solution: Simply delete the venv/ folder:

# Windows
rmdir /s venv

# macOS/Linux
rm -rf venv

Additional Resources


Contributing

Contributions are welcome! Feel free to:

  • Report issues
  • Suggest improvements
  • Add more examples
  • Fix typos

License

This tutorial is provided as-is for educational purposes.


Happy Coding! 🐍

About

A comprehensive guide to Python virtual environments with examples

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors