I am trying to build a security system on the Rasberry pi 3 using python 3 that senses motion then takes a photo and sends the photo via email to me. When i run the code it says it can't import the module picamera, I have installed the module on terminal using the 'sudo apt-get install python-pip' command but it says already satisfied. Here is the code:

import RPi.GPIO as GPIO
import time
import picamera
import datetime
import subprocess
import smtplib
import os

AuthUser = '[email protected]'
AuthPass = 'alarmsystem'

toAdd = '[email protected]'
fromAdd = AuthUser

subject = 'Motion Detected'

def getFileName(): 
    return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")

from gpiozero import MotionSensor

pir = MotionSensor(4)

from PiCamera import PiCamera

camera = PiCamera()

while True:
    print("Motion Detected")

mail = smtplib.SMTP('smtp.gmail.com',587)
gmailUpload = () 
mail.login ('[email protected]', 'alarmsystem')
mail.send_mail = (fromAdd, toAdd)

#sensor setup

sensorPin = 7

GPIO.setup(sensorPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

prevState = False
currState = False

camera = picamera.PiCamera()

from gpiozero import MotionSensor

pir = MotionSensor(4)

while True:
prevState = currState
currState = GPIO.input(sensorPin)
if currState != prevState:
    newState = "HIGH" if currState else "LOW"
print ("GPIO pin %s is %s" % (sensorPin, newState))
if currState:
    fileName = getFileName()
    print ("Starting Recording...")
    print (fileName)
    print ("Stopped Recording")
    print ("Sending Mail Notification...")
subprocess.call("mail -s 'Motion Detected' [email protected] < /home/pi/mail/images", shell=True)
print ("Complete")

Please help me fix this issue it would be greatly appreciated.

Thank you

  • 2
    Are you sure you use the right name (case is important). 'from PiCamera import PiCamera' should be 'from picamera import PiCamera' and remove the bare 'import picamera' at the top of the script. There are actually more problems: is this one script or two? It looks s if you stuck two scripts together without proper integration
    – Dirk
    Commented Aug 21, 2018 at 7:53

1 Answer 1


Running sudo apt-get install python-pip isn't going to install PiCamera for Python 3, as you stated that you expected. That command installs the Python 2 version of Pip, the Python package manager — so it's installing the wrong package, and for the wrong version of Python too!

As noted in the PiCamera documentation you should just directly install the PiCamera module for Python 3:

sudo apt-get install python3-picamera

Generally packages named python-X are Python 2 modules, and python3-X packages are for Python 3. Python 2 modules are not compatible with Python 3, so even if you have a working Python 2 version of the module you want, it won't necessarily work with Python 3.

As Dirk points out in the comments, it's unclear exactly what your code is supposed to do, and why you are importing things twice.

Consider reviewing the Quick Start example; the given way to instantiate a PiCamera is

import picamera

camera = picamera.PiCamera()

The following will also work:

from picamera import PiCamera
camera = PiCamera()

But note the first import (the bit after from) must be lower case.


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.