Skip to Content
Course content

162: Comparing venv, virtualenv, and conda

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've seen this happen a dozen times with junior devs: they start a project, run pip install pandas, and everything is great. Then, six months later, they start a second project that requires a newer version of Pandas. They upgrade the package, and suddenly, the first project—which they weren't even touching—starts crashing with a TypeError because a function they relied on was deprecated. This is the "global installation trap," and it's exactly why we use isolated environments.

Why the global site-packages folder is a trap

The naive approach is to just install everything to your system Python. It feels fast at first, but you're essentially creating a single, giant bucket where every project shares the same versions of every library. Eventually, you hit a wall where Project A needs Library X v1.0 and Project B needs Library X v2.0. You can't have both in the same folder. You spend your afternoon playing "version whack-a-mole," upgrading and downgrading packages just to get a script to run. It's a waste of time and a recipe for unstable code.

venv vs. virtualenv: The lightweight battle

For most of your daily work, you'll be choosing between venv and virtualenv. To be honest, for a lot of people, they feel identical. They both create a folder in your project directory that contains a copy of the Python binary and its own set of installed packages. When you "activate" the environment, you're just telling your shell to look in that folder before looking at the system folders.

venv is the standard. It's built into the Python 3 library, meaning you don't have to install anything extra to get started. If you're building a simple web app with Flask or a CLI tool, python -m venv .venv is all you need. It's lean and it works.

Now, virtualenv is actually the older sibling. It's a separate package you have to install, but it's generally faster at creating environments and offers a few more bells and whistles, like better handling of different Python versions on the same machine. I usually stick with venv for simplicity, but if I'm managing a massive amount of environments or need a specific legacy version of Python that venv is struggling with, I'll reach for virtualenv.

When you need Conda to handle the "non-Python" stuff

Then there's Conda. Conda is a different beast entirely. While venv and virtualenv manage Python packages, Conda is a general-purpose package manager. This is a crucial distinction. If you're doing heavy data science or machine learning, you aren't just using Python; you're using C++ libraries, CUDA for GPUs, and complex linear algebra binaries like MKL.

If you try to pip install a complex scientific library, pip assumes the necessary C-libraries are already on your system. If they aren't, you get a wall of red text and a "compiler not found" error that can take hours to debug. Conda solves this by installing the pre-compiled binaries of those non-Python dependencies directly into the environment. It's heavier, it takes up more disk space, and it's slower to initialize, but it saves you from the "DLL hell" of configuring your OS to support scientific computing.

# Use this for 90% of your Python projects
python -m venv .venv

# Use this when you're doing Data Science/ML with complex dependencies
conda create --name ml_env python=3.10 numpy pandas scikit-learn

My rule of thumb? Start with venv. If you find yourself fighting with your operating system's compiler or needing a specific version of a C-library to get a package to install, switch to Conda. Don't overcomplicate your workflow until the complexity is actually required.




📋 Practical Task

Exercise: Resolving a Pandas Version Conflict across Two Local Projects

In this exercise, you will simulate a version conflict and resolve it using isolated environments. You will create two separate project directories—one representing a "Legacy" project and one representing a "Modern" project—each requiring a different version of the pandas library.

Your goals:

  • Create a directory named legacy_project and a directory named modern_project.
  • In legacy_project, create a venv environment, activate it, and install pandas==1.3.5.
  • In modern_project, create a venv environment, activate it, and install the latest version of pandas.
  • Create a small script in both folders that prints the current pandas version using pd.__version__.
  • Verify that running the script in the legacy folder shows 1.3.5, and running it in the modern folder shows the latest version, proving that the installations are not leaking into each other.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.