selianpi
Posts: 6
Joined: Sat Feb 27, 2016 10:18 pm

Write Python code in PC then on RPi

Sat Feb 27, 2016 10:41 pm

Hello guys, this is my first post so please excuse me if that's not the appropriate section, and I'd really like to have a mod transfer it to the right section.

2 weeks ago or so, I bought my Raspberry Pi 2 B as I want to learn Python and make projects with it. I've been able to set it up for starters, write some code to see if the whole idea would appeal to me and boy, it did! But there is one thing I really hate.
I hate having to write the code through RPi (I'm using Raspbian btw) so that I can use or check if everything is alright with GPIO, and I really enjoy writing code on my PC using PyCharm (and since Google/Youtube tutorials are a big source of information for me, I'd really love to use my PC for that - and I can't really do that if keyboard/mouse/monitor is occupied by RPi).

Is there any way I can write and test the code I'm working on, on my PC? What I mean is to make PyCharm understand GPIO functions:
e.g output: GPIO1 is 1 (running)
GPIO2 is 0 (not running)
or maybe be able to run the code and have GPIO working without doing it from inside of Raspbian (if that's even possible).

I don't mind having to install Ubuntu for that. I'm currenty on windows 8.1, and I'm using Python 3.
Thanks in advance!

Heater
Posts: 12735
Joined: Tue Jul 17, 2012 3:02 pm

Re: Write Python code in PC then on RPi

Sat Feb 27, 2016 11:15 pm

My approach to this kind of problem, in whatever language, is to write dummy versions of whatever functions my program needs.

I have no idea of how the the GPIO interface goes in Python but I think you can imagine having something like

x := GPIO.read(2)

Which is using some GPIO module.

So just create your own GPIO module for use on the PC that returns whatever values you like.

This is basically how software testing is done. You can have the dummy module return whatever you like and test that your program works as expected.

Then move the finished program to the Pi and see how it flies.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Write Python code in PC then on RPi

Sat Feb 27, 2016 11:22 pm

selianpi wrote:really love to use my PC for that - and I can't really do that if keyboard/mouse/monitor is occupied by RPi).
Yea, don't do that. Use ssh to network into the pi and do everything from the main box with its nice everything. You don't need a keyboard, mouse or monitor plugged into the pi once you know it's booting correctly.

User avatar
joan
Posts: 14022
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Write Python code in PC then on RPi

Sun Feb 28, 2016 12:09 am

This won't help you, but may be helpful to others with slightly different needs.

My pigpio Python module should work on Windows (not tested on Windows 8). That would allow you to run your scripts under Windows but control the GPIO of a networked Pi.

nikoff90
Posts: 19
Joined: Wed Sep 25, 2013 12:43 am

Re: Write Python code in PC then on RPi

Sun Feb 28, 2016 12:27 am

stderr wrote:
selianpi wrote:really love to use my PC for that - and I can't really do that if keyboard/mouse/monitor is occupied by RPi).
Yea, don't do that. Use ssh to network into the pi and do everything from the main box with its nice everything. You don't need a keyboard, mouse or monitor plugged into the pi once you know it's booting correctly.
+1

This is exactly what i do.

User avatar
liudr
Posts: 672
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Write Python code in PC then on RPi

Sun Feb 28, 2016 5:08 am

SSH is too slow if you use a full screen editor like idle on a 1080p monitor. Local access is still the fastest.
What I do is different from others. I simply don't use PI's GPIO for anything. If you make a mistake, you could destroy your pi. And you never get to develop your code on a different machine. Having a fake GPIO is ok but only makes it harder to detect problems while you are flying blind. Instead I tether an Arduino on Pi and use it to let it do IO. I can move the arduino to my Mac, a few PCs with various computing power (or the lack of it) and my linux boxes. No need to actually write and test code unless you need the pi camera that other systems don't have. Maybe this works for you.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Write Python code in PC then on RPi

Sun Feb 28, 2016 5:54 am

liudr wrote:SSH is too slow if you use a full screen editor like idle on a 1080p monitor. Local access is still the fastest.
.
I certainly wasn't advocating running a large IDE type thing directly on the pi, it would likely want more than a gig to itself. In the current world, generally that can run locally on the main computer, but the python code can then run on the pi by ssh and some way of mounting directories on the pi such as using sshfs.

Now as the world evolves, the SBC that are available are becoming more powerful. Reportedly there will be low cost 2gig/2GHz quad core SBCs out realSoonNow. Would the IDE be able to run in that and then ssh -X out to your main machine?

timrowledge
Posts: 1266
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island
Contact: Website

Re: Write Python code in PC then on RPi

Sun Feb 28, 2016 6:19 am

Code: Select all

sudo apt-get install xrpd 
Find a suitable remote desktop protocol app for your pc/mac
For a mac also do

Code: Select all

sudo apt-get install netatalk
so the Pi file system appears in your finder
Presumably something faintly similar on PC, I don't know.
Configure per instructions
Use pi from pc/mac with full screen access.
End of problem
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

dgordon42
Posts: 755
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Write Python code in PC then on RPi

Sun Feb 28, 2016 6:58 am

Small typo, @timrowledge wrote:
sudo apt-get install xrpd
should be

Code: Select all

sudo apt-get install xrdp
With XRDP installed on your Pi, you can use Remote Desktop in a Windows computer to access your Pi's GUI. More info here.

Dave.

selianpi
Posts: 6
Joined: Sat Feb 27, 2016 10:18 pm

Re: Write Python code in PC then on RPi

Sun Feb 28, 2016 7:50 am

Wow guys, thanks for all the responses. What a nice community!

So to sum up the suggestions:
Use "dummies".
Use SSH.
If I get it right, I get access of the command line of my RPi through Windows, so I can write and test my code (maybe using dummies on PyCharm), then copy/paste it to command line and actually use it as if I was doing it on my PI all the time.
Use XRPD or similar programs Is this something like "Teamviewer"? Meaning I get to use the RPi interface through a window of my PC? I guess that's a nice way of doing things, unless there is an untolerable amount of delay/lag.

I'd like to test SSH first, and if it doesn't work for me I'll check XRPD then as a last solution the "dummies" (maybe I'll try to figure out pigpio too). Should I use https://www.raspberrypi.org/documentati ... windows.md as a tutorial to setup SSH or do you guys have a better way?

dgordon42
Posts: 755
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Write Python code in PC then on RPi

Sun Feb 28, 2016 7:58 am

Should I use https://www.raspberrypi.org/documentati ... windows.md as a tutorial to setup SSH or do you guys have a better way?
That tutorial is good.
Dave.

Heater
Posts: 12735
Joined: Tue Jul 17, 2012 3:02 pm

Re: Write Python code in PC then on RPi

Sun Feb 28, 2016 8:03 am

Despite what I said above I spend a lot of time logged into a Pi over SSH hacking on all manner of things.

Those instruction you linked to are quite reasonable and a lot of people use PUTTY.

I find PUTTY annoying though so I generally install cygwyn onto any Windows machine I'm forced to use and use normal command line ssh from a cygwyn command window. This has the added advantage of bringing a lot of useful Unix tools to Windows but it may be a bit of a heavy weight solution for you.

selianpi
Posts: 6
Joined: Sat Feb 27, 2016 10:18 pm

Re: Write Python code in PC then on RPi

Sun Feb 28, 2016 8:40 am

I got PuTTY to work, and I can acess my PI's terminal. I tried to light up a LED through terminal and sudo python 3, writing the code so I call that a success. The problem that I have is when I try to copy paste code from eg. PyCharm, it won't recognize indentations all that well. Most IDE's use a "Tab" worth of space for indents, but as I know in Terminal only a "Space" is for indents. As a result, copy-pasting code from my IDE to the Terminal so I can test it results in errors, as it doesn't recognize any of my indents. Is there any workaround?

dgordon42
Posts: 755
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Write Python code in PC then on RPi

Sun Feb 28, 2016 8:57 am

I'm not using PyCharm, but most IDE's have an option in "settings" to replace tabs with spaces.
Set this up for 'spaces' on all your IDE's and text editors on all your computers.
Python scripts are a nightmare to work with if you mix tabs and spaces.

Dave.

selianpi
Posts: 6
Joined: Sat Feb 27, 2016 10:18 pm

Re: Write Python code in PC then on RPi

Sun Feb 28, 2016 11:47 am

So guys, here's what I ended up with. Maybe someone that has similar "problems" in the future and looks at this thread, might find it helpful.

I'm using SSH for Terminal things, because it's easy to use the terminal from there (I can copy paste a command from my pc to the terminal).
I followed this guide http://blogs.arcsoftwareconsultancy.com ... etworking/ so that I can transfer files from my PC to Pi remotely (and vice-versa).
I'm using XRDP for complete remote access of my Pi through my windows PC.

All I do now, is write code to any IDE I like on my PC, transfer the .py file created to my Pi, and I can run/and test it on my Pi (in any way I want - IDLE3,NinjaIDE, Terminal or so) through XRDP.

This saves a lot of hassle thanks to you guys. I don't have to plug/unplug keyboard/mouse in order to test my Pi, and I can search tutorials and what not while writing code, what with pi's horse power was slow and sometimes painful. Thanks a lot!

orhnerdm
Posts: 1
Joined: Thu Feb 16, 2017 9:32 pm

Re: Write Python code in PC then on RPi

Thu Feb 16, 2017 9:39 pm

i have a similar problem, I prefer writing code on my laptop but python and library versions cause lots of problems and confusion. how can I install same versions on my laptop and raspberry pi?

for example I have Qt version: Python 3.6 ,5.7.1,SIP version: 4.19,PyQt version: 5.7.1 installed on my laptop but Python 3.4.2,QT 5.3.2,Pyqt 5.3.2,Sip 4.16.4 on my raspberry which are latest version for raspbian.

appreciate any ideas..

Heater
Posts: 12735
Joined: Tue Jul 17, 2012 3:02 pm

Re: Write Python code in PC then on RPi

Fri Feb 17, 2017 12:40 am

Install Debian Jessie on your laptop.

Then I expect almost everything on the laptop to be the same as the Pi.

Makes life very much easier.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Write Python code in PC then on RPi

Fri Feb 17, 2017 2:48 am

orhnerdm wrote:i have a similar problem, I prefer writing code on my laptop but python and library versions cause lots of problems and confusion. how can I install same versions on my laptop and raspberry pi?
You can edit the files on the laptop but run them on the pi very easily with sshfs or some other networking scheme. You can even set the pi's python program to run every time you save a new copy of your file, in an endless loop. So you change something in the editor, save it to the pi, the pi automatically runs it, you see the output, the errors, the success, you make more changes and incrementally you create whatever it is you are doing. This even lets you use whatever hardware features you have on the pi that would be difficult to deal with on the laptop beyond creating test stubs, which would at some point have to be fleshed out on the pi anyway.

pootle
Posts: 320
Joined: Wed Sep 04, 2013 10:20 am
Location: Staffordshire
Contact: Website

Re: Write Python code in PC then on RPi

Fri Feb 17, 2017 8:41 am

I run my pi totally over network. Mostly ssh, and sometimes srdp. Using a wired connection is much better than wifi. I dont use an ide, I run nfs kernel server on the pi and mount /home/pi on my (ubuntu) laptop, and use bluefish on my laptop for editing.

Dont forget Joan's note. You can drive pi gpio from python in your laptop using pigpio, as long as your code is not too time critical.

richrarobi
Posts: 271
Joined: Sun Feb 08, 2015 1:13 pm

Re: Write Python code in PC then on RPi

Sun Mar 05, 2017 8:53 pm

I have to agree with Heater ( :oops: ) - I use Linux Mint XFCE on my Laptop and Desktop PC's, SSH into my Pi's with options as follows:
ssh -Y -l pi a.local
Note the upercase Y. then run pcmanfm with & at the end to keep your terminal session available, You can run most apps from the pcmanfm GUI that pops up on your inux desktop.
N.B. XFCE works fine, but MATE is "Horrible".
I can edit Python (3.4) code using Genie with adequate speed

I also run tkinter on my PC's, identical to that on the pi.

p.s. I wouldn't touch m$ systems unless I absolutely had to. Then I would use putty. You can nearly get X functionality with XMing, but it is easier to get rid of windose and install Linux everywhere. Everything but a few games......

MR.ES1
Posts: 2
Joined: Mon Jul 09, 2018 12:47 am

Re: Write Python code in PC then on RPi

Wed Jul 11, 2018 3:10 pm

nikoff90 wrote:
Sun Feb 28, 2016 12:27 am
stderr wrote:
selianpi wrote:really love to use my PC for that - and I can't really do that if keyboard/mouse/monitor is occupied by RPi).
Yea, don't do that. Use ssh to network into the pi and do everything from the main box with its nice everything. You don't need a keyboard, mouse or monitor plugged into the pi once you know it's booting correctly.
+1

This is exactly what i do.
But after this wich IDE should i use ??

pilover1986
Posts: 8
Joined: Fri Jul 06, 2018 10:05 am

Re: Write Python code in PC then on RPi

Wed Jul 11, 2018 5:33 pm

Nice thread, I'm also confuse on what to use on my Rpi2, but most of the comments suggest to use ssh for the moment and I think it will be best for me since I'm only on a LAN, having a small desk and limited resources can't put more devices on my pi that's why I'll go with the ssh.

Return to “General discussion”