spyder: IPython magics no longer work in Spyder 4

Issue Report Checklist

  • Searched the issues page for similar reports
  • Read the relevant sections of the Spyder Troubleshooting Guide and followed its advice
  • Reproduced the issue after updating with conda update spyder (or pip, if not using Anaconda)
  • Could not reproduce inside jupyter qtconsole (if console-related)
  • Tried basic troubleshooting (if a bug/error)
    • Restarted Spyder
    • Reset preferences with spyder --reset
    • Reinstalled the latest version of Anaconda
    • Tried the other applicable steps from the Troubleshooting Guide
  • Completed the Problem Description, Steps to Reproduce and Version sections below

Problem Description

If we have ipython magic commands in the editor, running them with shift+enter no longer works with Spyder 4

%who print
# %%

runcell(0, 'C:/users/mark/git/vispy/vispy/app/untitled1.py')
  File "C:\users\mark\git\vispy\vispy\app\untitled1.py", line 1
    %who print
    ^
SyntaxError: invalid syntax

image

What is the expected output? What do you see instead?

It used to work in Spyder 3, has the decision been made to revert things?

Versions

  • Spyder version: 4.0.0
  • Python version: 3.7.3 64-bit
  • Qt version: 5.12.5
  • PyQt version: 5.12.3
  • Operating System name/version: Windows / Linux

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 39 (34 by maintainers)

Most upvoted comments

IPython magics are not valid Python code, so we decided to not support them anymore in Python files. That will avoid the common problem of files that work in Spyder but not outside of it.

If you want to keep using them, you need to run them like

from IPython import get_ipython
get_ipython().magic('who print')

I think @impact27 added support for .ipy files, i.e. IPython script files. In those files you can use magics freely.

and activate the option called Copy full cell contents to the console when running code cells.

I guess I’m trying to say that I’ve seen 4/4 users of Spyder 3 migrate to Spyder 4 and been tripped up by this behavior.

People posting on this thread are to say the least, pretty advanced users of python/spyder. This isn’t the norm.

I agree with your desire to help people write better code, but I would consider this a breaking change in the user experience, and some kind of guidance would be helpful.

It seems that you would prefer users not to use Copy full cell contents to the console when running code cells, but rather to save their files as ipy to keep them portable.

A little guidance will go a long way.

Changing how code is excuted (copied or now) can be ignored. The fact that scripts developed in spyder no longer work with spyder is not really fun.

Alternatively we could create ipy files instead of py files by default?

I don’t agree with this. Magics are more of a convenience for interactive computing than something people should be using in production code.

I could add a fix for that (at least on the editor side for a quick fix)?

I don’t think that’s a good idea because magics are invalid Python syntax.

Yes you can currently use line magics in .ipy files.

There is no support for output of the last command to be printed or cellmagics

If you’re interested in those features I don’t think we have a bug tracking those anymore but you can submit one here: https://github.com/spyder-ide/spyder-kernels/issues

See more debate about it here: https://github.com/spyder-ide/spyder-kernels/issues/139