ipython常用的命令
len? ##问号进行帮助的查询
def square(a):
"""return the square of a."""
return a ** 2
## 以上定义一个函数
## ?? 两个问号可以进行查询源代码
square??
## <TAB> 可以进行一个补全
# 通配符号 可以通过 * 来表示
*find*
colab的操作按键可以通过剪藏来进行保存
ipython的快捷键操作
可以Google一下 ipython cheat sheet
ipython的魔法操作
粘贴格子 相关快捷键
%paste %cpaste
运行外部文件 (python)
%run
分析代码的运行时间
%timeit 后面跟随命令 统计时间
帮助的命令
? , %magic, %lsmagic
ipython IN 和 OUT 标号的意义
- 通过标号 可以对输出输入流进行一个计算预估 可以通过标号的方式输出历史的输入输出流
- _ 下划线就是表示之前的输出 多少个下划线表示多少个下划线
- 语句通过加上; 就可以静默输出 而且不会进行记录
- 相关的有趣功能 %history
ipython与命令行之间的关系
osx:projects $ pwd
/home/jake/projects
osx:projects $ ls
datasci_book mpld3 myproject.txt
osx:projects $ mkdir myproject # mkdir = make new directory
osx:projects $ cd myproject/
osx:myproject $ mv ../myproject.txt ./ # mv = move file. Here we're moving the
# file myproject.txt from one directory
# up (../) to the current directory (./)
osx:myproject $ ls
myproject.txt
以上是shell 下面是ipython的实例 通过加上 !(感叹号)* 可以进行命令行命令的执行
In [1]: !ls
myproject.txt
In [2]: !pwd
/home/jake/projects/myproject
In [3]: !echo "printing from the shell"
printing from the shell
- 测试!返回了什么东西
In [4]: contents = !ls
In [5]: print(contents)
['myproject.txt']
In [6]: directory = !pwd
In [7]: print(directory)
['/Users/jakevdp/notebooks/tmp/myproject']
In [8]: type(directory)
IPython.utils.text.SList
- 记住cd的时候用原来的cd命令就可以改变了
In [11]: !pwd
/home/jake/projects/myproject
In [12]: !cd ..
In [13]: !pwd
/home/jake/projects/myproject
In [14]: %cd ..
/home/jake/projects
In [15]: cd myproject
/home/jake/projects/myproject
- 用 {} 来接受值
In [9]: message = "hello from Python"
In [10]: !echo {message}
hello from Python
ipython的automagic函数
ipython 进行除错
- 调节%xmode 为 verbose 或者 plain
- %debug 用pdb除错
- %pdb on 开启pdb模式
Partial list of debugging commands
There are many more available commands for interactive debugging than we've listed here; the following table contains a description of some of the more common and useful ones:
Command | Description |
---|---|
list |
Show the current location in the file |
h(elp) |
Show a list of commands, or find help on a specific command |
q(uit) |
Quit the debugger and the program |
c(ontinue) |
Quit the debugger, continue in the program |
n(ext) |
Go to the next step of the program |
<enter> |
Repeat the previous command |
p(rint) |
Print variables |
s(tep) |
Step into a subroutine |
r(eturn) |
Return out of a subroutine |
计算时间
- %timeit and %time 计算时间
- %timeit 计算循环比较好
- %time 详细了解系统资源使用会比较号
- %% 是为了多行指令的支持
- %prun 会打印出一堆文字 用在jupyter中比较好
- %lprun 也会打印 (一行一行打印) 要导入包
pip install line_profiler
%load_ext line_profiler
%lprun -f ....
- %memit 和 %mprun 思考一下和时间使用的方式很
ipython的深入学习
Web Resources
- The IPython website: The IPython website links to documentation, examples, tutorials, and a variety of other resources.
- The nbviewer website: This site shows static renderings of any IPython notebook available on the internet. The front page features some example notebooks that you can browse to see what other folks are using IPython for!
- A gallery of interesting Jupyter Notebooks: This ever-growing list of notebooks, powered by nbviewer, shows the depth and breadth of numerical analysis you can do with IPython. It includes everything from short examples and tutorials to full-blown courses and books composed in the notebook format!
- Video Tutorials: searching the Internet, you will find many video-recorded tutorials on IPython. I'd especially recommend seeking tutorials from the PyCon, SciPy, and PyData conferenes by Fernando Perez and Brian Granger, two of the primary creators and maintainers of IPython and Jupyter.
Books
- Python for Data Analysis: Wes McKinney's book includes a chapter that covers using IPython as a data scientist. Although much of the material overlaps what we've discussed here, another perspective is always helpful.
- Learning IPython for Interactive Computing and Data Visualization: This short book by Cyrille Rossant offers a good introduction to using IPython for data analysis.
- IPython Interactive Computing and Visualization Cookbook: Also by Cyrille Rossant, this book is a longer and more advanced treatment of using IPython for data science. Despite its name, it's not just about IPython–it also goes into some depth on a broad range of data science topics.
Finally, a reminder that you can find help on your own: IPython's ?
-based help functionality (discussed in Help and Documentation in IPython) can be very useful if you use it well and use it often.
As you go through the examples here and elsewhere, this can be used to familiarize yourself with all the tools that IPython has to offer.