11

I want to develop a Raspberry python script from the windows side, for several reasons: I want to keep all the code (more) secure on my computer, in case I fry the RPi/try another project/reinstall the Pi. Also my main computer with the two screen is on windows and I find that using vim over SSH(with putty) is not a good way of doing it(personal opinion, but I don't find it ergonomic).

I found this: http://visualgdb.com/tutorials/raspberry/ which seems to allow me to developp a C+ application for the Raspberry, compile it remotly and so on.

Is there something equivalent for the python script development?

Which will basically would allow me :

  1. To develop in python on my windows
  2. To hit "run" button and have the script(s) deployed to the RPi, and launched
  3. (Dream-solution) To put breakpoints and debug?
J4N
  • 293
  • 1
  • 3
  • 9

2 Answers2

4

Check out PyCharm. I've been a happy paying customer for years, however a free community edition is also available.

There is a file watcher plugin that will copy files over to a remote machine (your RPi). And it has the ability to perform remote debugging.

wire up debugging modules

With respect to setting up PyCharm remote debugging, first thing to do is wire up pycharm-debug.egg. I'm on a Mac and in my case the file is at /Applications/PyCharm.app/Contents/debug-eggs/pycharm-debug.egg. I copied it over to my RPi with the command:

scp /Applications/PyCharm.app/Contents/debug-eggs/pycharm-debug.egg pi@mediapi:

This file needs to be somewhere in Python's path in order for it to be found. I looked for a suitable place by running the following:

pi@mediapi ~ $ python -c 'import sys; print sys.path'
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/pymodules/python2.7']

From that list I believe /usr/local/lib/python2.7/dist-packages/ is most suitable (because it's in /usr/local), so:

sudo mv pycharm-debug.egg /usr/local/lib/python2.7/dist-packages/

And we need a .pth file to inject it into the path:

pi@mediapi ~ $ echo /usr/local/lib/python2.7/dist-packages/pycharm-debug.egg | sudo tee -a /usr/local/lib/python2.7/dist-packages/pycharm-debug.pth

Finaly, verify the setup:

pi@mediapi ~ $ python -c 'import pydevd; print pydevd'
<module 'pydevd' from '/usr/local/lib/python2.7/dist-packages/pycharm-debug.egg/pydevd.py'>

Cool, the debugging modules are wired in. On to debugging a script. I'm going to debug the listener script I wrote for this post.

PyCharm setup

In PyCharm, go to Run -> Edit Configurations...

Hit the + sign on the top left and select Python Remote Debug. Simply give it a name, remote debug and you're done here; hit ok.

remote debug configuration

Now click on Run -> Debug 'remote debug' and you'll see a message in the debug console, Use the following code to connect to the debugger

Copy the lines, in my case they ended up looking like this:

import pydevd
pydevd.settrace('localhost', port=56112, stdoutToServer=True, stderrToServer=True)

Paste them at the top of the file you're debugging and save the file.

Without using the file sync plugin I copy the files using rsync:

[berto@g6]$ rsync -av ./ pi@mediapi:broadcast/

Now, notice the configuration says localhost. The best way to go about wiring up the debugger is using SSH's port forwarding capabilities so that the RPi can be on your desk or around the globe and it works the same way. SSH into the RPi using this command to link the remote port to the local port:

[berto@g6]$ ssh -R 56112:localhost:56112 pi@mediapi

PyCharm should show you a message Waiting for process connection...

waiting for process connection

On the RPi, run the program and you'll see the debugger kick in. By default it pauses on the line right after the pydevd line, which you can change in the config if you wish.

And at that point you're remotely debugging! :)

breakpoint on remote script

berto
  • 1,231
  • 1
  • 9
  • 12
  • I will take a look, I already have Resharper for a while :) – J4N Jul 10 '15 at 11:28
  • Could you maybe give a tutorial or brief overview of how to setup PyCharm for remote debugging on the pi? I've been having problems getting it to work. – James Mertz Jul 10 '15 at 13:45
  • The answer has been updated with a remote debugging overview. @KronoS – berto Jul 10 '15 at 20:43
1

One option is simply to store your Python scripts on a network drive.

For instance I keep all my code on a laptop (suitably backed up) under a directory called code. I export the directory to the network using NFS.

On the Pi the laptop NFS directory is mapped to /code.

That allows me to develop on the laptop (or Pi) and run the script on the Pi simply by ssh'ing to the Pi and using /code/script.py.

Generally I find that Python reveals enough about my errors with its backtrace for me not to need additional debugging tools.

joan
  • 71,024
  • 5
  • 73
  • 106
  • I already tought that network shares were a possibility, but it still require me to go on the Pi, launch and stop everytime, and will never allow debugging. – J4N Jul 10 '15 at 08:07
  • 2
    @J4N I'd typically have a permanent SSH terminal window open on the laptop to the Pi. So running the script would just be up arrow, return. I've not had problems debugging. As I say the backtrace will supply plenty of information and for anything else a few prints scattered around the script usually finds the problem. – joan Jul 10 '15 at 09:56