Back to Python
2026-02-247 min read

Create Virtual Environment (Python Programming)

Learn Create Virtual Environment (Python Programming) step by step with clear examples and exercises.

Why This Matters

Creating a virtual environment is crucial in Python programming for several reasons:

  1. Isolation of Dependencies: A virtual environment allows you to isolate the packages and their versions for each project, preventing conflicts between different projects' dependencies.
  2. Reproducibility: By using virtual environments, you can ensure that your project runs consistently across different machines as it uses the same package versions.
  3. Efficient Collaboration: When working on a team or collaborating with others, virtual environments help maintain a consistent environment for all team members, reducing the chances of conflicts and errors.
  4. Managing Dependencies: Virtual environments make it easier to manage dependencies for each project, as you can install, update, or uninstall packages without affecting other projects.
  5. Experimentation: Virtual environments allow you to experiment with different package versions or configurations without affecting your global Python installation.
  6. Avoiding Global Pollution: By using virtual environments, you prevent your global Python installation from being polluted by unnecessary packages or outdated versions of libraries.
  7. Easier Package Management: Virtual environments make it easier to manage the dependencies of each project separately, making it less likely for conflicts and issues to arise.

Prerequisites

To follow this tutorial, you should have:

  1. Python installed on your system (Python 3.x). You can download it from official Python website or use a package manager like brew (for macOS) or apt-get (for Ubuntu).
  2. Basic understanding of the command line or terminal. Familiarize yourself with navigating directories, running scripts, and installing packages using pip. If you're new to the command line, consider reading this tutorial first.
  3. A text editor or Integrated Development Environment (IDE) for writing Python code. Some popular options include Visual Studio Code, PyCharm, and Jupyter Notebook.
  4. Familiarity with navigating your file system and creating new directories using the command line or terminal.

Core Concept

A virtual environment is a self-contained directory that includes all the necessary Python dependencies for a specific project. It's created using the venv module which comes with Python 3.x.

Creating a Virtual Environment

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

python -m venv my_project_env

Replace my_project_env with the name of your desired virtual environment. This command will create a new directory named my_project_env containing all necessary files to isolate your project's dependencies.

Activating the Virtual Environment

To activate the virtual environment, you need to source the activate script located in the Scripts folder inside the created environment:

  • On Windows:
my_project_env\Scripts\activate
  • On Linux/macOS:
source my_project_env/bin/activate

Once activated, your terminal prompt will change to show the name of the active virtual environment. Now, any Python packages you install using pip will be isolated within this environment.

Installing Packages

To install a package, use pip:

pip install <package_name>

For example, to install the popular library numpy, run:

pip install numpy

Deactivating the Virtual Environment

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

deactivate

Worked Example

Let's create a virtual environment for a project named my_project, install numpy, and verify the installation.

  1. First, navigate to your project directory using the command line:
cd path/to/my_project
  1. Create the virtual environment:
python -m venv my_project_env
  1. Activate the virtual environment:

(On Windows)

my_project_env\Scripts\activate

(On Linux/macOS)

source my_project_env/bin/activate
  1. Install numpy:
pip install numpy
  1. Verify the installation by importing it in a Python script:

Create a new file named main.py and write the following code:

import numpy as np
print(np.__version__)
  1. Run the script from the terminal:
python main.py

You should see the version of numpy installed in your virtual environment printed on the console.

Common Mistakes

  1. Not activating the virtual environment before installing packages: Packages will be installed globally if you don't activate the virtual environment first.
  2. Forgetting to deactivate the virtual environment: Leaving the virtual environment active can lead to confusion and conflicts with other projects.
  3. Using pip instead of pip3 (or vice versa): Make sure you use the correct version of pip for your Python installation.
  4. Not specifying the virtual environment when running scripts: If you run a script outside the activated virtual environment, it may not find the necessary packages. To avoid this, always activate the virtual environment before running scripts or use the --python option with pip to specify the interpreter:
my_project_env\Scripts\python main.py

or

pip install -r requirements.txt --python=path/to/venv/bin/python

