%matplotlib line magic causes SyntaxError in Python script
Solution 1
Line magics are only supported by the IPython command line. They cannot simply be used inside a script, because %something
is not correct Python syntax.
If you want to do this from a script you have to get access to the IPython API and then call the run_line_magic
function.
Instead of %matplotlib inline
, you will have to do something like this in your script:
from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')
A similar approach is described in this answer, but it uses the deprecated magic
function.
Note that the script still needs to run in IPython. Under vanilla Python the get_ipython
function returns None
and get_ipython().run_line_magic
will raise an AttributeError
.
Solution 2
Because line magics are only supported by the IPython command line not by Python cl, use: 'exec(%matplotlib inline)'
instead of %matplotlib inline
Solution 3
The syntax '%' in %matplotlib inline
is recognized by iPython (where it is set up to handle the magic methods), but not Python itself, which gives a SyntaxError.
Here is given one solution.
Solution 4
If you include the following code at the top of your script, matplotlib
will run inline when in an IPython
environment (like jupyter, hydrogen atom plugin...), and it will still work if you launch the script directly via command line (matplotlib
won't run inline, and the charts will open in a pop-ups as usual).
from IPython import get_ipython
ipy = get_ipython()
if ipy is not None:
ipy.run_line_magic('matplotlib', 'inline')
Solution 5
There are several reasons as to why this wouldn't work.
It is possible that matplotlib is not properly installed. have you tried running:
conda install matplotlib
If that doesn't work, look at your %PATH% environment variable, does it contain your libraries and python paths?
John
Updated on July 09, 2022Comments
-
John almost 2 years
I try to run the following codes on Spyder (Python 2.7.11):
# -*- coding: utf-8 -*- import numpy as np import pandas as pd %matplotlib inline import matplotlib.pyplot as plt import matplotlib.cm as cm import tensorflow as tf # settings LEARNING_RATE = 1e-4 # set to 20000 on local environment to get 0.99 accuracy TRAINING_ITERATIONS = 2000 DROPOUT = 0.5 BATCH_SIZE = 50 # set to 0 to train on all available data VALIDATION_SIZE = 2000 # image number to output IMAGE_TO_DISPLAY = 10
But I got this error:
line 10 %matplotlib inline ^ SyntaxError: invalid syntax.
I appreciate if anybody gives me an explanation.
P.S. the code is from Kaggle competition project: Digit Recognizer
-
GLaDOS over 8 yearsI thought this was the problem until I realized spyder is related to ipython, Then again this might be the problem.
-
PineapplePizza over 8 yearsOops. Surprisingly, there are a lot of problems with matplotlib in Spyder.
-
John over 8 yearsThank you for your response. However even I run the script on iPython console, it still gives me the same error.
-
John over 8 yearsThank you for your suggestion. I tried your approach, and got another err: ImportError: No module named moves (because iPython is running the code "from six.moves import zip, reduce") , I am trying to find out why this happens.
-
John over 8 yearsThank you for your info, I ran "conda install matplotlib" and got some updates. But still same err.
-
Always_Beginner over 7 yearsThank you for your suggestion , but I got this error
AttributeError: 'NoneType' object has no attribute 'run_line_magic'
, Can you help Thanks -
MB-F over 7 years@Always_Beginner this sounds like you are using a normal Python shell, in which case
get_ipython()
returnsNone
. Line magics are only supported in IPython. -
Always_Beginner over 7 years@kazemakase Okay but I thought this works for pure python too , as per this answer kazemakase link I thought it works for pure python too.
-
MB-F over 7 years@Always_Beginner in the linked answer 'pure python' refers to the fact that line magics starting with
%
are not correct Python syntax, but using the functionsrun_line_magic
ormagic
are syntactically correct "pure" Python. However, the line magic feature is only supported by the IPython interpreter and not by the normal Python interpreter. -
Carlos Cordoba over 6 yearsSpyder has an option for that. Please take a look at
Preferences > IPython console > Graphics
for it. -
egbit almost 5 years@John, Make sure the script filename ends with .ipy as in: ipython file.ipy . If you use a .py file ending, ipython runs it as a regular python script and it doesn't work. Also, "auto" can be used instead of "inline" for %matplotlib.
-
Shahin Ghasemi almost 3 yearsOkay, if
ipy
is None then this solution doesn't make any sense. -
Carlos Galdino about 2 yearsThis answer together with this check to see if the script is running on a interactive terminal works like a charm.