Back to Python
2026-03-165 min read

Creation of Virtual Environments in Python using venv

Learn Creation of Virtual Environments in Python using venv step by step with clear examples and exercises.

Title: Creating Virtual Environments in Python using venv - A full guide

Why This Matters

In software development, it's crucial to maintain a clean and isolated environment for your projects. This is where virtual environments come into play, ensuring that the packages you install for one project don't interfere with others. In this lesson, we will explore how to create and manage Python virtual environments using venv.

Virtual environments are essential when working on multiple projects that may have conflicting dependencies or versions of libraries. By isolating each project into its own environment, you can avoid potential issues caused by package conflicts. Additionally, virtual environments help in managing the dependencies between packages more efficiently.

Prerequisites

Before diving into creating virtual environments, make sure you have a good understanding of:

  • Basic Python syntax
  • Installing Python packages using pip
  • Navigating the command line or terminal (Linux/macOS) or Command Prompt (Windows)
  • Familiarize yourself with Python packages and their dependencies. You can learn more about this topic in our lesson on Managing Python Packages with pip

Understanding Python Versions and Packages

Understanding the different versions of Python and their associated packages is important when working with virtual environments. It helps you avoid compatibility issues and ensures that your projects run smoothly across various systems.

Core Concept

What is venv?

venv is a built-in module in Python 3.3 and later versions that helps you create isolated Python environments on your system. Each environment has its own copy of Python, packages, and site-packages directory, ensuring no conflicts between different projects.

Creating a Virtual Environment

To create a virtual environment, navigate to the project directory in your terminal and run:

python3 -m venv my_env

Replace my_env with the name you'd like for your virtual environment. This command creates a new folder named my_env containing the necessary files to run a Python interpreter in an isolated environment.

Activating the Virtual Environment

To activate the virtual environment, navigate into the created folder:

cd my_env
source bin/activate

On Windows, use:

.\Scripts\activate

Once activated, your terminal prompt will change to show the name of the active virtual environment. Now, when you install or upgrade packages using pip, they'll be isolated within this environment.

Installing Packages

To install a package in the activated virtual environment, use:

pip install <package_name>

For example:

pip install requests

Deactivating the Virtual Environment

To deactivate the virtual environment and return to your system's global Python environment, simply type:

deactivate

On Windows, use:

deactivate.ps1

Worked Example

Let's create a new project called my_project, navigate into it, create a virtual environment, install a package, and run a simple script.

  1. Create the project directory:
mkdir my_project && cd my_project
  1. Create a virtual environment and activate it:
python3 -m venv .venv && source .venv/bin/activate

On Windows, use:

python -m venv .venv & .\.venv\Scripts\activate
  1. Install the requests package:
pip install requests
  1. Create a simple Python script that fetches data from an API using requests:
import requests

response = requests.get('https://jsonplaceholder.typicode.com/todos/1')
data = response.json()
print(data)
  1. Save the script as fetch_data.py and run it:
python fetch_data.py

Common Mistakes

1. Forgetting to activate the virtual environment

When working in a project directory with an active virtual environment, make sure to source the activate script before running any pip commands or Python scripts.

2. Installing packages outside the virtual environment

Always activate your virtual environment before installing any packages using pip to ensure they're isolated within that environment.

3. Not specifying the interpreter when running the script

When running a Python script from outside the activated virtual environment, use the source command followed by the path to the activate script and then run the script:

source .venv/bin/activate && python fetch_data.py

On Windows, use:

.\.venv\Scripts\activate && python fetch_data.py

4. Not using a requirements file

It's good practice to maintain a requirements.txt file in your project directory that lists all the necessary packages and their versions. This helps in easily recreating the environment for other developers or machines.

5. Ignoring package conflicts within the virtual environment

Although virtual environments help isolate packages, it's still possible to have conflicting versions of packages within an environment. Use tools like pip freeze and pip install -r requirements.txt to manage and resolve these conflicts. You can learn more about this topic in our lesson on Managing Python Packages with pip.

6. Not deactivating the virtual environment when finished

Always deactivate the virtual environment when you're done working on a project to avoid any unintended changes to the global Python environment.

Practice Questions

  1. What is the purpose of using virtual environments in Python?
  2. How can you create a virtual environment for your project?
  3. How do you activate and deactivate a virtual environment?
  4. Explain how to install a package within an activated virtual environment.
  5. Why should you avoid installing packages outside the virtual environment?
  6. What are some potential issues that can arise when not using virtual environments in Python projects?
  7. How does venv help manage dependencies between packages in a project?
  8. Can you create multiple active virtual environments for different projects at the same time? If so, how do you switch between them?
  9. What happens if you accidentally delete a virtual environment's folder?
  10. Are there any limitations to using venv for managing Python projects and their dependencies?

FAQ

Q: Can I have multiple active virtual environments at the same time?

A: Yes, you can activate multiple virtual environments by navigating into their respective directories and sourcing their activate scripts. However, only one virtual environment will be considered "active" at any given moment. To switch between them, deactivate the current environment and activate a different one.

Q: How do I uninstall a virtual environment?

A: To remove a virtual environment, simply delete its directory from the project folder. The packages installed within that environment will not be affected system-wide.

Q: Can I use venv on Windows?

A: Yes, venv is compatible with both Linux and Windows operating systems. The process for creating and managing virtual environments is similar across platforms. However, there may be slight differences in activating the environment due to differences in command line usage between the two operating systems.

Q: What happens if I have conflicting package versions within my active virtual environment?

A: If you accidentally install packages with conflicting versions within your active virtual environment, you can use tools like pip freeze and pip install -r requirements.txt to manage and resolve these conflicts. You can learn more about this topic in our lesson on Managing Python Packages with pip.

Creation of Virtual Environments in Python using venv | Python | XQA Learn