Spyder: how to edit a python script locally and execute it on a remote kernel?

I use Spyder 2.3.1 under Windows 7 and have the current iPython 2.3 kernel on the Rasperry Pi RASPBIAN Linux operating system.

I can connect to the external core using a .json file and this tutorial: ipython remote console

But what now? If I run the script (F5), then the kernel tries to output the script as:

%run "C:\test.py" 

ERROR: File u'C:\\test.py' not found.

This returns with an error, ofc, because the script is on my machine under c :, and not on the remote machine / raspberry pi. How can I tell Spyder to somehow copy the script first to a remote computer and execute it there?

If I checked "This is a remote kernel", I can no longer connect to the existing kernel. What does this mean? Will it copy the script via SSH to the remote machine before execution? If I enter SSH login information, I get the error message “The kernel seems to have died unexpectedly”.

+5
source share
3 answers

The tutorial you mentioned is slightly different from the date, since Spyder now has the ability to connect to remote kernels. The "This is a remote kernel" check box includes part of the dialog where you can enter your ssh credentials. (This is necessary if you did not manually open the necessary ssh tunnels to forward the ports of the remote kernel process ...)

In addition, the ipython connection information (json file) should match the remote kernel running on your crimson peak.

Finally, there is currently no means to copy the script lying on your local computer when you hit mileage. The preferred method would actually be the other way around: mount your raspberry pi file system with a tool like sshfs and edit them in place. It is planned to implement the sftp client in Spyder so that it is not required, and you can explore the remote file system from the Spyder file explorer.

Summarizing:

1) assuming you are logged into your raspberry pi, start the local IPython kernel with ipython. It should indicate the name of your json file, which you must copy to your local computer.

2) in spyder on your local computer, connect to the remote kernel with this json file and your ssh credentials

I know this is cumbersome, but this is the first step.

+2
source

After searching the site-packages\spyderlib for the %run keyword, I found a method (in site-packages\spyderlib\plugins\ipythonconsole.py ) that builds the %run command:

  def run_script_in_current_client(self, filename, wdir, args, debug): """Run script in current client, if any""" norm = lambda text: remove_backslashes(to_text_string(text)) client = self.get_current_client() if client is not None: # Internal kernels, use runfile if client.kernel_widget_id is not None: line = "%s('%s'" % ('debugfile' if debug else 'runfile', norm(filename)) if args: line += ", args='%s'" % norm(args) if wdir: line += ", wdir='%s'" % norm(wdir) line += ")" else: # External kernels, use %run line = "%run " if debug: line += "-d " line += "\"%s\"" % to_text_string(filename) if args: line += " %s" % norm(args) self.execute_python_code(line) self.visibility_changed(True) self.raise_() else: #XXX: not sure it can really happen QMessageBox.warning(self, _('Warning'), _("No IPython console is currently available to run <b>%s</b>." "<br><br>Please open a new one and try again." ) % osp.basename(filename), QMessageBox.Ok) 

I added the following code to convert paths after else: # External kernels, use %run

  # ----added to remap local dir to remote dir------- localpath = "Z:\wk" remotepath = "/mnt/sdb1/wk" if localpath in filename: # convert path to linux path filename = filename.replace(localpath, remotepath) filename = filename.replace("\\", "/") # ----- END mod 

now it runs the file on the remote computer when I press F5. I am on Spyder 2.3.9 with samba sharing to z: drive.

+2
source

Another option is to use Spyder cells to send the entire contents of your file to the IPython console. I think this is easier than installing a remote file system with Samba or sshfs (in case it is impossible or difficult to do).

Cells are defined by adding lines of the form # %% to the file. For example, suppose your file:

 # -*- coding: utf-8 -*- def f(x): print(x + x) f(5) 

Then you can simply add a cell at the bottom as shown

 # -*- coding: utf-8 -*- def f(x): print(x + x) f(5) # %% 

and pressing Ctrl + Enter above the cell line, the full contents of your file will be sent to the console and evaluated immediately.

+1
source

Source: https://habr.com/ru/post/1206048/


All Articles