picamera2: [BUG] Cannot be used with non-system python
Describe the bug
Running import picamera2
under python that is non-system python rasises ModuleNotFoundError: No module named 'libcamera'
.
This is because dependency installation python3-libcamera
creates python binding only for system python, rendering development of applications using picamera2
limited to current Pi OS base python version.
To Reproduce first install all dependencies according to README.md then:
virtualenv venv --python /usr/local/bin/python3.11
source venv/bin/activate
pip install picamera2
python
from within python repl
import picamera2
which rasises
File "<stdin>", line 1, in <module>
File "/home/pi/libcamera/venv/lib/python3.11/site-packages/picamera2/__init__.py", line 1, in <module>
from .configuration import CameraConfiguration, StreamConfiguration
File "/home/pi/libcamera/venv/lib/python3.11/site-packages/picamera2/configuration.py", line 1, in <module>
from .controls import Controls
File "/home/pi/libcamera/venv/lib/python3.11/site-packages/picamera2/controls.py", line 4, in <module>
from libcamera import ControlType, Size, Rectangle
ModuleNotFoundError: No module named 'libcamera'
Expected behaviour
Default recommended installation with pip
with python bindings created for the python connected with pip. This would make isolated development on any python version possible.
Console Output, Screenshots If applicable, any console output or screenshots that show the problem and associated error messages.
Hardware : Raspberry Pi 4B running Pi OS Bullseye 64-bit
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 11
- Comments: 31 (2 by maintainers)
Shouldn’t you be able to build your
libcamera
against python 3.9, 3.10, 3.11 and 3.12 and upload all of them to pypi? This is what other python package providers are doing as well, if they need a python-version specific code of the library. Otherwise every python project that uses the camera depends on the system python.Well, in about a week from today the Picamera2 package will be 3 Python versions behind Python3.12 release schedule. These cameras are not ready for the Python community, who by and large operate in virtual environments other than 3.9. Why complain? I now own ~$250 worth of dust collection surface.
Thanks for the solution proposal. Although its very hacky I will give it a go and test it out with pi camera 3. Does you venv use newer python version like 3.11 or sticks to the system 3.9? BTW I think since Raspberry Pi Foundation sells the cameras you actually can complain when there isn’t any proper solution for a very standard use case.
Has there been any progress on this or alternate solutions? Has anyone been able to upgrade to Python 3.10+ on a Raspberry Pi and still use Picamera2/Libcamera?
–system-site-packages does not work for me, libcamera is still not showing up in the virtual environment.
I installed fresh Pi OS 64-bit along with Python3.10.9 built from source with
--enable-optimizations --with-lto
flags, renamed the mentioned file, rebooted, and just as suspected nothing happened. Why can’t you guys package the libcamera with python bindings and post it to pypi? If you’re having problems with that @davisking did a great job handling dlib bindings withpybind11
maybe he can lend a hand.Wasn’t 100% sure what you meant. There are no pip packages for libcamera and kms++ currently. Camera applications and Picamera2 should work on Bookworm, though there are some fairly significant updates pending which will add/improve functionality.
@JooJooBee666 Wait till the end of October. When RPi 5 will be available also Raspberry Pi OS will go to bookworm and Python 3.11. I am assuming libcamera Python bindings will also be based on Python 3.11 then for aarch64. Maybe @davidplowman can confirm this.
@adamgogacz Raspberry Pi OS follows Debian long term (currently Debian 11). Until the next version of Debian Stable is taken up by Raspberry Pi OS do not expect the picamera2 packages being based on a Python version later than 3.9. FYI Debian 12 (Bookworm) uses python 3.11.
In a virtual env (I’m using poetry like #466 but it’s the same for a simple venv), the
sys.path
contains/usr/lib/python3.9
, so we can copy the libcamera module and its dependencies there and it will be available in all virtual envs using python 3.9. It won’t work if the venv is using another python version.I got a bunch of errors such as:
which disappeared after installing the relevant libs.
It’s a bit insane to have to do that and I wish I understood why this package breaks virtual envs - doesn’t everyone use some sort of venv nowadays for reproducibility of deployments? That’s a big deal!
But it honestly goes over my head and since I can’t fix it I can’t complain 😃
This won’t work since the
--system-site-packages
adds the packages from the python version that is used inside virtualenv This solved #341 sincevenv
package creates only isolated packages and still uses system pythonvirtualenv
on the other hand creates an isolated packages and python environment.I’ve confirmed that it won’t work on a clean Pi OS 64-bit with source-compiled python 3.10
BUT to actually check if you can use
picamera2
with python different that which the system provides I’ve done a simple testvirtualenv
libcamera
SO No matter what, the
libcamera
would have to be prepackaged and build duingpicamera2
build. Otherwise the users are forced to installlibcamera
on their own from source which I personally find toubling as when buying Raspberry Pi camera v2 I was hoping for a more native and easier way to get it up and running on newer python versions. Now the only hope is the python-opencvPlease consider chaning the default installation to
pip
as it supposed to be in the python community. Im happy to help and run some more tests if you’d like. Im desperate to get it running onpicamera2
and at leastpython3.10