2

Unfortunately I am not a programmer and I am encountering the error:

$ python3 code.py
   File "code.py", line 7
     % pylab inline
     ^
SyntaxError: invalid syntax

The original code is on that link and is the first part. I'm not using Junyper.

Anyone can help me?

  • the % pylab inline is a [jupyter magic method](https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-pylab). If you're not using jupyter, then you need to leave that out – G. Anderson Apr 16 '20 at 18:06
  • is there no way to run without being in Junyper? – Marcelo João Apr 16 '20 at 18:11
  • 1
    related: [%precision 2 on Python](https://stackoverflow.com/q/50375504/674039) – wim Apr 16 '20 at 18:11

2 Answers2

3

%pylab is an IPython magic for matplotlib. It's equivalent to this:

import numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot

from IPython.display import display
from IPython.core.pylabtools import figsize, getfigs

from pylab import *
from numpy import *

The positional argument inline is specifying a matplotlib backend. Supported choices here depend on your platform and runtime (see matplotlib.rcsetup.all_backends for options).

If you're not using Jupyter notebooks, you can do the same in an IPython session. You'll need to have installed matplotlib in your Python environment.

wim
  • 338,267
  • 99
  • 616
  • 750
  • Yes, I was. I tried to execute your suggestion, but the same error happens... – Marcelo João Apr 16 '20 at 18:20
  • It will run in **IPython**. It will not run in regular python unless you remove/replace the magic. – wim Apr 16 '20 at 18:21
  • As I said I'm not a programmer, I'm trying to use the code created to analyze data... I run the program in the Ubuntu terminal using the command: python3 code.py – Marcelo João Apr 16 '20 at 18:30
  • You can use `python3 -m IPython code.py`. Or just use jupyter notebooks. Or replace the magics in the source code. Up to you :) – wim Apr 16 '20 at 18:34
1

You should run the program in Jupyter, or at least IPython. The % is IPython-specific syntax, and pylab is an IPython magic command for using matplotlib. Also note the file extension: .ipynb is a Jupyter notebook (formerly called IPython notebook), while a Python file would have a .py extension.

wjandrea
  • 28,235
  • 9
  • 60
  • 81