温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

linux下使用IPython编程工具

发布时间:2020-02-14 11:10:54 来源:网络 阅读:412 作者:warrent 栏目:编程语言

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']
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI