10

I am new to Python so maybe this is not an appropriate question. I am working on a project on Python that will use GPIO functionalities of the raspberry Pi. Right now, I am developing the python code on my local windows environment. This is because it is faster to work this way for me. I have it in a git repository so that when I want to test the code I just log into my raspberry pi and pull the latest code found on the repository.

However, I want to implement the code that will interact with the raspberry pi GPIO pins. I have downloaded the GPIO python source: RPi.GPIO 0.5.11, but I do not know how to add it into my python project on windows in order to use the GPIO functionality.

2
  • You should be able to work remotely with code on the pi, see raspberrypi.stackexchange.com/q/36398/5538, joan also left a comment along these lines on her answer.
    – goldilocks
    Jun 22, 2016 at 16:19
  • Don’t use that non-standard library in the first place. With libgpiod, which has a python bindings, you may experiment in virtual environments of basically any Linux OS.
    – 0andriy
    Jun 9, 2022 at 21:58

5 Answers 5

6

You can't, RPi.GPIO only works on the Raspberry Pi.

2
  • thanks. i wil need to manually change the code on deploy.
    – luisgepeto
    Aug 5, 2015 at 7:09
  • 3
    @LuisBecerril If the Pi is networked you could use NFS/Samba to mount your PC's directory. I use a Linux PC and just export my code directory as /code on the Pi. Alternatively, if you are a programmer, you could use my pigpio Python module. A daemon runs on the Pi but the Python script can run on a Windows, Mac, Linux machine.
    – joan
    Aug 5, 2015 at 7:36
22

If you are just looking to test the whole code and not worry about the actual pins (as windows machines don't have GPIO), then you can fake it. First, in your main python source directory, create a directory named "RPi". In that folder, put an empty text file named __init__.py. This lets python know the folder "RPi" is a package. Also in that folder put a text file named "GPIO.py". In that file put the following:

BOARD = 1
OUT = 1
IN = 1

def setmode(a):
   print a
def setup(a, b):
   print a
def output(a, b):
   print a
def cleanup():
   print 'a'
def setwarnings(flag):
   print 'False'

This puts a pretty blank attribute set that can answer some of the GPIO calls you would most likely be using. Of course, there is no way to actually check whether the calls worked, but you won't get error messages stating the module GPIO doesn't exist. It's pretty hacky, but I use it when I don't feel like ssh into my pi for some quick code updates. You can add any other GPIO attributes you may need.

2
  • 1
    this is actually quite a tidy, simple solution.
    – paddyg
    Jul 27, 2016 at 11:03
  • The file name is wrong because it's been interpreted as markup. It should have two leading and trailing underscores e.g. __init__.py Jul 27, 2016 at 11:36
2

you can use:

GPIOSimulator

This Raspberry Pi emulator simulates some of the functions used in the RPi.GPIO library (using python). The intention of this library is educational.

https://pypi.org/project/GPIOSimulator/

1

PyCharm is a great IDE for Python.

Hidden deep in the settings is a way by which you can download different libraries to import in your program. Anything, from the conventional numpy and kivy, to the Pi standard RPi.GPIO, to externally developed ones like pigpio (that's what I use on my Pi, because it has a daemon and doesn't get interrupted by the system processes).

You can get it on the JetBrains website. There's a free (they call it PyCharm Community) version, and a Pro version, both on the JetBrains website. I find the Community version satisfactory.

The downside is that it's not the most intuitive software, and it takes a while to find out how exactly you do stuff. But after a while, one finally gets the hang of it.

You can type your code on your PC using PyCharm, upload it, and then pull it on your Pi when you want to run it.

2
  • 6
    This answer does not provide any information how to install RPi libraries and thus should not be considered as a valid answer. Just plugging IDE does not provide solution.
    – stiebrs
    Mar 25, 2019 at 9:08
  • "Hidden deep in the settings is a way by which you can download different libraries to import in your program" -- And where is this setting??? Apr 27, 2021 at 18:43
0

You could try gpiozero library instead of RPi.GPIO. It can be installed in your dev environment and tested in Pi.

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