Python Data Science Handbook study

01.05 shell command

references

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值