3

Jupyter学习笔记:IPython魔法命令

 3 years ago
source link: https://www.biaodianfu.com/ipython.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Jupyter与IPython的关系

IPython是个比默认的Python Shell好用得多的Python交互命令行界面,支持变量自动补全、自动缩进、Bash Shell命令,还内置了许多很有用的功能和函数。大大提高交互式计算和软件开发的生产率。

Jupyter Notebook(前身是IPython Notebook)是一个基于Web的交互式计算环境,用于创建Jupyter Notebook文档。Notebook是web based IPython封装,使得可以在浏览器上使用IPython。得益于web端数据与表现分离的模式和富文本特性,使得Notebook可以将IPython被命令行限制的数据表现能力,通过浏览器得到完全展现。

2014年,Fernando Pérez宣布从IPython中衍生出一个名为Jupyter的项目。IPython继续以Python shell和Jupyter内核的形式存在,而IPython Notebook和其他与语言无关的部分移到了Jupyter名下。Jupyter是语言无关的,它的名称是对Jupyter支持的内核编程语言的引用,这些语言是Julia、Python和R,它支持几十种语言 (页面存档备份,存于互联网档案馆)的执行环境(也就是内核),这些语言包括Julia、R、Haskell、Ruby,当然还有Python(通过IPython内核)。

IPython魔法命令

IPython提供了许多魔法命令,使得在IPython环境中的操作更加得心应手。魔法命令都以%或者%%开头,以%开头的成为行命令,%%开头的称为单元命令。行命令只对命令所在的行有效,而单元命令则必须出现在单元的第一行,对整个单元的代码进行处理。

执行%lsmagic可以查看关于各个命令的说明,而在命令之后添加?可以查看该命令的详细说明。

Available line magics:
%alias %alias_magic %autocall %automagic %autosave %bookmark %cd %clear %cls %colors %config %connect_info %copy %ddir %debug %dhist %dirs %doctest_mode %echo %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %macro %magic %matplotlib %mkdir %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %ren %rep %rerun %reset %reset_selective %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode
Available cell magics:
%%! %%HTML %%SVG %%bash %%capture %%cmd %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile
Automagic is ON, % prefix IS NOT needed for line magics.
Available line magics:
%alias  %alias_magic  %autocall  %automagic  %autosave  %bookmark  %cd  %clear  %cls  %colors  %config  %connect_info  %copy  %ddir  %debug  %dhist  %dirs  %doctest_mode  %echo  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %macro  %magic  %matplotlib  %mkdir  %more  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %popd  %pprint  %precision  %profile  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %ren  %rep  %rerun  %reset  %reset_selective  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%cmd  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.

常用魔法命令:

  • %magic 显示所有魔法命令的详细文档
  • %time statement 报告statement的执行时间
  • %timeit statement 多次执行statement,以计算平均执行时间
  • %prun statement 通过cProfile执行statement,并打印分析器的结果
  • %run script.py 在ipython中执行一个外部的脚本文件
  • %pdb 在异常发生后自动计入调试器
  • %hist 查询输入的历史
  • %debug 从最新的异常跟踪的底部进入交互式调试器
  • %env 显示系统环境变量
  • %reset 清空 namespace
  • %cd 进入目录命令

参考链接:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK