PYTHON

Python Virtual Environment: Syntax, Usage, and Examples

A Python virtual environment is an isolated directory that allows you to manage dependencies and packages for a specific project without affecting the global Python setup. It provides a clean slate where you can install packages tailored to a particular application, avoiding conflicts between different projects.

Using a virtual environment Python workflow is considered best practice for both beginners and professionals, especially when dealing with different versions of libraries or deploying applications.


Why Use a Python Virtual Environment

Different projects often require different package versions. One project may depend on Flask 1.x, while another might need Flask 2.x. Installing both globally can lead to version conflicts. A Python virtual environment helps solve this problem by creating isolated spaces where each project can have its own set of dependencies.

This is especially useful in collaborative projects, deployment pipelines, and long-term application maintenance.


How to Create Python Virtual Environment

To create a virtual environment in Python, use the built-in venv module:

python -m venv venv_name

This command creates a new folder venv_name containing a local Python interpreter and libraries.

You can also specify a path if you don’t want to use the default name:

python -m venv ./envs/my_project_env

This isolates dependencies for each project, keeping your global Python environment clean.


Activating the Virtual Environment Python Way

Once you've created the environment, you need to activate it:

On Windows:

venv_name\Scripts\activate

On macOS/Linux:

source venv_name/bin/activate

After activation, your terminal prompt changes to show the environment’s name. All installed packages now go into this local directory.


Installing Packages in a Python Virtual Environment

With the environment activated, install packages like this:

pip install requests

This keeps the package confined to the virtual environment. You can verify it by running:

pip list

Only packages installed in that specific environment will appear.


Deactivating the Environment

To return to the global Python context, deactivate the virtual environment:

deactivate

This is a simple but crucial step when switching between projects.


How to Recreate the Environment on Another Machine

Use requirements.txt to make your setup reproducible:

pip freeze > requirements.txt

Then, on another system:

python -m venv new_env
source new_env/bin/activate
pip install -r requirements.txt

This ensures the same versions are used across environments, critical for consistency.


Common Issues and Fixes

  • Virtual Environment Not Activating
    • Make sure you’re using the correct shell.
    • On Windows, use PowerShell or Command Prompt.
    • On macOS/Linux, use bash or zsh.
  • pip Not Found in Environment
    • Try reinstalling with:

      python -m ensurepip
      
  • Wrong Python Version
    • Ensure you use the desired Python interpreter:

      python3.10 -m venv env310
      

Storing Virtual Environments in a Standard Location

You might want to keep all your environments in a single folder like .venvs for easier management:

mkdir ~/.venvs
python -m venv ~/.venvs/project1

Then you can activate them from any project location by sourcing the proper path.


Integrating with IDEs

Many editors like VS Code, PyCharm, or Sublime Text can auto-detect virtual environments. In VS Code:

  1. Press Ctrl+Shift+P.
  2. Select Python: Select Interpreter.
  3. Choose the interpreter from your venv directory.

This ensures your environment and editor are working in sync.


Python Virtual Environment Best Practices

  • Always create a virtual environment for each project.
  • Include requirements.txt in version control.
  • Avoid installing unnecessary packages globally.
  • Use virtual environments in CI/CD pipelines.
  • Use descriptive names for clarity, e.g., venv_api, venv_ui.

Using Virtualenv Instead of venv

While venv is built-in, some developers prefer virtualenv, a third-party tool with additional features and backward compatibility:

pip install virtualenv
virtualenv myenv

It works similarly but can be more flexible in some environments.


A Python virtual environment gives you full control over dependencies and keeps your global Python setup clean. You’ve learned how to create, activate, and manage a virtual environment Python project structure. Whether you're building a small script or deploying a large web app, isolating your packages helps avoid conflicts and improves maintainability.

Learn to Code in Python for Free
Start learning now
button icon
To advance beyond this tutorial and learn Python by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH

Reach your coding goals faster