0

Why would it be that a module could be imported via command line, but not via script?

As you can see below, I can import picamera2 in the first attempt via terminal, but when I'm running scripts the module is not found.

Why would this happen? The sys paths are almost exactly the same except for the inclusion of the directory of the script file.

mike@Robo64:~ $ python3.8
Python 3.8.18 (default, Mar 22 2024, 17:58:59) 
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/usr/local/lib/python38.zip', '/usr/local/lib/python3.8', '/usr/local/lib/python3.8/lib-dynload', '/home/mike/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/site-packages']
>>> import picamera2
>>> exit()
mike@Robo64:~ $ cd robot/src/robosrc/

mike@Robo64:~/robot/src/robosrc $ python3.8 test.py 
path =  ['/home/mike/robot/src/robosrc', '/usr/local/lib/python38.zip', '/usr/local/lib/python3.8', '/usr/local/lib/python3.8/lib-dynload', '/home/mike/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/site-packages']

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    import picamera2
ModuleNotFoundError: No module named 'picamera2'

...TLDR section with details...

I've had to manually install Python 3.8 on my RasPi 4 system due to compatibility issues with some tools (particularly tflite-support).

Running a Pi 4 Bookworm 64 bit which came with python 3.11.12

I've been able to successfully install and use a number of libraries via:

python3.8 -m pip install <package>

such as numpy, protobuf, gpiozero, even tflite-runtime, but picamera2 is being difficult when running with my custom python3.8. Installing picamera2 via pip hangs my system, so I installed it with sudo apt install python3-picamera2

test.py (used above) is:

import sys
print("path = ",sys.path)
print("")
import picamera2

Installed python 3.8 with these steps

>wget https://www.python.org/ftp/python/3.8.18/Python-3.8.18.tgz
>tar zxf Python-3.8.18.tgz
>cd Python-3.8.18/
>./configure
>make
>sudo make install

-- Update -- Confirmed that picamera2 needs python 3.9 or above here. The picamera2 module I had installed was very old v 0.1.1 I'm not sure this explains why the import would not work when run in a script.

3
  • How did you install python 3.8? If you installed into a virtual environment you can run multiple versions and not break your OS. 3.8 seems a poor choice as the Pi never had that if you REALLY need an older version 3.7 would be a better choice.
    – Milliways
    Commented Mar 23 at 21:37
  • I built 3.8 and installed it manually. I tried 3.7 but had way more problems with other modules so I tried 3.8 and so far everything works except picamera2. installed like this: >wget python.org/ftp/python/3.7.16/Python-3.7.16.tgz >tar zxf Python-3.7.16.tgz >cd Python-3.7.16/ >./configure >make >sudo make install
    – MichaelC
    Commented Mar 23 at 22:07
  • If you ran sudo apt install python3-picamera2 you installed _libcamera.cpython-311-aarch64-linux-gnu.so. python3-picamera2 should already be installed in /usr/lib/python3/dist-packages for a normal Bookworm.
    – Milliways
    Commented Mar 25 at 23:58

1 Answer 1

0

You can have multiple python versions.

I often install the latest (currently 3.12.2) (although I have never installed an old version you should be able to do this).

Manually installed versions live in /usr/local/bin/ which takes precedence over the system installed version in /usr/bin/ but you can run any version by using the explicit version.
python3.11 should run the system version on Bullseye and you should be able to run python3.8. You would probably need to install the tool you want to python3.8

You can stop python3 from selecting your manually installed version by deleting /usr/local/bin/python3 (which is a link to the manually installed version). This may not be the most elegant (or even recommended approach)

My sys.path is ['', '/usr/lib/python311.zip', '/usr/lib/python3.11', '/usr/lib/python3.11/lib-dynload', '/usr/local/lib/python3.11/dist-packages', '/usr/local/lib/python3.11/dist-packages/rgpio-0.2.2.0-py3.11.egg', '/usr/lib/python3/dist-packages', '/usr/lib/python3.11/dist-packages'] even though I have 3.12.2.

If I run python3.12 it is ['', '/usr/local/lib/python312.zip', '/usr/local/lib/python3.12', '/usr/local/lib/python3.12/lib-dynload', '/usr/local/lib/python3.12/site-packages']

2
  • Thanks for this, do you think that could be my issue even though I am explicitly calling python3.8 and I confirmed that the paths are the same?
    – MichaelC
    Commented Mar 25 at 18:56
  • Your problem (although you have not posted detailed diagnostics) appears to be that 3.8 is the default so you can't run 3.11 normally, but should enable you to fix the problem. You NEED to use 3.11 for now camera code.
    – Milliways
    Commented Mar 25 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.