概述
Jupyter Console
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
Getting Help
Jupyter 控制台有很丰富的內建帮助系统:
- You can type ? after starting the console. This will display help about Jupyter. You can exit by typing q.
- You can type %quickref. This is a magic that will tell you some useful commands.
- If you want information about a variable, just type the name of the variable, followed by ?. ——var?
- Type help() to get access to Python help. This will enable you to get help on all the modules and functions currently available. You can quit by typing quit.
- If you want to use the Python help system to get information on a variable, type help(variable_name).——help(var)
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
Persistent Sessions
与 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
Jupyter Magics
%quickref是 Jupyter种一种特殊的命令(Magics ),总是以% 开头,它们使你能够在Python没有执行你的命令前访问Jupyter的特殊功能。下面是一些特殊的magics :
- %run – allows you to run an external Python script. Any variables in the script will be stored in the current kernel session.
- %edit – opens a file editor. Any code you type into the editor will be executed by Jupyter when you exit the editor.
- %debug – if there’s an error in any of your code, running %debug afterwards will open an interactive debugger you can use to trace the error.
- %history – shows you the last few commands you ran.
- %save – saves the last few commands you ran to a file.
- %who – print all the variables in the session.
- %reset – resets the session, and removes all stored variables.
# 新建一个zm.py的脚本,添加了一个变量,一个打印语句
if __name__=="__main__":
zm = "ZM is a good programmer"
print(zm)
- 在ipython中使用%run执行这个文本,使用%who来查看变量:
In [1]: %run zm.py
ZM is a good programmer
In [2]: %who
zm
- 在使用%who查看变量之前,首先要是有%run来执行一个脚本,然后%who显示的就是刚刚执行的那个脚本中的变量。
Autocompletion
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
Accessing The Shell
- 可以在Jupyter 中运行shell命令,只要在命令的前面加上!就可以。比如!ls会显示当前Jupyter 所在目录的所有文件:
In [1]: !ls
In [2]: !whoami
dq
In [3]: !ls -a
. .. .bashrc .byobu .cache .ipython .tmux.conf
In [4]:
Pasting In Code
在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
# 再输入--退出时你的程序就会被执行。
Next Steps
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.
最后
以上就是乐观含羞草为你收集整理的Jupyter(IPython)的全部内容,希望文章能够帮你解决Jupyter(IPython)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复