Developing with Python: Why Virtual Environments and Version Management Matter

Developing with Python: Why Virtual Environments and Version Management Matter
Photo by Rubaitul Azad / Unsplash

Python’s versatility is one of the reasons it remains one of the most widely used languages in software development, data science, automation, machine learning, web frameworks, and more. Its extensive ecosystem of libraries makes it easy to build complex applications quickly. However, this same ecosystem can become a challenge without proper dependency and version management.

Two core tools — venv and pyenv — allow developers to isolate project environments and switch seamlessly between Python versions. Developing without them can quickly lead to conflicting package versions, dependency hell, and pip install errors that often stem from mismatched Python versions.

This article explains why these tools are essential and demonstrates how to use them effectively.

The Problem: One Python to Rule Them All (Does Not End Well)

If you install every library globally (system-wide), the system Python environment will accumulate:

  • Different versions of the same packages
  • Dependencies required by some projects but not others
  • Conflicts where upgrading a library for one project breaks another

Additionally, different applications may require different Python interpreters:
One project may need Python 3.7 while another uses 3.11 — and simply upgrading the global environment may break older applications.

To solve these two distinct but related problems, we use:

Problem Solution
Managing multiple Python versions pyenv
Isolating dependencies per project venv

Managing Python Versions with pyenv

pyenv allows you to install, manage, and switch between multiple Python versions on the same machine.

Install pyenv (Linux Example)

curl https://pyenv.run | bash

Add the initialization to your shell:

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Install Python Versions

pyenv install 3.9.18
pyenv install 3.11.7

Set Global or Project-Specific Python

Set the default:

pyenv global 3.11.7

Set a Python version only for a single project:

cd myproject/
pyenv local 3.9.18
Now your project runs with Python 3.9 while the rest of your machine uses 3.11.

Creating Project-Specific Virtual Environments with venv

Once the correct interpreter is selected via pyenv, create a virtual environment for the project:

python3 -m venv .venv

This creates a directory .venv/ inside your project containing:

  • A dedicated Python interpreter
  • A private pip package directory

Activate the Environment

Linux:

source .venv/bin/activate

Your shell now shows:

(.venv) $

Install packages safely

pip install requests flask numpy

These installs affect only this project, and will not interfere with others.

When Pip Errors Mention Python Version Conflicts

You may see errors like:

ERROR: This package requires Python >=3.10

Or:

No matching distribution found for <package>

Solution: Use pyenv to install the needed Python version:

pyenv install 3.10.13
pyenv local 3.10.13
python3 -m venv .venv
source .venv/bin/activate
pip install <package>

This ensures the interpreter matches the expected environment for that package.

Workflow Summary (Best Practice)

Freeze dependencies for reproducibility:

pip freeze > requirements.txt

Install dependencies:

pip install -r requirements.txt

Activate the environment:

source .venv/bin/activate

Create a virtual environment:

python3 -m venv .venv

Select the correct Python version:

pyenv local <version>

Conclusion

Developing with Python becomes significantly more resilient and maintainable when using:

  • pyenv to manage which Python interpreter version each project uses
  • venv to ensure isolated dependency environments that do not conflict

These tools help avoid dependency conflicts, simplify collaboration, support reproducibility, and future-proof your development workflows — making them essential for every professional Python project.

You're welcome.

Read more