Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run IPython script from the command line - syntax error with magic functions, %

I want to run IPython from the command line. However, I get a syntax error on the first line, importing pylab with the magic function %pylab is giving a syntax error on the %. The command I am using is simply ipython -i script.py.

Any ideas how to solve this?

like image 571
Chet Avatar asked Feb 04 '14 01:02

Chet


People also ask

How do I run IPython from command-line?

The easiest way is to run easy_install ipython[all] as an administrator (start button, type cmd , shift+right click on “cmd.exe” and select “Run as administrator”). This installs the latest stable version of IPython including the main required and optional dependencies.

What is IPython magic command?

Magic commands or magic functions are one of the important enhancements that IPython offers compared to the standard Python shell. These magic commands are intended to solve common problems in data analysis using Python. In fact, they control the behaviour of IPython itself.

How do you use %% Timeit in Jupyter?

The “%timeit” is a line magic command in which the code consists of a single line or should be written in the same line for measuring the execution time. In the “%timeit” command, the particular code is specified after the “%timeit” is separated by a space.


1 Answers

You need to name your file script.ipy. When it ends in .ipy it can contain ipython syntax.

From ipython --help:

Usage

    ipython [subcommand] [options] [files]

    If invoked with no options, it executes all the files listed in sequence
    and exits, use -i to enter interactive mode after running the files.  Files
    ending in .py will be treated as normal Python, but files ending in .ipy
    can contain special IPython syntax (magic commands, shell expansions, etc.)
like image 138
ditkin Avatar answered Oct 10 '22 04:10

ditkin