简体   繁体   English

将RPi.GPIO导入python脚本时出错

[英]Error when importing RPi.GPIO into python script

I have a python script called main.py that imports RPi.GPIO library using import RPi.GPIO as GPIO When I run the script using python3 main.py I get an error that states RPi.GPIO was not found. 我有一个名为main.py的python脚本,该脚本使用import RPi.GPIO as GPIO RPi.GPIO库。当我使用python3 main.py运行脚本时,出现一个错误,指出未找到RPi.GPIO。 If I run main.py using sudo python3 main.py then everything runs fine. 如果我使用sudo python3 main.py运行main.py,那么一切运行正常。

I installed RPi.GPIO using a tar.gz file. 我使用tar.gz文件安装了RPi.GPIO。 I copied it to my /home/pi/work directory and extracted the tarball in the same directory. 我将其复制到我的/ home / pi / work目录中,并将压缩文件解压缩到同一目录中。 I then changed to the extracted directory and installed RPi.GPIO using sudo python3 setup.py install. 然后,我转到提取的目录,并使用sudo python3 setup.py install安装了RPi.GPIO。

As I don't normally use linux I don't fully understand the permissions which I am sure is what is causing this issue. 由于我通常不使用linux,因此我无法完全理解权限,而我确定这是导致此问题的原因。 I am guessing that since I installed using sudo that the package is only available to the sudo user. 我猜测自从我使用sudo安装以来,该软件包仅对sudo用户可用。 The problem is I am starting this program from rc.local file and the main.py script wont run at startup with the RPi.GPIO import statement. 问题是我正在从rc.local文件启动该程序,并且main.py脚本在启动时不会使用RPi.GPIO import语句运行。 If I remove the import statement it starts as expected. 如果删除导入语句,它将按预期启动。 Below is the code in the rc.local file that starts the program su -l pi -c '/usr/bin/python3 /home/pi/Work/main.py &' I tried changing su to sudo but that did not work. 以下是rc.local文件中的代码,该文件启动程序su -l pi -c '/usr/bin/python3 /home/pi/Work/main.py &'我尝试将su更改为sudo,但没有用。

Is there a different way I can install RPi.GPIO or change the rc.local script to get this working? 有没有其他方法可以安装RPi.GPIO或更改rc.local脚本以使其正常工作? Also FYI my pi has no internet connection so I can't use APT-GET to uninstall or install the package. 另外,仅供参考,我的pi没有互联网连接,因此无法使用APT-GET卸载或安装软件包。

Also just in case some of you wonder if the package installed properly it has. 另外,以防万一您想知道软件包是否已正确安装。 If I start python with sudo python3 I get >>>. 如果我使用sudo python3启动python, sudo python3得到>>>。 I then type import RPi.GPIO as GPIO I get >>> again. 然后,我再次输入import RPi.GPIO as GPIO我再次>>>。 Then I type GPIO.VERSION it displays the correct version I installed. 然后,我键入GPIO.VERSION它将显示我安装的正确版本。 Any help would be appreciated. 任何帮助,将不胜感激。

UPDATE I did not create the code for the rc.local file and looked a little closer at it. 更新我没有为rc.local文件创建代码,而是仔细查看了一下。 The statement su -l pi -c '/usr/bin/python3 /home/pi/Work/main.py &' I found out changes the user from root to pi and executes the script under the pi user. 我发现su -l pi -c '/usr/bin/python3 /home/pi/Work/main.py &'语句将用户从root更改为pi,并在pi用户下执行脚本。 So I tried changing the statement to su -l sudo -c '/usr/bin/python3 /home/pi/Work/main.py &' thinking that since I can run the main.py by using sudo python3 main.py that changing user from pi to sudo in the rc.local file would execute the file as sudo. 所以我尝试将语句更改为su -l sudo -c '/usr/bin/python3 /home/pi/Work/main.py &' ,因为我可以通过使用sudo python3 main.py运行sudo python3 main.py ,所以在rc.local文件中将用户从pi更改为sudo会将文件作为sudo执行。 It still does not work. 它仍然不起作用。 I then tried removing the su command from the rc.local command and ran like this /usr/bin/python3 /home/pi/Work/main.py & but this also didn't work. 然后,我尝试从rc.local命令中删除su命令,并像/usr/bin/python3 /home/pi/Work/main.py &这样/usr/bin/python3 /home/pi/Work/main.py &但这也没有用。 Does anyone have any suggestions on how I can get this to work? 有人对我如何使它起作用有任何建议吗?

For developing in Python, on Linux or any OS, one would almost always use a virtualenv , one for each python project I want to develop. 对于在Linux或任何操作系统上使用Python开发,几乎总是会使用virtualenv ,对于我要开发的每个python项目都使用一个virtualenv

A virtualenv is easy to set up, and once activated, you can execute your pip install commands without using sudo . virtualenv易于设置,并且一旦激活,您就可以执行pip install命令,而无需使用sudo Try setting up a virtualenv and installing GPIO via pip. 尝试设置一个virtualenv并通过pip安装GPIO。

Even if you don't set up a virtualenv to help with package management for your Python project, you can still use pip to install GPIO, but you'll face the permission issues you're dealing with now. 即使您没有设置virtualenv来帮助您的Python项目进行程序包管理,您仍然可以使用pip安装GPIO,但是您将面临现在要处理的权限问题。

But, you're in luck! 但是,您真幸运! GPIO is already installed on Raspbian. GPIO已安装在Raspbian上。 Open up the Terminal and type python . 打开终端并输入python Once you're in the Python interpreter, type import RPi.GPIO . 进入Python解释器后,输入import RPi.GPIO If you get an error, there is a genuine issue with your installation, but it can be easily overcome by using a virtualenv. 如果出现错误,则说明安装确实存在问题,但是可以使用virtualenv轻松解决。 It may help to know which is your default python, with python --version . 使用python --version可以知道哪个是您的默认python。

Pip is conceptually similar to the apt package manager you've probably used with your Raspberry Pi to install other pieces of software. Pip在概念上类似于您可能与Raspberry Pi一起用于安装其他软件的apt软件包管理器。 It's a package manager for Python, basically a registry of libraries you can install instantly on the command line. 它是Python的软件包管理器,基本上是一个库注册表,您可以在命令行上立即安装它。

The command line is your friend on an RPi, especially when it comes to developing original software. 在RPi上,命令行是您的朋友,特别是在开发原始软件时。

Good luck! 祝好运!

Try to write bash script with sleep before running python script and put it into rc.local 在运行python脚本并将其放入rc.local之前,尝试使用睡眠编写bash脚本

#!/bin/sh

sleep 5
python python_script.py &

Don't forget to make script executable: chmod 755 yourscript.sh 不要忘记使脚本可执行: chmod 755 yourscript.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM