IPython 使用技巧

IPython 简介

IPython是一种基于Python的互动式直译器。相较于原生的Python Shell,IPython提供了更为强大的编辑和互动功能。 ——wiki

"安装方法"

pip install ipython       # windows
sudo apt install ipython  # linux 

更强!更优美! 更高效 !


我记录了一些我常用技巧如下:

1. 显示帮助文档

In [1]: import os

In [2]: os.system?
Signature: os.system(command)
Docstring: Execute the command in a subshell.
Type:      builtin_function_or_method

In [3]: help(os.system)

In [4]: os.system??

在开头或结尾添加 “?” 来显示任何函数、模块和变量的文档

如果您想查看函数(或类/模块)的完整源代码,请改用两个问号,有些函数无法查看源代码

2. 通配符搜索

In [1]: import os

In [2]: os.*dir*?
os.__dir__
os.chdir
os.curdir
os.fchdir
os.listdir
os.makedirs
os.mkdir
os.pardir
os.removedirs
os.rmdir
os.scandir
os.supports_dir_fd

In [3]: os.*?

3. 运行 shell 命令

In [1]: !pwd
/home/user

In [2]: !ls -al
total 252
drwxr-xr-x 19 user user   4096 Jun 18 09:41 .
drwxr-xr-x  3 root root   4096 Mar 29 13:26 ..
-rw-------  1 user user     49 Jun 11 10:57 .Xauthority
-rw-------  1 user user  11809 Jun 18 10:16 .bash_history
-rw-r--r--  1 user user    220 Mar 29 13:26 .bash_logout
-rw-r--r--  1 user user   3887 Jun 17 17:11 .bashrc
drwxr-xr-x  5 user user   4096 Apr  5 14:29 .cache
drwx------  5 user user   4096 Jun 12 14:58 .config
-rw-r--r--  1 user user     45 Mar 30 13:26 .gdbinit

In [3]: cd .ssh/
/home/user/.ssh

In [4]: pwd
Out[4]: '/home/user/.ssh'

在不离开 IPython 的情况下运行 shell 命令——只需要在它前面加上感叹号。最常见的 shell 命令,如 ls, pwd,cd。

即使没有加 !也能工作(除非你有一个同名的 Python 函数)

4. 使用 %cd 在文件系统中移动

In [1]: pwd
Out[1]: '/home/user/.ssh'

In [2]: %cd ..
/home/user

%cd魔术命令在文件系统中移动

5. 前面命令的输出

In [1]: pwd
Out[1]: '/home/qwq'

In [2]: pwd
Out[2]: '/home/qwq/.ssh'

In [3]: _
Out[3]: '/home/qwq/.ssh'

In [4]: _1
Out[4]: '/home/qwq'

前面所有命令的结果都存储在变量中_number中(第一个命令的结果存在_1中)、(第二个命令存在_2中),不加数字代表前一个命令结果。

6. 编辑任何函数或模块

In [1]: import random

In [2]: random.randint(1,100)
Out[2]: 80

In [3]: %edit random.randint

%edit 可以用来编辑任何 Python 函数。任何函数 - 来自您的代码的函数,来自使用 pip 安装的包,甚至是内置函数。甚至不需要知道该函数位于哪个文件中。只需指定名称(必须先导入它),IPython 会为您找到它。

7. 执行用另一种语言编写的代码

In [1]: %%bash
   ...: for i in {1..5};
   ...: do echo $i;
   ...: done
   ...:
   ...:
1
2
3
4
5

想在不离开 IPython 的情况下执行一些用另一种语言编写的代码。 IPython 开箱即用地支持 Ruby、Bash 或 JavaScript。当您安装额外的内核时,甚至可以支持更多的语言!

只需键入%%bash,编写一些 Bash 代码,然后按 Enter 两次,IPython 就会毫无问题地运行它。它也适用于 Python2 ( %%python2)。

8. 将会话保存到文件

In [1]: a = 100

In [2]: b = 200

In [3]: c = a + b

In [4]: c
Out[4]: 300

In [5]: %save filename.py 1-4
The following commands were written to file `filename.py`:
a = 100
b = 200
c = a + b
c

9. 列出所有变量

In [1]: a = 100

In [2]: name = "Sebastian"

In [3]: squares = [x*x for x in range(100)]

In [4]: squares_sum = sum(squares)

In [5]: def say_hello():
   ...:     print("Hello!")
   ...:

In [6]: %whos
Variable      Type        Data/Info
-----------------------------------
a             int         100
name          str         Sebastian
say_hello     function    <function say_hello at 0x111b60a60>
squares       list        n=100
squares_sum   int         328350

10. IPython 脚本

$ ls
file1.py    file2.py    file3.py    file4.py    wishes.ipy

$ cat wishes.ipy
files = !ls
# Run all the files with .py suffix
for file in files:
    if file.endswith(".py"):
        %run $file

$ ipython wishes.ipy
Have a
Very Merry
Christmas!
🎄🎄🎄🎄🎄🎄

可以执行包含 IPython 特定代码的文件(以 为前缀的 shell 命令!或以 为前缀的魔术方法%)。只需使用“.ipy”扩展名保存文件,然后将其传递给ipython命令。


这篇文章也是我在学习中的记录,主要参考了 [25 IPython Tips for Your Next Advent of Code](https://switowski.com/blog/25-ipython-tips-for-your-next-advent-of-code) ,其中还有其他酷酷的 tips,只是觉得可用性不强便没有记录
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值