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.

Steve Robillard
  • 34,687
  • 17
  • 103
  • 109
luisgepeto
  • 213
  • 1
  • 2
  • 4
  • You should be able to work remotely with code on the pi, see http://raspberrypi.stackexchange.com/q/36398/5538, joan also left a comment along these lines on her answer. – goldilocks Jun 22 '16 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 09 '22 at 21:58

5 Answers5

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.

skidoo
  • 351
  • 2
  • 2
6

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

joan
  • 71,024
  • 5
  • 73
  • 106
  • thanks. i wil need to manually change the code on deploy. – luisgepeto Aug 05 '15 at 07: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 05 '15 at 07:36
3

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/

AiC
  • 91
  • 2
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.

Rishi Acharya
  • 37
  • 1
  • 5
  • 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 '19 at 09: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??? – Captain Man Apr 27 '21 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.

goldilocks
  • 58,859
  • 17
  • 112
  • 227
Soundar
  • 1
  • 1