Repository: spyder-ide/spyder

Issue:
Run in console's namespace with existing kernel not working
I cannot access variables from an existing kernel hooked to spyder from a spyder script.
I am trying to use spyder with an embedded kernel from a C# application and there are things in the kernels namespace I need to access. They are accessible in the console but not from scripts. I'd be happy for any way to accomplish this.

**Repro**

Start a spyder kernel:
      python -m spyder_kernels.console

In spyder connect a console to this existing kernel
Set configuration per file to use the current console and "Run in console's namespace"

In the console set a variable:
In []:   x = 5

Run a script in spyder to print that variable:
print(x}

You get:
   NameError: name 'x' is not defined

This works if I use a console made in spyder but not with an existing kernel.


Discussion:
I see that with the console made in spyder the script is run with: 
runfile('C:/home/.spyder-py3/temp.py', wdir='C:/home/.spyder-py3', current_namespace=True)

Whereas in the existing kernel it's:
%run "C:\home\.spyder-py3\temp.py"

Can I get spyder to use runfile with the existing kernel?

Edit:
Sadly my embedded kernel doesn't seem to have 'runfile'. I'm using:
    IPKernelApp.instance(kernel_class=SpyderKernel)

How would I build a spyder kernel that has 'runfile'? The kernel from 'python -m spyder_kernels.console' has it.

Edit2:
Ok, I see that spyder_kernels/console/start.py does all kinds of stuff like adding 'customizations', including runfile. I could recreate a bunch of that stuff in my app if needed but perhaps there is a simpler way to just get the kernel variables into the script namespace. And spyder would still use %run anyway.
@davidaue, thanks for reporting. The fix shouldn't be that hard.

@dalthviz, please take a look at this one. We should use `runfile` for external kernels that are in fact spyder-kernels (that's decided in the IPython console plugin module).
Just for reference: I think this problem

https://stackoverflow.com/questions/41530184/spyder-run-script-located-on-remote-server

will be fixed when we fix this too.

Resolving patch:
diff --git a/spyder/plugins/ipythonconsole/plugin.py b/spyder/plugins/ipythonconsole/plugin.py
--- a/spyder/plugins/ipythonconsole/plugin.py
+++ b/spyder/plugins/ipythonconsole/plugin.py
@@ -442,7 +442,8 @@ def run_script(self, filename, wdir, args, debug, post_mortem,
 
         if client is not None:
             # Internal kernels, use runfile
-            if client.get_kernel() is not None:
+            if (client.get_kernel() is not None or
+                    client.shellwidget.is_spyder_kernel()):
                 line = "%s('%s'" % ('debugfile' if debug else 'runfile',
                                     norm(filename))
                 if args:
@@ -454,7 +455,7 @@ def run_script(self, filename, wdir, args, debug, post_mortem,
                 if console_namespace:
                     line += ", current_namespace=True"
                 line += ")"
-            else: # External kernels, use %run
+            else:  # External, non spyder-kernels, use %run
                 line = "%run "
                 if debug:
                     line += "-d "


Test patch: