Jupyter控制台,原名IPython是一个增强的Python解释器。在之前的shell中我们是利用Python解释器来执行我们的Python脚本文件,而Jupyter加强了这个shell,并添加了一些细节,简化处理数据。
通常在你写数据分析脚本时或者编写原型代码时,你需要快速测试一些代码,此时你会在shell环境下执行它,因为这样很快速。Jupyter console与Jupyter notebook 最大的区别是console是在交互模式下运行。当你输入一行代码,他就会立刻执行,然后你会看到结果。如果你想写中篇的代码片断或做数据集的深入探索,Jupyter notebook会更好。如果你想要你写测试代码,或运行快速命令,Jupyter console会更好。
Jupyter项目正处于IPython向Jupyter重塑的过程中,输入jupyter console或者ipython 即可访问jupyter控制台。
~$ ipython
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
Type "copyright", "credits" or "license" for more information.
IPython 3.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: print(10)
10
In [2]: exit
Jupyter 控制台有很丰富的內建帮助系统:
In [3]: dq=5
In [4]: dq?
Type: int
String form: 5
Docstring:
int(x=0) -> integer
int(x, base=10) -> integer
Convert a number or string to an integer, or return 0 if no arguments
are given. If x is a number, return x.__int__(). For floating point
numbers, this truncates towards zero.
If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
# 输入q退出
In [5]: help(dq)
# 输入quit退出
# 输入exit退出Jupyter
与 Jupyter notebook一样,当你第一次加载Jupyter console时会启动一个内核会话(kernel session)。每次在控制台中运行代码时,将会把变量存储在这个会话中,使得你下面运行的代码可以访问这些变量。
~$ ipython
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
Type "copyright", "credits" or "license" for more information.
IPython 3.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
~$ ipython
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
Type "copyright", "credits" or "license" for more information.
IPython 3.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: dq = 5
In [2]: dq_10 = dq * 10
In [3]: exit
%quickref是 Jupyter种一种特殊的命令(Magics ),总是以% 开头,它们使你能够在Python没有执行你的命令前访问Jupyter的特殊功能。下面是一些特殊的magics :
# 新建一个zm.py的脚本,添加了一个变量,一个打印语句
if __name__=="__main__":
zm = "ZM is a good programmer"
print(zm)
In [1]: %run zm.py
ZM is a good programmer
In [2]: %who
zm
Autocompletion(自动补全):当你输入一个变量名称时,按TAB键可以自动匹配所有的变量,自动补全剩余的字符。If you hit TAB after typing a variable name, Jupyter will show you the methods on the variable.
# 输入字母a之后按TAB键,自动回车显示所有匹配的变量名以及函数名
In [1]: a
%alias %autoindent all as
%alias_magic %automagic and ascii
%autocall abs any assert
In [1]: !ls
In [2]: !whoami
dq
In [3]: !ls -a
. .. .bashrc .byobu .cache .ipython .tmux.conf
In [4]:
在python采用复制时不会复制缩进,因此粘贴时会出现错误。因此需要使用paste magics:
- %cpaste – opens a special editing area where you can paste in code normally, without whitespace being a problem. You can type –
alone on a line to exit. After you exit, any code you pasted in will
be immediately executed.- %paste – takes code from your clipboard and runs it in Jupyter. This doesn’t work on remote systems, where Jupyter doesn’t
have access to your clipboard.
# 首先将下面这段代码复制到剪切板
for i in range(10):
if i < 5:
print(i)
else:
print(i * 2)
# 打开ipython,然后在其中输入%cpaste
# 再输入--退出时你的程序就会被执行。
Some specific explorations you can try:
- Explore more of the magics.
- Try using Jupyter to debug exceptions.
- Develop a Python script locally, and see if Jupyter can help with your workflow.