Creating a Virtual Environment Using the venv Module

🏷️ Setting Up Your Python Environment / Virtual Environments

When you start working on Python projects, you'll quickly discover that different projects often need different versions of libraries or packages. Installing everything globally can lead to conflictsβ€”one project might need version 1.0 of a library while another needs version 2.0. This is where virtual environments come to the rescue. A virtual environment is like a clean, isolated sandbox for each of your projects, keeping dependencies separate and your system tidy.


βš™οΈ What Is a Virtual Environment?

A virtual environment is a self-contained directory that contains: - A specific Python interpreter version - Its own set of installed packages - Its own scripts and executables

When you activate a virtual environment, any Python command or package installation you run will only affect that environment, not your global Python installation or other projects.


πŸ› οΈ Why Use venv?

Python's built-in venv module is the recommended way to create virtual environments. Here's why engineers prefer it: - No extra installation needed – It comes with Python 3.3 and later - Lightweight and fast – Creates environments quickly - Cross-platform – Works on Windows, macOS, and Linux - Standard and reliable – Maintained by the Python core team


πŸ“Š Creating Your First Virtual Environment

The process is straightforward. Follow these steps:

Step 1: Navigate to your project folder Open your terminal or command prompt and change to the directory where your project lives.

Step 2: Create the virtual environment Run the command: python -m venv myenv (replace myenv with any name you prefer, such as .venv or env)

Step 3: Check what was created After running the command, you'll see a new folder with the name you chose. Inside it, you'll find: - A bin folder (or Scripts on Windows) containing the Python executable and activation scripts - A lib folder where installed packages will live - A pyvenv.cfg configuration file


πŸš€ Activating and Deactivating the Environment

Once created, you need to activate the environment to start using it:

On macOS/Linux: - Activate with: source myenv/bin/activate - Your terminal prompt will change to show (myenv) at the beginning

On Windows: - Activate with: myenv\Scripts\activate - Your command prompt will show (myenv)

To deactivate (on any platform): - Simply type: deactivate - Your prompt will return to normal


πŸ“¦ Installing Packages Inside the Environment

With your virtual environment activated, any package you install will be isolated:

  • Use: pip install requests to install the requests library
  • Use: pip list to see all installed packages in this environment
  • Use: pip freeze > requirements.txt to save your environment's packages to a file

The requirements.txt file is a simple text file listing all packages and their versions. Other engineers can recreate your exact environment by running: pip install -r requirements.txt


πŸ•΅οΈ Key Differences: Global vs. Virtual Environment

Feature Global Python Virtual Environment
Package location System-wide (e.g., /usr/lib/python) Inside your project folder
Isolation All projects share same packages Each project has its own packages
Version conflicts Common and hard to manage Avoided completely
Cleanup Manual and risky Delete the folder, done
Reproducibility Difficult to replicate Easy with requirements.txt

βœ… Best Practices for Engineers

  • Always create a virtual environment for every new project, no matter how small
  • Name your environment .venv (with a dot) – it's a common convention and many tools recognize it
  • Add the environment folder to .gitignore – never commit your virtual environment to version control
  • Use a requirements.txt file to document your project's dependencies
  • Delete and recreate environments if things get messy – it's safer than trying to fix them

πŸ”„ Common Workflow Example

Here's how a typical session looks for an engineer:

  1. Create a project folder and navigate into it
  2. Run python -m venv .venv to create the environment
  3. Activate it with source .venv/bin/activate (or the Windows equivalent)
  4. Install needed packages with pip install
  5. Save dependencies with pip freeze > requirements.txt
  6. Work on your project
  7. When done, type deactivate to exit the environment

The next time you return to this project, just activate the existing environment and continue working.


⚠️ Troubleshooting Tips

  • "python not found" – Try using python3 instead of python on some Linux/macOS systems
  • Activation script not working – On Windows, you might need to run Set-ExecutionPolicy Unrestricted -Scope Process in PowerShell first
  • Packages installing globally – Make sure you see the environment name in your terminal prompt before installing
  • Environment not found – Check that you're in the correct directory where you created the environment

Virtual environments are a fundamental tool in Python development. Once you start using them, you'll wonder how you ever worked without them. They keep your projects clean, your system organized, and your fellow engineers happy when they can easily reproduce your work.


A virtual environment is an isolated folder that keeps your project's Python packages separate from other projects.


πŸ› οΈ Example 1: Creating a basic virtual environment

This example shows how to create a virtual environment named "myenv" in your current directory.

# Run this command in your terminal, not in Python
python -m venv myenv

πŸ“€ Output: A new folder named "myenv" appears in your current directory


πŸš€ Example 2: Activating the virtual environment on Windows

This example shows how to activate your virtual environment so you can use its isolated Python and packages.

# Run this command in your terminal
myenv\Scripts\activate

πŸ“€ Output: (myenv) appears at the beginning of your terminal prompt


πŸš€ Example 3: Activating the virtual environment on macOS/Linux

This example shows how to activate your virtual environment on Unix-based systems.

# Run this command in your terminal
source myenv/bin/activate

πŸ“€ Output: (myenv) appears at the beginning of your terminal prompt


πŸ“¦ Example 4: Checking which Python interpreter is being used

This example shows how to confirm your virtual environment is active by checking the Python path.

# Run this command in your terminal while the virtual environment is active
which python

πŸ“€ Output: /path/to/your/project/myenv/bin/python


🧹 Example 5: Deactivating the virtual environment

This example shows how to exit your virtual environment and return to the system's default Python.

# Run this command in your terminal while the virtual environment is active
deactivate

πŸ“€ Output: (myenv) disappears from your terminal prompt


πŸ“Š Comparison Table: Virtual Environment States

State Command What Happens
Not created python -m venv myenv Creates isolated folder
Active myenv\Scripts\activate (Windows) or source myenv/bin/activate (macOS/Linux) Terminal shows (myenv)
Inactive deactivate Terminal returns to normal

When you start working on Python projects, you'll quickly discover that different projects often need different versions of libraries or packages. Installing everything globally can lead to conflictsβ€”one project might need version 1.0 of a library while another needs version 2.0. This is where virtual environments come to the rescue. A virtual environment is like a clean, isolated sandbox for each of your projects, keeping dependencies separate and your system tidy.


βš™οΈ What Is a Virtual Environment?

A virtual environment is a self-contained directory that contains: - A specific Python interpreter version - Its own set of installed packages - Its own scripts and executables

When you activate a virtual environment, any Python command or package installation you run will only affect that environment, not your global Python installation or other projects.


πŸ› οΈ Why Use venv?

Python's built-in venv module is the recommended way to create virtual environments. Here's why engineers prefer it: - No extra installation needed – It comes with Python 3.3 and later - Lightweight and fast – Creates environments quickly - Cross-platform – Works on Windows, macOS, and Linux - Standard and reliable – Maintained by the Python core team


πŸ“Š Creating Your First Virtual Environment

The process is straightforward. Follow these steps:

Step 1: Navigate to your project folder Open your terminal or command prompt and change to the directory where your project lives.

Step 2: Create the virtual environment Run the command: python -m venv myenv (replace myenv with any name you prefer, such as .venv or env)

Step 3: Check what was created After running the command, you'll see a new folder with the name you chose. Inside it, you'll find: - A bin folder (or Scripts on Windows) containing the Python executable and activation scripts - A lib folder where installed packages will live - A pyvenv.cfg configuration file


πŸš€ Activating and Deactivating the Environment

Once created, you need to activate the environment to start using it:

On macOS/Linux: - Activate with: source myenv/bin/activate - Your terminal prompt will change to show (myenv) at the beginning

On Windows: - Activate with: myenv\Scripts\activate - Your command prompt will show (myenv)

To deactivate (on any platform): - Simply type: deactivate - Your prompt will return to normal


πŸ“¦ Installing Packages Inside the Environment

With your virtual environment activated, any package you install will be isolated:

  • Use: pip install requests to install the requests library
  • Use: pip list to see all installed packages in this environment
  • Use: pip freeze > requirements.txt to save your environment's packages to a file

The requirements.txt file is a simple text file listing all packages and their versions. Other engineers can recreate your exact environment by running: pip install -r requirements.txt


πŸ•΅οΈ Key Differences: Global vs. Virtual Environment

Feature Global Python Virtual Environment
Package location System-wide (e.g., /usr/lib/python) Inside your project folder
Isolation All projects share same packages Each project has its own packages
Version conflicts Common and hard to manage Avoided completely
Cleanup Manual and risky Delete the folder, done
Reproducibility Difficult to replicate Easy with requirements.txt

βœ… Best Practices for Engineers

  • Always create a virtual environment for every new project, no matter how small
  • Name your environment .venv (with a dot) – it's a common convention and many tools recognize it
  • Add the environment folder to .gitignore – never commit your virtual environment to version control
  • Use a requirements.txt file to document your project's dependencies
  • Delete and recreate environments if things get messy – it's safer than trying to fix them

πŸ”„ Common Workflow Example

Here's how a typical session looks for an engineer:

  1. Create a project folder and navigate into it
  2. Run python -m venv .venv to create the environment
  3. Activate it with source .venv/bin/activate (or the Windows equivalent)
  4. Install needed packages with pip install
  5. Save dependencies with pip freeze > requirements.txt
  6. Work on your project
  7. When done, type deactivate to exit the environment

The next time you return to this project, just activate the existing environment and continue working.


⚠️ Troubleshooting Tips

  • "python not found" – Try using python3 instead of python on some Linux/macOS systems
  • Activation script not working – On Windows, you might need to run Set-ExecutionPolicy Unrestricted -Scope Process in PowerShell first
  • Packages installing globally – Make sure you see the environment name in your terminal prompt before installing
  • Environment not found – Check that you're in the correct directory where you created the environment

Virtual environments are a fundamental tool in Python development. Once you start using them, you'll wonder how you ever worked without them. They keep your projects clean, your system organized, and your fellow engineers happy when they can easily reproduce your work.

Interactive Views

You are currently in πŸ“š All-in-One mode. Use the tabs at the top to switch to πŸ“– Theory Only or πŸ’» Code Only views.

A virtual environment is an isolated folder that keeps your project's Python packages separate from other projects.


πŸ› οΈ Example 1: Creating a basic virtual environment

This example shows how to create a virtual environment named "myenv" in your current directory.

# Run this command in your terminal, not in Python
python -m venv myenv

πŸ“€ Output: A new folder named "myenv" appears in your current directory


πŸš€ Example 2: Activating the virtual environment on Windows

This example shows how to activate your virtual environment so you can use its isolated Python and packages.

# Run this command in your terminal
myenv\Scripts\activate

πŸ“€ Output: (myenv) appears at the beginning of your terminal prompt


πŸš€ Example 3: Activating the virtual environment on macOS/Linux

This example shows how to activate your virtual environment on Unix-based systems.

# Run this command in your terminal
source myenv/bin/activate

πŸ“€ Output: (myenv) appears at the beginning of your terminal prompt


πŸ“¦ Example 4: Checking which Python interpreter is being used

This example shows how to confirm your virtual environment is active by checking the Python path.

# Run this command in your terminal while the virtual environment is active
which python

πŸ“€ Output: /path/to/your/project/myenv/bin/python


🧹 Example 5: Deactivating the virtual environment

This example shows how to exit your virtual environment and return to the system's default Python.

# Run this command in your terminal while the virtual environment is active
deactivate

πŸ“€ Output: (myenv) disappears from your terminal prompt


πŸ“Š Comparison Table: Virtual Environment States

State Command What Happens
Not created python -m venv myenv Creates isolated folder
Active myenv\Scripts\activate (Windows) or source myenv/bin/activate (macOS/Linux) Terminal shows (myenv)
Inactive deactivate Terminal returns to normal