1

I'm using a copy of very simple Pi-3 camera Python app.

import picamera
camera = picamera.PiCamera()
camera.capture('test.jpg')

If I run those lines in Python 3 (IDLE) console, it works.

If I save the same 3 lines of code in a file and run it, I get:

AttributeError: 'module' object has not attribute 'PiCamera'

enter image description here

7
  • It could be that you're implicitly calling Python 2.x when you should be calling Python 3.x or vice versa. What's the exact command you use to call the script containing the 3 lines? Try typing python your_script.py and python3 your_script.py and see if there's a difference.
    – jDo
    Apr 11, 2016 at 13:50
  • I'm using the Python 3 IDLE. I can run/file in that IDE, or run in console $ python3 picamera.py - and get the same error.
    – Reddirt
    Apr 11, 2016 at 14:15
  • python or python2 gives me the same error.
    – Reddirt
    Apr 11, 2016 at 14:18
  • Ok, odd. You can write a script containing only these two lines: import picamera and dir(picamera). The dir() command will show you all the methods and attributes of the module in question. What do you see when you run that in python2.x and python 3.x? Anything resembling "PiCamera"?
    – jDo
    Apr 11, 2016 at 14:27
  • Yes - >>> import picamera >>> dir(picamera) ['Blue', 'CircularIO', 'Color', 'Green', 'Hue', 'Lightness', 'PiCamera', 'PiCameraAlreadyRecording', 'PiCameraCircularIO', 'PiCameraClosed', 'PiCameraError', 'PiCameraMMALError', 'PiCameraNotRecording', 'PiCameraRuntimeError', ...
    – Reddirt
    Apr 11, 2016 at 14:32

1 Answer 1

1

Since OP was getting an AttributeError rather than an ImportError, something was clearly being imported correctly but simply didn't have the method PiCamera().

By calling dir(picamera) from a script, not in IDLE, to get a list of all available module methods, we found out that he was actually importing a different, home-made script with the same name as the picamera module (picamera.py). It probably worked in IDLE because it runs from a different folder.

So... Use dir() when you get an AttributeError and avoid using module names for your home-made scripts - it'll make things less confusing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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