IPython and Shell Commands
Shell Commands in IPython
Any command that works at the command-line can be used in IPython by prefixing it with the !
character.
Example
! echo 'hello world!' # echo is like Python's print function
hello world!
! pwd # = print working dir
/tmp
Passing Values to and from the Shell
Shell commands can not only be called from IPython, but can also be made to interact with the IPython namespace. For example, we can save the output of any shell command to a Python list using the assignment operator.
For example
contents = !ls
contents
['my-project']
directory = !pwd
directory
Note that these results are not returned as lists, but as a special shell return type defined in IPython:
type(directory)
IPython.utils.text.SList
This looks and acts a lot like a Python list, but has additional functionality, such as the grep
and fields
methods and the s
, n
, and p
properties that allow us to search, filter, and display the results in convenient ways.
Communication in the other direction–passing Python variables into the shell–is possible using the {varname}
syntax:
name = 'Ben'
! echo "hello {name}"
hello Ben
Shell-Related ,agic Commands
Shell commands in the notebook are executed in a temporary subshell. If we’d like to execute the shell commands in a more enduring way, we can use the %
magic command.
With %automagic
magic function, we can enable automagic
function. Then available shell-like magic functions, such as %cd
, %cat
, %cp
, %env
, %ls
, %man
, %mkdir
, %more
, %mv
, %pwd
, %rm
, and %rmdir
, can be used without the %
sign.
Magic Commands
There are two types of magic commands
- Line magics: start with
%
- Cell magics: start with
%%
Useful magic commands:
%matplotlib
: activates matplotlib interactive support during an IPython session%run
: runs a Python script from within IPython shell%time
: displays time required by IPython environment to execute a Python expression.%timeit
: uses the Python timeit module which runs a statement 100,000 times (by default) and then provides the mean of the fastest three times.%%writefile
: Write the contents of the cell to a file. The file will be overwritten unless the-a
(–append) flag is specified.