简体   繁体   English

忽略 python 中的 IPython 魔法

[英]Ignore IPython magic in python

What is the best way to ignore IPython magic when running scripts using the python interpreter?使用 python 解释器运行脚本时忽略 IPython 魔法的最佳方法是什么?

I often include IPython magic in my script files because it work with the code interactively.我经常在我的脚本文件中包含 IPython magic,因为它以交互方式与代码一起工作。 For example, with the autoreload magic, I don't have to keep reload -ing the modules after I make some changes and fix bugs:例如,使用autoreload魔法,我不必在进行一些更改和修复错误后继续reload模块:

%load_ext autoreload
%autoreload 2

However, when I try to run this script using a usual python interpreter, I get an error:但是,当我尝试使用通常的 python 解释器运行此脚本时,出现错误:

  File "<string>", line 1
    %load_ext autoreload
    ^
SyntaxError: invalid syntax

Wrapping IPython magic inside an if statement does not work, because incorrect syntax is detected before the file is actually ran.if语句中包装 IPython 魔术是行不通的,因为在文件实际运行之前会检测到不正确的语法。


So what is the best way to get python to ignore IPython magic?那么让 python 忽略 IPython 魔法的最佳方法是什么?

It's annoying to have to change your scripts whenever you want to run then in python, pdb, sphinx, etc.每当您想在 python、pdb、sphinx 等中运行时,都必须更改脚本,这很烦人。

For all tools that can read from standard input you could use grep to remove any magic lines and pipe the result into python: 对于可以从标准输入中读取的所有工具,您可以使用grep删除任何魔术线并将结果通过管道传递到python中:

grep -v '^%' magicscript.ipy | python

Works well as a bash alias: 作为bash别名很好用:

alias pynomagic='( grep -v "^%" | python ) < '
pynomagic magicscript.ipy

Tools like pdb that only accept filenames could be called like this (bash again): 像pdb这样只接受文件名的工具可以这样调用(再次进行bash):

pdb <(grep -v '^%' magicscript.ipy)

You should load such magic in your config file, not in your scripts! 您应该在配置文件中而不是脚本中加载此类魔术! It is just not valid Python. 这只是无效的Python。

Put the following in your ~/.ipython/profile_default/ipython_config.py : 将以下内容放入~/.ipython/profile_default/ipython_config.py

c = get_config()
c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = ['%autoreload 2']
c.InteractiveShellApp.exec_lines.append('print("Warning: disable autoreload in ipython_config.py to improve performance.")')
  1. Create a template file named simplepython.tpl . 创建一个名为simplepython.tpl的模板文件。 Copy the below statements. 复制以下语句。

     {% extends 'python.tpl'%} {% block codecell %} {{ super().replace('get_ipython','#get_ipython') if "get_ipython" in super() else super() }} {% endblock codecell %} 
  2. Save simplepython.tpl . 保存simplepython.tpl

  3. Type in command line: 在命令行中输入:

    jupyter nbconvert --to python 'IPY Notebook' --template=simplepython.tpl --stdout jupyter nbconvert --to python'IPY Notebook'--template = simplepython.tpl --stdout

In case this helps anyone. 万一这对任何人都有帮助。

At least for Databricks, when syncing a notebook with a .py file in Github, a magic function can be specified with a specially formatted comment. 至少对于Databricks而言,在Github中将笔记本与.py文件同步时,可以使用特殊格式的注释指定魔术功能。 Like this: 像这样:

# MAGIC %run ./my_external_file

Spyder gives warning (as given in the picture below), when a coder use this type of code and says that it is not a valid Python code.当编码人员使用此类代码并表示它不是有效的 Python 代码时,Spyder 会发出警告(如下图所示)。

So, in order to use IPython magics, saving files with the.ipy extension may be a solution.因此,为了使用 IPython 的魔法,以 .ipy 扩展名保存文件可能是一种解决方案。

Spyder screenshot蜘蛛截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM