[SUGGESTION][COMPATIBILITY] Make pip installing picamera2 work in different python versions with pyenv
I think it would be a good idea to put more attention to making picamera2 with libcamera python wrappers work also on other versions of python. I have bought a pi5, camera module 3 and google coral usb TPU all in the hopes that these will work on the newest hardware. Found out today that google coral only supports python 3.9, pi5 comes with python 3.11 out of the box, and since it has libcamera 0.2 it is impossible to pip install picamera2 in a separate python 3.9 environment.
Maybe i dont understand the proble, but is the only thing we would need not just python 3.9 wrappers/bindings for libcamera? Is there a plan to create this, or a good reason that we dont have them yet? The pi5 is supposed to be THE SBC for computer vision.
What i've tried and given up on:
- Building libcamera from source
- Pip installing https://github.com/raspberrypi/pylibcamera with the master branch of libcamera
- using --system-site-packages with pyenv (works only for same python version as system)
Yes, this is all more difficult than you might expect because different libcamera versions are not binary compatible and so, if you're not using the standard latest versions that we distribute, you have to build everything to match carefully from scratch. Have you looked at the instructions here?
@will-v-pi Is there anything you'd add to those instructions for someone trying to build for Python 3.9 on a Pi 5?
It should work for our November release of libcamera - I'm just making an update for the new release last week, as the vendor controls need to be manually defined in pylibcamera
New release made, and tested with python3.9, so those instructions should work fine
Installing does work, which is great, but if i import it (picamera2) inside a python script, i get an error that says no module named libcamera? Im guessing this is because libcamera is built against the systems python version (3.11) and not 3.9? Do i need to build libcamera from source inside my virtualenv to make it work
That shouldn't be necessary, as the rpi-libcamera pip package builds the python bindings for whatever python is active - what's the output you get from running pip list inside your virtualenv?
That shouldn't be necessary, as the rpi-libcamera pip package builds the python bindings for whatever python is active - what's the output you get from running
pip listinside your virtualenv?
This is it, on python version 3.9 inside a venv:
(pycoral) zanza@5zero:~/picamera2/examples/tensorflow $ pip list
Package Version
---------------------------- --------------
absl-py 2.1.0
astunparse 1.6.3
av 11.0.0
cachetools 5.3.2
certifi 2024.2.2
charset-normalizer 3.3.2
flatbuffers 20181003210633
gast 0.5.4
google-auth 2.28.1
google-auth-oauthlib 1.2.0
google-pasta 0.2.0
grpcio 1.62.0
h5py 3.10.0
idna 3.6
importlib-metadata 7.0.1
keras 2.15.0
libclang 16.0.6
Markdown 3.5.2
MarkupSafe 2.1.5
ml-dtypes 0.2.0
numpy 1.26.4
oauthlib 3.2.2
opencv-python 4.9.0.80
opt-einsum 3.3.0
packaging 23.2
picamera2 0.3.17
pidng 4.0.9
piexif 1.1.3
Pillow 9.5.0
pip 24.0
protobuf 4.25.3
pyasn1 0.5.1
pyasn1-modules 0.3.0
python-prctl 1.8.1
requests 2.31.0
requests-oauthlib 1.3.1
rsa 4.9
setuptools 58.1.0
simplejpeg 1.7.2
six 1.16.0
tensorboard 2.15.2
tensorboard-data-server 0.7.2
tensorflow-io-gcs-filesystem 0.36.0
termcolor 2.4.0
tflite-runtime 2.15.0
typing_extensions 4.9.0
urllib3 2.2.1
v4l2-python3 0.3.3
Werkzeug 3.0.1
wheel 0.42.0
wrapt 1.14.1
zipp 3.17.0
(pycoral) zanza@5zero:~/picamera2/examples/tensorflow $ python
Python 3.9.18 (main, Feb 23 2024, 10:11:25)
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import picamera2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/zanza/.pyenv/versions/pycoral/lib/python3.9/site-packages/picamera2/__init__.py", line 3, in <module>
import libcamera
ModuleNotFoundError: No module named 'libcamera'
>>> exit()
It looks like you haven't actually install rpi-libcamera and rpi-kms - you need to run these instructions from within your virtual environment, as detailed in the forum post
pip install --upgrade pip
pip install wheel
pip install rpi-libcamera rpi-kms picamera2
Thanks, that does work, do you know if the GUI also works? I cannot use the preview because i cant build pyqt5, but maybe i should open a separate issue for this.
It looks like you haven't actually install rpi-libcamera and rpi-kms - you need to run these instructions from within your virtual environment, as detailed in the forum post
pip install --upgrade pip pip install wheel pip install rpi-libcamera rpi-kms picamera2
@will-v-pi I have problems when it shows "Preparing metadata (pyproject.toml)". This is the output:
Collecting picamera2
Downloading https://www.piwheels.org/simple/picamera2/picamera2-0.3.17-py3-none-any.whl (76 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 76.4/76.4 kB 10.9 kB/s eta 0:00:00
Collecting rpi-libcamera
Downloading rpi_libcamera-0.1a3.tar.gz (9.7 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... done
Installing backend dependencies ... done
Preparing metadata (pyproject.toml) ... error
error: subprocess-exited-with-error
× Preparing metadata (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [19 lines of output]
+ meson setup /tmp/pip-install-3i9up34x/rpi-libcamera_b92ae0bec685433e9e461be3950db7c1 /tmp/pip-install-3i9up34x/rpi-libcamera_b92ae0bec685433e9e461be3950db7c1/.mesonpy-6ffbuy0v -Dbuildtype=release -Db_ndebug=if-release -Db_vscrt=md -Drevision=v0.2.0+rpt20240215 -Drepository=https://github.com/raspberrypi/libcamera.git -Dversion=v0.2.0+46-075b54d5 --native-file=/tmp/pip-install-3i9up34x/rpi-libcamera_b92ae0bec685433e9e461be3950db7c1/.mesonpy-6ffbuy0v/meson-python-native-file.ini
The Meson build system
Version: 1.3.2
Source dir: /tmp/pip-install-3i9up34x/rpi-libcamera_b92ae0bec685433e9e461be3950db7c1
Build dir: /tmp/pip-install-3i9up34x/rpi-libcamera_b92ae0bec685433e9e461be3950db7c1/.mesonpy-6ffbuy0v
Build type: native build
Project name: pylibcamera
Project version: 0.1.0
C compiler for the host machine: cc (gcc 12.2.0 "cc (Debian 12.2.0-14) 12.2.0")
C linker for the host machine: cc ld.bfd 2.40
C++ compiler for the host machine: c++ (gcc 12.2.0 "c++ (Debian 12.2.0-14) 12.2.0")
C++ linker for the host machine: c++ ld.bfd 2.40
Host machine cpu family: aarch64
Host machine cpu: aarch64
Message: Cloning from: https://github.com/raspberrypi/libcamera.git, revision v0.2.0+rpt20240215
../meson.build:19:0: ERROR: Command `/tmp/pip-install-3i9up34x/rpi-libcamera_b92ae0bec685433e9e461be3950db7c1/clone-libcamera.sh https://github.com/raspberrypi/libcamera.git v0.2.0+rpt20240215 pypatch` failed with status 1.
A full log can be found at /tmp/pip-install-3i9up34x/rpi-libcamera_b92ae0bec685433e9e461be3950db7c1/.mesonpy-6ffbuy0v/meson-logs/meson-log.txt
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.`~~~
`
But, I have tried to look for the meson-log.txt but I can't find it anywhere, not in the /tmp or in the ~/.pyenv, please let me know how I can fix this issue. I really need to make the picamera2 work in pyenv`
@will-v-pi I tried installing rpi-kms and picamera2 using pip, and I was able to do it, but I have problems installing libcamera
Same. That's probably because of the virtual env.
Same. That's probably because of the virtual env.
you can install it if you're using the same python version inside venv as the system python version, but in my case, system Python is (3.11), version inside venv is (3.9, I have to use <3.10 because of dependancy issues). So, I can't use the --system-site-packages flag. I need a way to install picamera2 inside different version of Python inside pyenv
@AlgorithmicAce That's odd, it looks like the script to clone libcamera is failing when installing rpi-libcamera. Could you clone the pylibcamera repository and try running the command ./clone-libcamera.sh https://github.com/raspberrypi/libcamera.git v0.2.0+rpt20240215 pypatch from inside that repository, as that should give you more output for debugging?
./clone-libcamera.sh https://github.com/raspberrypi/libcamera.git v0.2.0+rpt20240215 pypatch
This is the output
~/pylibcamera/libcamera ~/pylibcamera
Note: switching to 'v0.2.0+rpt20240215'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 075b54d5 RASPBERRYPI ONLY: Add a Github workflow to generate release tarballs
~/pylibcamera````
@will-v-pi I was trying to import libcamera in my pyenv after cloning the repo and running the command, it worked, but when I'm trying to import picamera2, I'm getting the following output:
>>> import picamera2 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/pi/.pyenv/versions/myproject/lib/python3.9/site-packages/picamera2/__init__.py", line 5, in <module> from .configuration import CameraConfiguration, StreamConfiguration File "/home/pi/.pyenv/versions/myproject/lib/python3.9/site-packages/picamera2/configuration.py", line 1, in <module> from .controls import Controls File "/home/pi/.pyenv/versions/myproject/lib/python3.9/site-packages/picamera2/controls.py", line 4, in <module> from libcamera import ControlType, Rectangle, Size ImportError: cannot import name 'ControlType' from 'libcamera' (unknown location)
./clone-libcamera.sh https://github.com/raspberrypi/libcamera.git v0.2.0+rpt20240215 pypatch
This is the output ...
The output from that command looks correct, and I have not been able to replicate the issue (using a python 3.9 virtual environment created with pyenv, on Raspberry Pi OS Bookworm), so I would suggest cleaning your virtual environment and any caches pyenv uses, and trying the installation again (possibly also with --no-cache in the pip commands). If that doesn't work then try from a clean installation of Raspberry Pi OS
@will-v-pi Hi, I haven't tried from Pi 5 but I have tried from my Pi 4 using Bookworm and it works, thanks. Could you guide me to any links that will help me install PyQt5 in the virtual environment? I will try again later with the Pi 5. Thank you so much
@AlgorithmicAce I've had success running
sudo apt install qtbase5-dev
pip -v install --config-settings --confirm-license= pyqt5
but if that doesn't work you'll need to look at the PyQt5 documentation (which is where I got that command from)
@will-v-pi Hi, I have install PyQt5, yet I am not seeing any output video in a window when I'm running inside the virtual machine, any idea how to fix it?