Using IPython programming tool under linux

One. Install IPython tools

IPython is a third-party tool that is installed using the following command:

[root@python ~]# pip install ipython        #Install ipython
[root@python ~]# ipython             #Execute IPython to enter IPython interactive interface
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)                                                       
In [3]: import os                                                        
In [4]: os.getlogin()                                                    
Out[4]: 'root'

Ippython has the following features:

  • Support syntax highlighting;
  • Auto indent;
  • Tab completion;
  • Get help information quickly;
  • Search history;
  • Execute shell command;

2. magic function

IPython provides many powerful functions, all of which start with "%". Such powerful functions that start with "%" are called magic functions in IPython. Magic functions mainly provide enhanced functions for IPython, interact with the operating system, manipulate the input and output of users, and configure IPython.

The list of magic functions can be obtained by the following instructions, as follows:

In [28]: %lsmagic                #Any of the following functions can be added? No. to query the function's corresponding functions, such as:% save?
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. Using IPython to interact with the operating system

IPython can better interact with the operating system. When using Python for interactive programming, you can execute linux commands without exiting the Python shell. The% cd and% pwd in the magic function are equivalent to the cd command and pwd command under Linux. In addition, in IPython, you can execute any Linux command in the form of "! cmd", as follows:

#Direct interaction
In [31]: %pwd                                                            
Out[31]: '/root'

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

In [33]: %pwd                                                            
Out[33]: '/usr/src'
In [42]: !ls / | grep e                                                  
#You can also use assignment as follows:
In [43]: data=!df                                                        

In [44]: data                                                            
['file system               1K-block    Already used     Available used% Mount point',
 '/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']

Tags: Python IPython Linux pip

Posted on Mon, 03 Feb 2020 09:34:12 -0500 by rkrass