# Why do I need Virtual Python environment?

**URL:** <https://forums.pimoroni.com/t/why-do-i-need-virtual-python-environment/27250>\
**Category:** Support\
**Created:** [February 5, 2025, 10:08am UTC](https://forums.pimoroni.com/t/why-do-i-need-virtual-python-environment/27250 "2025-02-05T10:08:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vk5la](https://avatars.discourse-cdn.com/v4/letter/v/f475e1/32.png) [@vk5la](https://forums.pimoroni.com/u/vk5la)\
**Post date:** [February 5, 2025, 10:08am UTC](https://forums.pimoroni.com/t/why-do-i-need-virtual-python-environment/27250/1 "2025-02-05T10:08:02Z")

</div>

Hi all,  
Completely wet behind the ears here. I’m trying to get my head around using the pi with the Automation hat.  
Why is the automation hat libraries installed in a “virtual environment”?  
Sure, I can use the examples no worries, but how do I use the hat in a normal python script without having to call up the virtual environment every time?  
Sorry if this is dumb but if I write a script and call

import automationhat

Without being in the “virtual environment”

It fails

Is the installation script on the Pimoroni site just for the examples? The script won’t work if you tell it no to the virtual environment

Sorry just a bit lost

Andy

---

<div class="post-metadata">

**Author:** ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)\
**Post date:** [February 5, 2025, 12:40pm UTC](https://forums.pimoroni.com/t/why-do-i-need-virtual-python-environment/27250/2 "2025-02-05T12:40:41Z")

</div>

The Python Virtual Environment is something the Raspberry Pi Foundation implemented. As far as I know anyway. IMHO, its a PITA.

I have similar questions.  
How do you auto run / activate it on boot up?  
And how do you run your code from Thonny in it?

---

<div class="post-metadata">

**Author:** ![hel](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/hel/32/6075_2.png) [@hel](https://forums.pimoroni.com/u/hel)\
**Post date:** [February 5, 2025, 1:04pm UTC](https://forums.pimoroni.com/t/why-do-i-need-virtual-python-environment/27250/3 "2025-02-05T13:04:51Z")

</div>

More of a recommendation from Python/upstream Linux, which has been overdue for implementation in Pi OS for some time, as I understand it.

@gadgetoid wrote an quick intro to venvs here, which includes details of how to use them with Thonny:

> **[Python Virtual Environments on Raspberry Pi Bookworm](https://pimoroni.github.io/venv-python/)**
>
> An introduction to Python Venv on
> Raspberry Pi OS Bookworm.

This article also provides a nice (brief!) intro:

[https://www.raspberrypi.com/news/using-python-with-virtual-environments-the-magpi-148/](https://www.raspberrypi.com/news/using-python-with-virtual-environments-the-magpi-148/)

And Adafruit’s guide is a bit chunkier but contains lots of good info (including how you can set up a Python script as a service so it will run at boot):

> **[Python Virtual Environment Usage on Raspberry Pi](https://learn.adafruit.com/python-virtual-environment-usage-on-raspberry-pi/)**
>
> How set up Python Virtual Environments as required by Raspberry Pi OS

---

<div class="post-metadata">

**Author:** ![bablokb](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@bablokb](https://forums.pimoroni.com/u/bablokb)\
**Post date:** [February 5, 2025, 1:15pm UTC](https://forums.pimoroni.com/t/why-do-i-need-virtual-python-environment/27250/4 "2025-02-05T13:15:25Z")

</div>

The basic rationale is that you run into trouble if you want to manage your Python packages with two different package-managers that have a different idea of what is the “current” or “correct” package version. All Linux-systems have their own package manager (e.g. Debian/PiOS has “apt”). And Python has also a package-manager called “pip”.

Some packages are available from both, some only from a single package manager. Virtual environments are a safe space for pip to operate on - i.e. pip won’t interfere with apt. And you can have multiple venvs, in case you have programs that need a package in version A and the other in version B.

Very often you don’t encounter problems though if you mix package managers. That is why many people think venvs are not necessary. But sooner or later, you will run into problems. For single application systems, like running on a Pi-Zero, this is unlikely, but if you use your Pi for daily work, it is only a matter of time.

---

<div class="post-metadata">

**Author:** ![hel](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/hel/32/6075_2.png) [@hel](https://forums.pimoroni.com/u/hel)\
**Post date:** [February 5, 2025, 1:16pm UTC](https://forums.pimoroni.com/t/why-do-i-need-virtual-python-environment/27250/5 "2025-02-05T13:16:53Z")

</div>

You can also use cron (`crontab -e`) to set a script running at boot, if you add something like this:

`/home/user/.virtualenvs/pimoroni/bin/python /path/to/python/script.py`

(replacing ‘user’ with your Pi OS username and the second bit with the path to the script you want to run)
