Ignore IPython magic in python -
what best way ignore ipython magic when running scripts using python interpreter?
i include ipython magic in script files because work code interactively. example, autoreload
magic, don't have keep reload
-ing modules after make changes , fix bugs:
%load_ext autoreload %autoreload 2
however, when try run script using usual python interpreter, error:
file "<string>", line 1 %load_ext autoreload ^ syntaxerror: invalid syntax
wrapping ipython magic inside if
statement not work, because incorrect syntax detected before file ran.
so best way python ignore ipython magic?
it's annoying have change scripts whenever want run in python, pdb, sphinx, etc.
for tools can read standard input use grep remove magic lines , pipe result python:
grep -v '^%' magicscript.ipy | python
works bash alias:
alias pynomagic='( grep -v "^%" | python ) < ' pynomagic magicscript.ipy
tools pdb accept filenames called (bash again):
pdb <(grep -v '^%' magicscript.ipy)
Comments
Post a Comment