Automatically enabling venv at login
If your main use for a Raspberry Pi is running Python scripts, then needing to activate a virtual environment every time can get tedious. By adding the venv activation to your .bashrc
file, it will be activated everytime you log in.
For example, if a venv named foobar
has been previously created, add this line to the end of your .bashrc
file:
source ~/foobar/bin/activate
The venv will activate along with the changed prompt with each login.
Using Debian Packages instead of Python Modules
There are two general ways to install Python modules:
- pip - This is the Python specific tool for installing Python modules
- apt - This is the operating system tool for installing system level packages (apt-get is generally the same)
Using sudo with apt is OK. In fact, it's generally required since the packages will be installed in system level protected folder locations. However, using sudo with pip is potentially dangerous. So pip installing modules to the system level (via sudo) is generally not recommended. But many Python modules are available as OS (Debian) packages. Therefore, it is possible, and OK, to sudo apt install these Python modules.
For example, to install PIL/Pillow:
sudo apt install python3-pil
This will make PIL/Pillow available to anyone running Python on the setup. However, the OS packages will generally be older release versions than available via pip. And maybe that's OK? This is something you will have to determine for your specific use case.
Disabling Prompt Change
If you don't want the venv name to show up in the prompt, it can be disabled by setting the environment variable VIRTUAL_ENV_DISABLE_PROMPT
before activating the venv.
pi@raspberrypi:~ $ VIRTUAL_ENV_DISABLE_PROMPT=1 pi@raspberrypi:~ $ source foobar/bin/activate pi@raspberrypi:~ $ which python /home/pi/foobar/bin/python pi@raspberrypi:~ $
This can be done interactively as shown above or placed in .bashrc so it's active with every login.
Hide the venv folder
This is simple - just add a .
to the start of the venv name. This takes advantage of the Linux behavior treating dot files as hidden and not showing them by default with ls
. In a lot of guides, the name .venv
is used, but it's up to you what to call it. It's just the name you'll use when activated or referring to the venv.
This is entirely cosmetic.
That --break-system-packages option
This option can be used with sudo pip to force the installation despite the warning. However, this is not recommended. It should be possible to do everything using venv.
Return to the "good 'ole days"
If you're totally against venv's and don't care about possibly breaking your system's Python setup and just want to continuing doing things like you used to, then system wide pip installs can be re-enabled (undisabled?) by deleting a file named EXTERNALLY-MANAGED found in the system's Python setup. This is really just the same as using the --break-system-packages option mentioned above.
For example, on a freshly booted Pi running Bookworm, the file can be found at:
/usr/lib/python3.11/EXTERNALLY-MANAGED
So just sudo rm it:
sudo rm /usr/lib/python3.11/EXTERNALLY-MANAGED
Now you can sudo pip install all you want. At least until it finally corrupts the Python setup.
Multiple venvs
Keep in mind that more than one venv can be created. Each just ends up being its own folder with all the venv contents inside. This can come in handy when dealing with module conflicts or specific requirements. Like FooEditor requires PyQt5 5.9.2 *only* while BarEditor requires PyQt 5.12.1 *only*. A venv can be setup for each to keep the PyQt module installs separate.
Page last edited March 08, 2024
Text editor powered by tinymce.