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

StackOverflow https://stackoverflow.com/questions/21541319

  •  06-10-2022
  •  | 
  •  

문제

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?

도움이 되었습니까?

해결책

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.)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top