How to Actually Use Python Virtual Environments

<2023-07-01 Sat>

For a long time, I did not understand the concept of a virtual environment in Python. Virtual environments are a concept that nobody ever explains, because they usually come up in the context of some tutorial for something more specific. I'd be trying to install some package, and one of the steps is "run this command to create a venv," and I'd just do it without caring. Then I'd end up with problems, and just deal with them on a case-by-case basis.

It's like if you're trying to work on your car, you might search the internet and find tutorials for how to replace parts, but nobody ever explains "hey, by the way, the official way to do this is written down in the service manual." This is the reason why a lot of idiots end up doing stupid things like stripping their lug nuts. Don't be that idiot.

1. The purpose of a virtual environment (venv)

Virtual environments allow you to have multiple versions of the same package. Suppose that package A requires version 1.0 of Numpy and package B requires Numpy 2.0. Or perhaps program A needs python 3.8 and program B needs python 3.10. Then, program A and Program B cannot coexist unless you install them in different venvs. There you go, that's the purpose of virtual environments.

2. Just Use Conda

Don't mess with venvs without first installing Conda (Miniconda). It's technically possible to use venvs without Conda, but you really shouldn't. Yes, you'll have to download/run an 80MB shell script. Don't worry about it. Make sure you accept the "run conda init" prompt. This will edit your ~/.bashrc so that it displays your current venv.

Once you have conda, you can:

  • Create a new venv
  • List existing venvs
  • Switch between venvs
  • Install packages in a venv


That's all you really need to know about virtual environments in Python.

3. Optional: use the Mamba solver

If you're like me, you'll notice pretty quickly that Conda is slow. Turns out there's a way to make it faster:

The disadvantage of the Mamba solver, supposedly, is that it is more likely to find solutions that change your packages more than the bare minimum.

Keywords: python

Modified: 2023-09-30 20:15:09 EDT

Emacs 29.1.50 (Org mode 9.6.1)