如何调试python import not found错误?

时间:2017-03-22 18:27:39

标签: python linux raspberry-pi

我正在尝试配置Raspberry Pi 3以在启动时运行python脚本。在教程之后我在etc / init.d中创建了这个脚本:

#! /bin/sh
# etc/init.d/upload_photos_pir

### BEGIN INIT INFO
# Provides:     upload_photos_pir
# Required-Start:   $remote_fs $syslog
# Required-Stop:    $remote_fs &syslog
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
# Short-Description:    Start PIR photo uploads
# Description:      Start PIR photo uploads
### END INIT INFO

case "$1" in
    start)
      echo "starting pir photos upload"
      # run appl
      /home/pi/upload_photos_pir.py
      ;;
    stop)
      echo "stopping pir photos upload"
      # kill app
      killall upload_photos_pir.py
      ;;
    *)
      echo "Usage: etc/init.d/upload_photos_pir {start|stop}"
      exit 1
      ;;
esac

exit 0

运行脚本会产生以下内容:

starting pir photos upload
/home/pi/upload_photos_pir.py: 3: /home/pi/upload_photos_pir.py: import: not found
/home/pi/upload_photos_pir.py: 4: /home/pi/upload_photos_pir.py: import: not found
/home/pi/upload_photos_pir.py: 5: /home/pi/upload_photos_pir.py: import: not found
/home/pi/upload_photos_pir.py: 6: /home/pi/upload_photos_pir.py: import: not found
/home/pi/upload_photos_pir.py: 7: /home/pi/upload_photos_pir.py: import: not found
from: can't read /var/mail/time
from: can't read /var/mail/datetime
from: can't read /var/mail/picamera
from: can't read /var/mail/subprocess
/home/pi/upload_photos_pir.py: 13: /home/pi/upload_photos_pir.py: global: not found
/home/pi/upload_photos_pir.py: 16: /home/pi/upload_photos_pir.py: photosHourlyLimit: not found
/home/pi/upload_photos_pir.py: 17: /home/pi/upload_photos_pir.py: photosTaken: not found
/home/pi/upload_photos_pir.py: 18: /home/pi/upload_photos_pir.py: Syntax error: "(" unexpected

我试图启动的脚本upload_photos_pir.py从python shell运行正常。

我不明白为什么进口不会发生。我是Raspberry Pi和python的新手,所以我对路径,文件位置等方面有很多我不确定,所以我猜这个问题与在#! /bin/sh文件中使用错误的路径有关,但不知道甚至提出正确的问题。

任何人都可以解释可能无法解决的问题,如何调试此问题,或指出我的解释方向?

非常感谢。

1 个答案:

答案 0 :(得分:1)

请注意,您将调用python程序作为

/home/pi/upload_photos_pir.py

我打赌你在这个文件的顶部没有一行看起来像

#!/usr/bin/env python

这意味着你的程序将被sh / bash调用。这些错误消息是bash在尝试(并且失败)解析你的python文件时打印的错误消息。

您可以通过以下两种方式解决问题:

  • 将您的脚本更改为使用python /home/pi/upload_photos_pir.py
  • 进行调用
  • #!/usr/bin/env python添加到文件顶部