Back to Python
2026-01-106 min read

What is Virtual Environment in Python?

Learn What is Virtual Environment in Python? step by step with clear examples and exercises.

Why This Matters

The significance of a virtual environment in Python cannot be overstated, as it plays a crucial role in managing dependencies and isolating projects from each other. With an extensive library ecosystem, Python's virtual environments become essential for ensuring consistency across different machines, avoiding conflicts between packages, and maintaining efficient dependency management.

In large-scale projects or collaborative work, using virtual environments helps maintain a consistent environment for all developers, reducing the chances of compatibility issues arising from different versions of packages being used. Furthermore, virtual environments allow you to test your code in various isolated environments, ensuring that it works as expected under multiple conditions.

Prerequisites

Before diving into the core concept of virtual environments in Python, it is assumed that you have a basic understanding of Python fundamentals such as:

  • Installing packages using pip
  • Understanding the Python environment
  • Working with command-line interfaces

If you're new to these topics, consider checking out our previous lessons on Python installation, packages, and environments.

Core Concept

A virtual environment in Python is an isolated space that contains all the necessary dependencies for a specific project. By creating separate virtual environments for each project, developers can ensure consistency across different machines, avoid conflicts between packages, and manage dependencies more efficiently.

When you install a package globally on your system, it becomes available for every Python project. However, this can lead to potential issues when multiple projects require different versions of the same package, causing compatibility problems. To prevent such issues, virtual environments are created to isolate each project's dependencies.

Creating a Virtual Environment with venv

Python comes with a built-in module called venv for creating virtual environments. Here's how you can create and activate one:

Create a new virtual environment named my_project

python3 -m venv my_project

Navigate into the virtual environment

cd my_project

Activate the virtual environment (on Unix-based systems)

source bin/activate


Once activated, your command prompt will change to indicate that you're now working within the virtual environment. From here, you can install packages using `pip`, and they will only be available within this isolated environment.

### Creating a Virtual Environment with pipenv

Another popular tool for managing Python projects is `pipenv`. It not only creates a virtual environment but also handles package installation, lockfile management, and script running. Here's how to create and use a project with `pipenv`:

Install pipenv globally (only once)

pip install pipenv

Navigate into your project directory

cd my_project

Create a new virtual environment and initialize it as a pipenv project

pipenv install


After running the above commands, `pipenv` will create a virtual environment, install the specified packages in the `Pipfile`, and set up the necessary scripts. To activate the virtual environment and run your project, use:

Activate the virtual environment (on Unix-based systems)

pipenv shell

Worked Example

Let's consider two projects, project_a and project_b, that both require different versions of the requests package. If you install requests globally, it can cause conflicts between the two projects. However, by creating separate virtual environments for each project and installing the required version of requests within each environment, you can avoid such issues:

Create a virtual environment for project_a

python3 -m venv project_a

cd project_a

source bin/activate

pip install requests==2.25.1 # Install the required version of requests

Create a virtual environment for project_b

python3 -m venv project_b

cd project_b

source bin/activate

pip install requests==2.27.0 # Install a different version of requests


Now, when you run `project_a` and `project_b`, they will use their respective isolated versions of the `requests` package, ensuring compatibility between them.

Common Mistakes

  1. Not activating the virtual environment: Always activate your virtual environment before installing packages or running your project to ensure that the dependencies are isolated from the global Python environment.
  2. Installing packages globally within a virtual environment: Avoid installing packages globally while inside a virtual environment, as it can lead to conflicts with other projects' dependencies.
  3. Not using a virtual environment at all: Failing to use a virtual environment can result in dependency conflicts and inconsistencies between different machines or environments.
  4. Ignoring the need for lockfiles: Using pipenv or similar tools that handle lockfiles is essential for ensuring consistent dependencies across team members and different machines.
  5. Not cleaning up virtual environments: Remember to delete unused virtual environments periodically to avoid cluttering your system with unnecessary isolated spaces.
  6. ### Subheadings under Common Mistakes:
  • Failing to update the lockfile when making changes to package dependencies
  • Forgetting to deactivate a virtual environment before switching to another project or global Python environment
  • Installing packages using pip instead of within the activated virtual environment

Practice Questions

  1. Why should you use a virtual environment in Python?
  2. What is the difference between installing packages globally and within a virtual environment?
  3. How can you create a new virtual environment using venv and activate it on Unix-based systems?
  4. What is pipenv, and what advantages does it offer over using venv alone?
  5. Why is it important to use lockfiles when managing dependencies with tools like pipenv?
  6. ### Subheadings under Practice Questions:
  • Explain the potential issues that can arise from not using a virtual environment in Python projects.
  • Discuss the advantages of isolating project dependencies within a virtual environment.
  • Compare and contrast installing packages globally versus within a virtual environment.
  • Describe how to create a new virtual environment using venv and activate it on Windows systems.
  • Explain the purpose of lockfiles when managing dependencies with tools like pipenv.

FAQ

Q: Can I have multiple virtual environments for the same project on my machine?

A: Yes, you can create multiple virtual environments for the same project on your machine to manage different versions of packages or isolate specific features.

Q: What happens if I deactivate a virtual environment without exiting the terminal?

A: Deactivating a virtual environment while still in the terminal does not affect the environment itself, but it may cause confusion as you'll no longer have the isolated Python interpreter active. To avoid this, make sure to exit the terminal or create a new session before deactivating the virtual environment.

Q: Can I share my virtual environment with other developers on my team?

A: Sharing a virtual environment between team members is not recommended due to potential conflicts and inconsistencies. Instead, consider using tools like pipenv or tox for managing shared dependencies across your team.

Q: Is it necessary to create a new virtual environment for every small project?

A: It's generally a good practice to create a new virtual environment for each project, especially when working with multiple projects that have different dependencies or versions of the same package. This ensures consistency and avoids potential conflicts between projects.

Q: How can I delete an unused virtual environment?

A: To delete an unused virtual environment, navigate to its directory and remove it using rm -rf [virtual_environment_directory]. Be careful when deleting virtual environments to avoid removing important project files by mistake.

  • ### Subheadings under FAQ:
  • How can I find the list of all active virtual environments on my machine?
  • What happens if I accidentally delete a virtual environment that contains important project files?
What is Virtual Environment in Python? | Python | XQA Learn