So you have installed Debian Buster on your headless Raspberry Pi and you want to use Python and you want to make as few system changes as possible?
Let’s do it.
Python
You want to install Python.
Who am I kidding? You don’t want to install Python, but for some reason you need to.
Could be that you’re going to install a Django-based application or service. Could be that you just want to experiment with Python.
In any case, you want to make as few system changes as possible. After all, you did install a minimal Debian Buster, so you don’t want to pull in a zillion packages.
The good news is, there’s already a mechanism in Python to make sure that (non-root) users can manage their own Python packages without interfering with the system (or with other users of the system).
This mechanism is managed by pip, the package installer for Python. Think of it as your personal apt for Python.
For instance, do you need to install Django?
You sure can run apt install python3-django
, but you need to run this as root and you’ll end up with version 1.11.29 (currently), and if there’s any updates to Django, you’ll have to wait for Debian to package them.
Whereas if you run pip3 install Django
, you won’t need to be root at all you’ll get version 3.1.3 (again, currently) and you’ll get updates way sooner.
Also: zero system impact and minimal Python-related system packages.
The good news is, the minimal system you have installed has just this: minimal python-related system packages. Basically, Python itself, and pip3. That’s all.
If you don’t believe me, run pip3 list
as a non-root user (you did create a non-root user, did you not?) and see for yourselves.
You install the rest of your Python packages using pip3.
Now go learn Python.
Virtual environments
Of course, there’s still the problem that you may want to install Python packages for some tinkering then remove them, and you don’t want to have to track them individually. Or you want to install a specific version of a Python package for one application which needs exactly this one, but other Python code you use needs the latest version.
Python has a mechanism for just this: giving each Python application its own set of installed packages. It is called a virtual environment.
The good news is: you can install Python virtual environment support through pip3 — no need for system packages. It is as simple as pip3 install virtualenv
.
Of course you ran this command without reading the whole page, didn’t you? And you saw that ominous message telling you that…
The script virtualenv is installed in ‘/home/user/.local/bin’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.
… and now you’re stuck wondering whether you should panic or not?
You should not.
Just exit the shell and start a new one, or you can stay in the initial shell and just run PATH=$PATH:${HOME}/.local/bin
.
Now you can create a virtual environment with the usual command:
$ virtualenv myenv
$ source myenv/bin/activate
(myenv) $ _
You’re all set up now! Go learn about virtual environments where they’ll explain those much better than I would.