linux下使用IPython編程工具

1、安裝IPython工具

IPython是一個第三方工具,使用以下命令進行安裝:

[root@python ~]# pip install ipython        #安裝ipython
[root@python ~]# ipython             #執行ipython進入IPython交互式界面
Python 3.8.1 (default, Feb  3 2020, 18:39:50) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: sum=0                                                            

In [2]: for i in range(10): 
   ...:     sum+=i 
   ...: print(sum)                                                       
45
In [3]: import os                                                        
In [4]: os.getlogin()                                                    
Out[4]: 'root'

Ipython具有以下特性:

  • 支持語法高亮;
  • 自動縮進;
  • Tab補全;
  • 快速獲取幫助信息;
  • 搜索歷史;
  • 執行shell命令;

2、magic函數

IPython提供了很多功能強大的函數,所有IPython提供的函數都以“%”開頭。以“%”開頭的這類功能強大的函數,在IPython中稱爲magic函數。magic函數主要是爲IPython提供增強的功能、與操作系統交互、操縱用戶的輸入和輸出以及對IPython進行配置。

可以通過以下指令來獲取magic函數列表,如下:

In [28]: %lsmagic                #以下任意函數都可以加?號來查詢函數對應的功能,如:%save?
Out[28]: 
Available line magics:
%alias  %alias_magic  %autoawait  %autocall  %autoindent  %automagic  %bookmark  %cat  %cd  %clear  %colors  %conda  %config  %cp  %cpaste  %debug  %dhist  %dirs  %doctest_mode  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %lf  %lk  %ll  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %lx  %macro  %magic  %man  %matplotlib  %mkdir  %more  %mv  %notebook  %page  %paste  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %quickref  %recall  %rehashx  %reload_ext  %rep  %rerun  %reset  %reset_selective  %rm  %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  %%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.

3、使用IPython與操作系統交互

IPython可以更好的與操作系統交互,在使用Python進行交互編程時,不用退出Python shell 就可以執行linux命令,magic函數裏的%cd和%pwd作用相當於linux下的cd命令和pwd命令,此外,在IPython中,可以通過“!cmd”的形式執行任何Linux命令,如下:

#直接交互
In [31]: %pwd                                                            
Out[31]: '/root'

In [32]: %cd /usr/src/                                                   
/usr/src

In [33]: %pwd                                                            
Out[33]: '/usr/src'
In [42]: !ls / | grep e                                                  
dev
etc
home
media
#亦可以使用賦值的方式進行使用,如下:
In [43]: data=!df                                                        

In [44]: data                                                            
Out[44]: 
['文件系統               1K-塊    已用     可用 已用% 掛載點',
 '/dev/mapper/cl-root 52403200 4779936 47623264   10% /',
 'devtmpfs              917796       0   917796    0% /dev',
 'tmpfs                 933644      84   933560    1% /dev/shm',
 'tmpfs                 933644    9184   924460    1% /run',
 'tmpfs                 933644       0   933644    0% /sys/fs/cgroup',
 '/dev/sda1            1038336  176544   861792   18% /boot',
 '/dev/mapper/cl-home 49250820   33052 49217768    1% /home',
 'tmpfs                 186732      16   186716    1% /run/user/42',
 'tmpfs                 186732       0   186732    0% /run/user/0']
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章