Python venv for development

Use venv and never pip install as root.

I often use Python wrappers around C++, for example for testing or visualisation. To handle versioning, it is essential to setup a virtual environment for modules. Historically there have been many different approaches to this, but now there is one good way. While python2 will no doubt remain installed on my systems indefinitely, I won't be using it for new development, so the best way to separate my libraries from system libraries - and other development versions of my libraries - uses venv:

python3 -m venv ~/pythonenvironment

On Ubuntu, you'll need to apt-get install python3-venv first. venv is included by default on Redhat and most Windows distributions as far as I can tell.

Virtual Environments or Containers

Another way to isolate Python dependencies is the same way you isolate any running code - using separate containers, which generally means Docker. That works and achieves almost everything that venv does.

Since you will likely be packaging code with Docker (see this simple docker example for python), why bother with a different set of commands? For me the difference is that venv creates a separated development environment, and docker creates an isolated deployment environment. You're free to develop in that deployment environment, but I find it simpler not to.

Put another way, the main advantage of venv is that it brings the python environment directly to the host machine. For example, using python scripts to make connections with remote resources, it's easier to have those available in your development environment rather than your deployment environment.

Rather than docker replacing the need for venv, there are good reasons to use venv when building docker images, but I've already strayed too far from the topic of development.