Guide by Jaryd; Virtual Environments on a Raspberry Pi | A Beginners Guide

I have just shared content in Guides > Raspberry Pi: “Virtual Environments on a Raspberry Pi | A Beginners Guide”





Read more

2 Likes

This is about to become the most linked to page on the forums. :stuck_out_tongue:

4 Likes

For some reason venv seem to confuse many. I wrote a guide Using Python Virtual Environments to help.
More usefully this explains how to unstall code into a venv and run without needing to activate the venv.

Hi @Ian38390

My preference for this is to have a macro setup on my macropad that will take whatever you have copied and use it for the command.

python3 -m venv --system-site-packages <copied_text>
source <copied_text>/bin/activate 
pip3 install <copied_text>

So if you were wanting to install say the Piicodev libraries, you would have piicodev on your clipboard, when you press the button it will spit out:

python3 -m venv --system-site-packages piicodev
source piicodev/bin/activate
pip3 install piicodev

I have used virtual environments, and see their usefulness … such as VirtualBox for occasional Windows on my main linux PC, and Proxmox on my OptiPlex 7050 which runs Home Assistant and, well, nothing else at the moment - but it can.

I have several Raspberry Pi’s (though they are of the older 1, 3A, 3B and 4 models) and as far as i am concerned … Raspberry Pi is a virtual environment. It is so easy to just swap microSD cards, and they are cheap enough. If I want multiple apps to work together on the same RasPi, they should work together and not ned to be kept separate. Personally I don’t understand the apparent requirement for docker on RasPi - but then I’m an old timer.

Of course with the larger RAM and faster CPU I accept that more people will be using RasPi 5 and 4 as their main computer, and for them virtual environments will be appropriate. I’ll shut up now.

Hi @Donald23173

Virtual environments and virtual machines are a little different from each other, where a VM creates a whole new operating system that runs inside the original OS, a virtual environment is really just a sandboxed directory that isolates software dependencies to prevent conflicts.

1 Like

true, true … and I do use venv on my desktop PC for the occasional bit of programming.
But there is overhead of keeping a full copy of all the libraries used by each app, just in case there is any difference between this version and each copy of the same library included in any other app on the machine. This is justified on a desktop workstation where you want latest versions of all the apps available all the time without conflicts … but the trade-off is more disk and RAM usage.

As i said, I am old school, my Raspberry Pis are the older models with much less memory, and I’m not trying to use them as general purpose desktop PCs. One project per SD card suits me.

2 Likes