Common Mistakes (Continued)

  1. Ignoring package conflicts: Virtual environments help manage package conflicts, but it's essential to be aware of potential conflicts and resolve them when they occur. Use tools like pipdeptree or pipenv for managing dependencies more effectively.
  2. Installing unnecessary packages: Avoid installing unnecessary packages to keep the virtual environment lightweight and efficient. Use tools like pipreqs or pip-audit for generating a requirements.txt file with only the required packages.
  3. Not using requirements.txt: Always create and maintain a requirements.txt file to document your project's dependencies, making it easier for others to recreate your environment or collaborate on your project.
  4. Installing system packages instead of Python packages: Be aware that some packages may be available both as system packages (installed using package managers like apt-get or homebrew) and as Python packages (installed using pip). Always install Python packages to ensure compatibility with other dependencies.
  5. Not updating the virtual environment regularly: Keep your virtual environment up-to-date by periodically running pip freeze > requirements.txt to update the requirements.txt file, and then reinstalling the packages using pip install -r requirements.txt.
  6. Misunderstanding the purpose of the global Python installation: The global Python installation should be used for system-level scripts or utilities that don't require specific dependencies. It is not recommended to use it for project development as it may lead to conflicts and inconsistencies.

Common Mistakes (Continued)

  1. Not using virtualenvwrapper: virtualenvwrapper is a powerful tool that simplifies the process of creating, activating, and managing virtual environments. It provides additional commands for easier management and customization of your virtual environments.
  2. Ignoring package documentation: Always read the documentation for packages to understand their dependencies, usage, and best practices for installation and configuration.

Practice Questions

  1. Create a virtual environment for a project named my_new_project and install the pandas library.
  2. You have multiple projects with conflicting dependencies. Explain how creating separate virtual environments can help manage these dependencies efficiently.
  3. What happens if you forget to deactivate a virtual environment after using it?
  4. Why is it important to activate the virtual environment before installing packages using pip?
  5. How can you run a script located outside the activated virtual environment while ensuring that it uses the correct interpreter and packages from the virtual environment?
  6. What are some tools or strategies for managing dependencies effectively in Python projects?
  7. What is the purpose of a requirements.txt file, and why should you maintain one for your projects?
  8. What are some best practices for using virtual environments in Python projects?
  9. How can you automate the creation of virtual environments for your projects?
  10. What are some common mistakes to avoid when working with virtual environments in Python projects?

FAQ

  1. Can I have multiple virtual environments for different projects in the same directory?

Yes, you can have as many virtual environments as needed within the same project folder. Each will be isolated from the others.

  1. What happens if I delete a virtual environment?

Deleting a virtual environment will remove all its contents, including any installed packages. You'll need to recreate the environment and reinstall the necessary packages if needed.

  1. Can I share my project with someone else who doesn't have the same Python dependencies?

By using virtual environments, you can create a requirements.txt file that lists all the required packages along with their versions. The other person can then recreate your environment by running pip install -r requirements.txt.

  1. Is it necessary to create a virtual environment for every project?

While not strictly necessary, creating a virtual environment is highly recommended for larger projects or when working with multiple dependencies to avoid conflicts and ensure reproducibility. For small projects or scripts without external dependencies, it may be less important.

  1. How can I automate the creation of virtual environments for my projects?

You can use tools like virtualenvwrapper to simplify and automate the process of creating, activating, and managing virtual environments for your projects.

  1. What are some best practices for using virtual environments in Python projects?

Some best practices include:

  • Creating a new virtual environment for each project.
  • Activating the virtual environment before installing packages or running scripts.
  • Maintaining a requirements.txt file and keeping it up-to-date.
  • Using tools like pipdeptree, pipenv, or virtualenvwrapper to manage dependencies effectively.
  • Deactivating the virtual environment when finished to avoid conflicts with other projects.
  • Installing Python packages instead of system packages.
  • Updating the virtual environment regularly to ensure compatibility and security.
  • Reading package documentation for understanding dependencies, usage, and best practices for installation and configuration.
Create Virtual Environment (Python Programming) | Python | XQA Learn