gintonik
Posts: 35
Joined: Fri Apr 22, 2016 4:09 pm

ImportError: No module named sh

Tue Oct 03, 2017 5:13 pm

Hi,

i have a problem and doesn´t find a solution. I try to cotrol a canon powershot by using a rpi. the programm gphoto2 brings everything i need to go. the single commands executed from bash works fine. Then i want to build a python script for controlling the cam. i saw a Video on https://www.youtube.com/watch?v=1eAYxnS ... 42&list=WL and copied everything he told, thats the basement of my own project.

After executing python script this Message will be shown:

Traceback (most recent call last):
File "capture.py", line 3, in <module>
from sh import gphoto2
ImportError: No module named sh


I start project with installing rasbian strech to a new SD-Card on a Rpi Zero W.
Update and upgrade, installing "sudo apt-get install python3-pip" and "sudo pip3 install sh"
already done. I tried for searching a solution but doesn´t get a result.

Here´s my used Script:

Code: Select all

from time import sleep
from datetime import datetime
from sh import gphoto2 as gp
import os, signal, subprocess

#kill gphoto2 process
#starts whenever camera connect

#def killgphoto2process():
#    p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
#    put, err = p.communicate()
#    
 
# search for the line that the process we want to kill
#    for line in out.spitlines():
#        if b'gvfsd-gphoto2' in line:
            # kill the process!
#            pid = int(line.split(NONE, 1) [0])
#            os.kill(pid, signal.SIGKILL)
            
shot_date = datetime.now().strftime("%Y-%m-%d-")
shot_time = datetime.now().strftime("%Y-%m-%d %h:%m:%s")
picID = "PiShots"

clearCommand = ["--folder", "/store_00010001/DCIM/100CANON", \
                "-R", "--delete-all-files"]
triggercommand = ["--trigger-capture"]
downloadCommand = ["--get-all-files"]

foldername = shot_date + picID
save_location = "/home/pi/Pictures/gphoto" + foldername

def createSaveFolder():
    try:
        os.makedirs(save_location)
    except:
        print ("Failed to create new directory.")
    os.chdire(save_location)

def captureImages():
    gphoto2(triggercommand)
    sleep(3)
    gphoto2(downloadCommand)
    gphoto2(clearCommand)

def renameFiles(ID):
    for filename in os.listdir("."):
        if len(filename) < 13:
            if filename.endswith(".JPG"):
                os.rename(filename, (shot_time + ID + ".JPG"))
                print("renamed the JPG")
            elif filename.endswith(".CR2"):
                os.rename(filename, (shot_time + ID + ".CR2"))
                print ("renamed the CR2")

#killgphoto2Process()
gphoto2(clearCommand)
createSaveFolder()
captureImages()
renameFiles(picID)
    
Don´t know what can i do anything...

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: ImportError: No module named sh

Tue Oct 03, 2017 5:49 pm

You've installed the module for python 3 but I suspect you're running your script with python 2. Have you tried running your script with python3 e.g. "python3 capture.py"?
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

gintonik
Posts: 35
Joined: Fri Apr 22, 2016 4:09 pm

Re: ImportError: No module named sh

Tue Oct 03, 2017 6:09 pm

Hi elParaguayo,

you damm crazy guy!

Thats it!

Brilliant!

It´s a long time ago last time a written a script and i never used the version-number behind python else.
No i start it with and it works !!!

Code: Select all

 SUDO PYTHON3 SCRIPT.PY
THANKS THANKS THANKS !!!!

learned something again...

sameenashaikh29
Posts: 7
Joined: Thu Jan 11, 2018 7:15 am

Re: ImportError: No module named sh

Thu Jan 18, 2018 12:38 pm

Hi,
How can i install sh for pip2 or python 2 as i wan to work with python 2 itself for rest part of the code. As my dronekit should be done with python2 itself..

Thanks & Regards
Sameena Shaikh

Return to “Python”