01.05 shell command
osx:~ $ echo "hello world" # echo is like Python's print function
hello world
osx:~ $ pwd # pwd = print working directory
/home/jake # this is the "path" that we're sitting in
osx:~ $ ls # ls = list working directory contents
notebooks projects
osx:~ $ cd projects/ # cd = change directory
osx:projects $ pwd
/home/jake/projects
osx:projects $ ls
datasci_book mpld3 myproject.txt
osx:projects $ mkdir myproject # mkdir = make new directory
osx:projects $ cd myproject/
osx:myproject $ mv ../myproject.txt ./ # mv = move file. Here we're moving the
# file myproject.txt from one directory
# up (../) to the current directory (./)
osx:myproject $ ls
myproject.txt
shell in ipython
ipython
In [1]: !ls
myproject.txt
In [2]: !pwd
/home/jake/projects/myproject
In [3]: !echo "printing from the shell"
printing from the shell
passing values to and from the shell
ipython
In [4]: contents = !ls
In [5]: print(contents)
['myproject.txt']
In [6]: directory = !pwd
In [7]: print(directory)
['/Users/jakevdp/notebooks/tmp/myproject']```
Communication in the other direction–passing Python variables into the shell–is possible
using the {varname} syntax:
ipython
In [9]: message = "hello from Python"
In [10]: !echo {message}
hello from Python
shell-related magic commands
If you'd like to change the working directory in a more enduring way,
you can use the %cd magic command:
ipython
In [11]: !pwd
/home/jake/projects/myproject
In [12]: !cd ..
In [13]: !pwd
/home/jake/projects/myproject
ipython
In [14]: %cd ..
/home/jake/projects
01-06 Errors nad Debugging
Controlling Exceptions: %xmode
%xmode takes a single argument, the mode, and there are three possibilities:
Plain, Context, and Verbose.
The default is Context, and gives output like that just shown before.
Plain is more compact and gives less information:
The Verbose mode adds some extra information,
including the arguments to any functions that are called:
%xmode Verbose
Debugging
%debug magic command
If you'd like the debugger to launch automatically whenever an exception is raised,
you can use the %pdb magic function to turn on this automatic behavior:
%xmode Plain
%pdb on
Partial list of debugging commands
There are many more available commands for interactive debugging than we’ve listed here; the following table contains a description of some of the more common and useful ones:
For more information, use the help command in the debugger, or take a look at ipdb’s online documentation.