← Back to Chapters

Python Virtual Environment

? Python Virtual Environment

⚡ Quick Overview

A virtual environment in Python is an isolated workspace that has its own Python interpreter and installed packages. Each project can have its own environment, so package versions never clash across projects.

This is especially useful when:

  • Different projects need different versions of the same library.
  • You want to keep your system-wide Python clean.
  • You plan to share your project with others using a requirements.txt file.

? Use Cases

  • Building web apps with frameworks like Django or Flask.
  • Working on multiple Python projects with different dependency versions.
  • Creating reproducible environments for deployment or collaboration.

? Key Concepts

  • Environment folder (e.g., myenv): contains its own Python and libraries.
  • Activate: tells your terminal to use the environment’s Python and packages.
  • Deactivate: switches back to the system-wide Python.
  • pip inside venv: installs packages only for that environment.
  • requirements.txt: a file listing all dependencies for the project.

? Commands & Usage

? Creating a Virtual Environment

? View Command
# Create a virtual environment named "myenv"
python -m venv myenv

This creates a folder called myenv containing a fresh Python environment.

▶️ Activating the Virtual Environment

? View Commands for Windows, macOS & Linux
# On Windows (Command Prompt)
myenv\Scripts\activate

# On Windows (PowerShell)
myenv\Scripts\Activate.ps1

# On macOS/Linux
source myenv/bin/activate

After activation, your terminal prompt usually shows the environment name, for example: (myenv) C:\Users\YourName>.

⏹️ Deactivating the Virtual Environment

? View Deactivation Command
# Deactivate the current virtual environment
deactivate

? Installing Packages in a Virtual Environment

? Install and Save Dependencies
# Install packages inside the active virtual environment
pip install requests

# Save all installed packages to requirements.txt
pip freeze > requirements.txt

# Install packages from requirements.txt
pip install -r requirements.txt

? Code Example

Example workflow: create an environment for a project, install a package, and use it in a script.

? Full Example Workflow
# 1) Create and activate a virtual environment
python -m venv myenv
# Activate it (command depends on your OS)

# 2) Install a package inside the environment
pip install requests

# 3) Create a Python file (app.py) and use the package
# app.py
import requests

response = requests.get("https://api.github.com")
print("Status code:", response.status_code)

# 4) Run your script using the environment's Python
python app.py

? Live Output / Explanation

What Happens When You Run This?

  • The venv command creates an isolated folder with its own Python and pip.
  • After activation, any pip install goes into that environment only.
  • pip install requests downloads and installs the requests library inside myenv.
  • Running python app.py uses the environment’s Python and finds requests there.
  • If you deactivate the environment, your system Python will not see those packages unless installed globally.

? Tips & Best Practices

  • Create a new virtual environment for every project.
  • Always activate the environment before installing packages or running project code.
  • Use pip freeze > requirements.txt to save all dependencies for sharing or deployment.
  • Use pip install -r requirements.txt to quickly set up the same environment on another machine.
  • Avoid installing project-specific packages globally on your system Python.

? Try It Yourself / Practice Tasks

  • Create a virtual environment named project_env in a new folder.
  • Activate project_env and install numpy and pandas.
  • Run pip freeze > requirements.txt and inspect the generated file.
  • Delete the environment folder, recreate it, and use pip install -r requirements.txt to restore all packages.
  • Create a small script that prints the version of numpy and verify it works only when the environment is active.