-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Raspberry Pi OS Bookworm + Pep 668 #13
Conversation
7f0c107
to
fc12d7d
Compare
Pull Request Test Coverage Report for Build 7476616755
💛 - Coveralls |
If you have anything that should go into that documentation, which as you mention is linked right in the error message, please either drop me email or raise a PR against the |
The two people who actually read error messages will appreciate that 😭 Right now I don't have any grand designs on the documentation, my preference is mostly to smooth over the transition as much as possible via our installers (due to aforementioned and unfortunately not sarcastic problem with people simply not reading errors or documentation). I will add that it did confuse me a little when I clicked through and went straight to "Python on Raspberry Pi" rather than "About Python virtual environments." (Thanks for finding your way here, by the way, I assume you followed my whimpering cries of anguish) |
install.sh
Outdated
if [ ! -f $PY_VENV_DIR/bin/activate ]; then | ||
inform "Creating virtual Python environment in $PY_VENV_DIR, please wait...\n" | ||
mkdir -p $PY_VENV_DIR | ||
/usr/bin/python3 -m venv $PY_VENV_DIR --system-site-packages |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be nice to add --prompt Pimoroni
or something similar to the creation of the venv so that when the environment is activated, the user's prompt changes to (Pimoroni) pi@raspberrypi:~ $
which at least indicates that they're in a Pimoroni environment. The default prompt of (venv) pi@raspberrypi:~ $
is a little confusing as it's not clear which "venv" you have activated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for taking the time to look at this. I'm keenly aware that foisting something like this upon users should be done at least somewhat carefully, and thus any and all feedback is really, really appreciated.
TLDR: Yes, I totally agree!
Considering recommending an extra line in
|
I mean, the folks who read it seem happy afterward, e.g. https://twitter.com/drfootleg/status/1714906054302060692.
…and I don't disagree.
Future-proofing myself, I've got a sneaking suspicion that I'm going to need more verbiage eventually.
Pretty much! 😆 |
This sounds like a good plan! |
d9710f7
to
8052ee4
Compare
This is a noble attempt at helping, which really should be addressed at the OS level. I raised an issue here raspberrypi/bookworm-feedback#100 |
My attempts at following the suggestions here, on Bookworm 64bit OS on the Pi 5. First I tried to run the example without sudo (predicably it cannot access the LEDs). Then I try with sudo but still it fails. (robots-venv) pi@raspberrypi5:~/inventorhatmini-python/examples $ python led_rainbow.py
Traceback (most recent call last):
File "/home/pi/inventorhatmini-python/examples/led_rainbow.py", line 18, in <module>
board = InventorHATMini()
^^^^^^^^^^^^^^^^^
File "/home/pi/robots-venv/lib/python3.11/site-packages/inventorhatmini/__init__.py", line 99, in __init__
GPIO.setup(self.PI_USER_SW_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
RuntimeError: No access to /dev/mem. Try running as root!
Exception ignored in: <function InventorHATMini.__del__ at 0x7fff52ecd3a0>
Traceback (most recent call last):
File "/home/pi/robots-venv/lib/python3.11/site-packages/inventorhatmini/__init__.py", line 142, in __del__
self.ioe.reset()
^^^^^^^^
AttributeError: 'InventorHATMini' object has no attribute 'ioe'
(robots-venv) pi@raspberrypi5:~/inventorhatmini-python/examples $ sudo --preserve-env=PATH python led_rainbow.py
Traceback (most recent call last):
File "/home/pi/inventorhatmini-python/examples/led_rainbow.py", line 18, in <module>
board = InventorHATMini()
^^^^^^^^^^^^^^^^^
File "/home/pi/robots-venv/lib/python3.11/site-packages/inventorhatmini/__init__.py", line 99, in __init__
GPIO.setup(self.PI_USER_SW_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
RuntimeError: Mmap of GPIO registers failed
Exception ignored in: <function InventorHATMini.__del__ at 0x7fff9a65d3a0>
Traceback (most recent call last):
File "/home/pi/robots-venv/lib/python3.11/site-packages/inventorhatmini/__init__.py", line 142, in __del__
self.ioe.reset()
^^^^^^^^
AttributeError: 'InventorHATMini' object has no attribute 'ioe' |
Yup RPi.GPIO - which any of our drivers using GPIO will use - is pretty much dead in the water at the moment. That's why I'm embroiled in getting the libgpiod Python bindings ship shape, so I can start migrating drivers to those. There is an lgpio (which uses libgpiod) based compatibility shim for RPi.GPIO which might help: https://github.com/waveform80/rpi-lgpio I've been too mired in getting the absolute basics working to see if there's any short term crutch we can rely on, however. |
8052ee4
to
d9710f7
Compare
Using sed -i on /boot/config.txt would read the file and write it back as a real file instead of a link. Breaking assumptions about this file being a link to /boot/firmware/config.txt and leading to all sorts of confusion.
Fix a bug where auto_venv.sh was being created in a non-existent directory. Trap exit codes for some commands and add some help text + GitHUb url at the end of the install process. Try to comment what some sections do, and insert linebreaks so they are more logically broken up in the installer output. Try to be more consistent with colours. Try to be more friendly with colours- remove full red warning text in favour of a prefix so the errors/warnings are easier to read. Return a failure exit code if bits of the script have failed. Try to re-order output so it's more logical. Re-word venv creation message.
install.sh
Outdated
fi | ||
else | ||
if [ -f "/boot/$CONFIG_FILE" ] && [ ! -L "/boot/$CONFIG_FILE" ]; then | ||
warning "Oops! It looks like /boot/$CONFIG_FILE is not a link to $CONFIG_DIR/$CONFIG_FILE" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My latest Pi OS install has a non-link file for /boot/config.txt
with the contents:
DO NOT EDIT THIS FILE
The file you are looking for has moved to /boot/firmware/config.txt
Need to check if this is now standard across all Pi OS installs and, in any case, check for this text and avoid emitting this warning.
A simpler path might be to avoid all this kerfuffle and always prefer /boot/firmware/config.txt
if it exists, without any efforts to warn about other config files.
Or maybe even fail outright if /boot/firmware/config.txt
does not exist...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I found the change: RPi-Distro/pi-gen@a125344
It will be standard, but a quick test of 64bit Pi OS lite suggests it currently is not. Therefore the symlink warning is obsolete.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just tried growhat on RiOS bookworn 32bit, and it blew up at me, this was one of the problems. It doesn't have anything in /boot/firmware/ at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grow should have a fallback to /boot/config.txt
- do you have a full output of the install script?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try again, but nothing was added to the normal config.txt.
For now to get it working I've moved to legacy 'bullseye' and the install was ok.
I'll have another go with bookworm when I get the time. (Then for fun I'll try the EnviroHat and the WeatherHat and the led matrix pi zero sized thing if forget the name of... obviously not as much fun as you're having rewriting all of this for all the devices.
But the venv never seemed to go right either, it didn't boot to one. and then when I manually enabled it none of the modules seemed to be there when running the examples. I'll check what it actually contains properly, it might be a few days though.
(Pi Zero, so has to be the 32bit lite version).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently I'm not enabling the virtual environment by default, though it should install a script into ~/Pimoroni/auto_venv.sh
which can be optionally added to ~/.bashrc
.
source ~/.virtualenvs/pimoroni/bin/activate
should activate the venv manually.
This file was changed from a symlink to a "DO NOT EDIT" real file in: RPi-Distro/pi-gen@a125344
Quotes would cause a list of packages to be treated as a single package and lookup would fail. Reported-by: thirdr <[email protected]>
19ad16d
to
31b961c
Compare
`warning: `ruff <path>` is deprecated. Use `ruff check <path>` instead.`
Rationale
Update:
~/.virtualenvs/pimoroni' as the default virtualenv dir, since this is what
virtualenvwrapper` uses, and thus is one of the search paths used by Visual Studio Code.As of RPi OS Bookworm (required for the Raspberry Pi 5) incorporating Pep 668, we are required to install Python packages into a virtual environment or "venv".
The TLDR of 668 is twofold -
Attempting to modify the system Python environment will kick out an error along the lines of:
Raspberry Pi's own documentation (linked in this error message on RPi OS) attempts to cover some basic use cases of virtual environments ans shows some per-project and per user patterns you might consider if you're keen to learn.
For most beginners, however, the specifics of setting up a virtual environment are largely meaningless and due to the non-destructive - you might say "virtual" - nature of these environments there is no harm in us stepping in as the arbiter of truth and setting up one by default.
We can do this in a couple of ways:
Supporting venvs in our Python boilerplate
The changes in this PR attempt to detect and (optionally) manage a virtual environment for the user.
The virtual environment we use by default will live in
$HOME/.virtualenvs/pimoroni
and this will be shared across all packages, giving a single common mixed use venv for beginners.Providing a simple "manage me a venv" script
It's possible for a user to opt to gloss over the existence of a virtual environment by adding the following lines to
~/.bashrc
-This will create (if it does not exist) and activate a Python virtual environment automatically any time a bash shell is opened. If the user doesn't want to use it, they can type "deactivate" or switch into another virtual environment just as easily.
The idea behind this is that users who have installed our software, and are set up to use our
$HOME/.virtualenvs/pimoroni
virtual environment don't need to explicitly activate it every time they want to work with our products.Putting this into
~/.bashrc
automatically would be unwise since:~/.bashrc
and how it might (negatively) interact with this snippetBut it's probably useful for users who really don't want to have to think about virtual environments (much.)
System Packages
In both cases, since we still rely on apt packages to satisfy some dependencies, we're using
--system-site-packages
to pass through the system Python packages into the virtualenv. This is generally unwise since it introduces packages we can't properly manage into the virtualenv, but for now it permits the use ofgpiozero
+lgpio
(the latter missing a proper pypi package) and 'libgpiod' (also currently missing a working pypi package),What about my precious rpi_ws281x?
In order to run Python code as root within the virtualenv context you need to:
In conclusion
Many users don't really want to know or care about a per-project virtual Python environment, and just want a close approximation of the old behaviour. This gets us part of the way with the notable exceptions that: