OS inde­pendent Venvs for Deploy­ments

2018-01-29 | #python, #server, #webdev

Let's begin with the statement, that you should always deploy using a venv, whether you are using one in development or not (using Docker for example). The reason for this is simple: if you are on a managed system you may not be able to update system site packages after the initial setup, since you would have to write outside your user dir. What a headache! Just initially make sure to keep your python environment local.

But even if you are using a venv there is still one trap to step in:

Imagine your hoster gets the idea to update the python version. What happens to your venv? Well nothing happens to the binaries, those are copied inside the venv. The basic libs are fixed symlinks to the host system, no problem there if a new version is installed ... if they make sure to not uninstall the old version. Which they don't.

The consequence are missing base libs, which make your venv completely dysfunctional until you reinstall it. Buggers.

But, hooray, command line parameters to the rescue: remember to tell venv on installation to not symlink, but to copy libs to the venv, making it much more independent to what happens in the rest of the OS.

venv --always-copy -p python3 venv

And you are good to go.