python命令行工具是什么_分享几个实用的python命令行工具

1. pyenv

pyenv是一个使用简单的python版本管理工具,可以让你简单的切换python版本。python2.x在5年后将不在更新维护,所以是时候熟悉python3了!

那么如何方便的即用python2又可以在方便的切换到python3呢?上pyenv。当然,还有一个叫做p的工具也可以,不过比较之下我更喜欢pyenv,pyenv更强大些,但是p切换版本很方便。

安装:

$ curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash

或者

brew install pyenv

在你的shellrc文件中添加:

export PATH="$HOME/.pyenv/bin:$PATH"

eval "$(pyenv init -)"

eval "$(pyenv virtualenv-init -)"

查看所有可用的命令:

pyenv commands

查看可以安装的python版本:

pyenv install --list

查看当前可用的Python版本:

pyenv versions

安装和卸载其他版本的python:

pyenv install 3.4.0

pyenv rehash

pyenv uninstall 3.4.0

切换python版本,默认版本为system:

pyenv local 3.4.0 # 在当前目录改变python版本

pyenv local --unset # 取消改变

pyenv global 3.4.0 # 全局改变python版本

pyenv shell 3.4.0 # 改变当前shell的python版本

安装新版本的python或者其他二进制包后都需要运行:

pyenv rehash

否则不会生效

eg:在pyenv中使用virtualenv部署werkzueg的测试app:

# 将virtualenv安装在pypy中

pyenv local pypy-2.3.1

pyenv virtualenv venv_pypy

pyenv local venv_pypy

which python

pip install -Ur requirements.txt

pip install -U Gunicorn

pyenv rehash

which gunicorn

gunicorn -b :5000 -w 9 werkzeug.testapp:test_app

2. unp

unp是一个草鸡好用的解压工具,支持格式:

- ar AR Archives (*.a)

- bz2 Bz2 Compressed Files (*.bz2)

- tbz2 Bz2 Compressed Tarballs (*.tar.bz2)

- gz Gzip Compressed Files (*.gz)

- tgz Gzip Compressed Tarballs (*.tar.gz; *.tgz)

- tar Uncompressed Tarballs (*.tar)

- cab Windows Cabinet Archive (*.cab)

- xz XZ Compressed Files (*.xz)

- txz XZ Compressed Tarballs (*.tar.xz)

- zip Zip Archives (*.zip; *.egg; *.whl; *.jar)

在linux下解压文件只需

unp FILENAME

就能解压,无需再给一堆麻烦的参数。

安装unp:

sudo pip install unp

3. cheat

好的,很多命令的参数你都记不住,你会man一下是吧,但是是不是看起来效率很低,不具可读性呢?如果你这么觉得,那么cheat就是你需要的。

安装:

brew install cheat

或者

sudo pip install cheat

eg:

> cheat find

# To find files by case-insensitive extension (ex: .jpg, .JPG, .jpG):

find . -iname "*.jpg"

# To find directories:

find . -type d

# To find files:

find . -type f

# To find files by octal permission:

find . -type f -perm 777

# To find files with setuid bit set:

find . -xdev \( -perm -4000 \) -type f -print0 | xargs -0 ls -l

# To find files with extension '.txt' and remove them:

find ./path/ -name '*.txt' -exec rm '{}' \;

# To find files with extension '.txt' and look for a string into them:

find ./path/ -name '*.txt' | xargs grep 'string'

# To find files with size bigger than 5 Mb and sort them by size:

find . -size +5M -type f -print0 | xargs -0 ls -Ssh | sort -z

# To find files bigger thank 2 MB and list them:

find . -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'

# To find files modified more than 7 days ago and list file information

find . -type f -mtime +7d -ls

# To find symlinks owned by a user and list file information

find . -type l --user=username -ls

# To search for and delete empty directories

find . -type d -empty -exec rmdir {} \;

# To search for directories named build at a max depth of 2 directories

find . -maxdepth 2 -name build -type d

# To search all files who are not in .git directory

find . ! -iwholename '*.git*' -type f

# Find all files that have the same node (hard link) as MY_FILE_HERE

find . -type f -samefile MY_FILE_HERE 2>/dev/null

4. pipreqs

有没有觉得有时候pip freeze > requirements.txt里面有些乱七八糟的模块,可能是手贱安装的,也可能是安装了后来没有用到的,模块多了很难发现,那么要保持一个干净的requirements也可以很easy哦。

pipreqs是一个根据你的项目的import来生成requirements.txt的工具。

安装:

pip install pipreqs

用法:

$ pipreqs /home/project/location

Successfully saved requirements file in /home/project/location/requirements.txt

5. ptpython

如果你喜欢ipython,那你一定会爱上ptpython。ptpython是一个比ipython更加高级的Python REPL。

安装:

pip install ptpython

特性:

可以选用vi或emacs模式 (默认emacs,ptpython --vi使用vi模式)

语法高亮,语法检查

自动补全 (tab/shift-tab,如果是vi模式可以C-n/C-p)

多行编辑 (冒号后的代码会变成多行编辑,不会像ipython一样不好编辑)

安装:

pip install ptpython

如果你安装了ipython,输入一把ptipython让你飞起来。

6. thefuck

thefuck是一个改正你上一条错误命令的工具,当你输入了一个错误拼写的命令,这时直接输入fuck就可以运行你本来需要执行的正确命令。

eg:

我新建了一个分支,要push他,但是发现尼玛远程还没有,这么长的正确命令我懒得复制,但是我可以fuck。

> git push

fatal: The current branch test has no upstream branch.

To push the current branch and set the remote as upstream, use

git push --set-upstream origin test

> fuck

git push --set-upstream origin test

Total 0 (delta 0), reused 0 (delta 0)

To git@github.com:axiaoxin/tests.git

* [new branch] test -> test

Branch test set up to track remote branch test from origin.

安装:

pip install thefuck

然后在你的.bashrc中添加

alias fuck='eval $(thefuck $(fc -ln -1)); history -r'

# You can use whatever you want as an alias, like for Mondays:

alias FUCK='fuck'

zsh粉在.zshrc中添加

alias fuck='eval $(thefuck $(fc -ln -1 | tail -n 1)); fc -R'

7. SimpleTornadoServer

用python -m SimpleHTTPServer传文件很酸爽吧,但是SimpleHTTPServer 不支持「续传」,和 wget 组合在糟糕的网络下传递一个稍大的文件总是会中途出错又重头再来,再来再错永远也下载不完。

那么,试试python -m SimpleTornadoServer吧,从此生活变得更加美好,你会有更多时间陪伴你的女朋友。

安装:

pip install SimpleTornadoServer

8. pythonpy

好的,上面的server工具表示在命令行可以直接执行某个python模块,还比如json.tool什么的,那么,我要直接执行python代码可以吗?我记不住shell语法我要直接用python好吗?好的,pythonpy带你飞。

安装:

pip install pythonpy

用法:

$ py '3 * 1.5'

4.5

$ py 'math.exp(1)'

2.71828182846

$ py 'random.random()'

0.103173957713

$ py 'range(3)'

0

1

2

$ py '[range(3)]'

[0, 1, 2]

$ py 'range(3)' | py -x 'int(x)*7'

0

7

14

$ py 'range(3)' | py -x 'x + ".txt"'

0.txt

1.txt

2.txt

$ py 'range(8)' | py -x 'x if int(x)%2 == 0 else None'

0

2

4

6

$ py 'range(3)' | py -l 'l[::-1]'

2

1

0

$ py 'range(3)' | py -l 'sum(int(x) for x in l)'

3

$ py 'range(17)' | py -l 'len(l)'

17

$ cat /usr/share/dict/words | py -x 'x[0].lower()' | py -l 'collections.Counter(l).most_common(5)'

('s', 11327)

('c', 9521)

('p', 7659)

('b', 6068)

('m', 5922)

$ py 'datetime.datetime.now?'

Help on built-in function now:

now(...)

[tz] -> new datetime with tz's local day and time.

9. json2xls

json2xls是一个根据json生成excel表格的工具

安装:

pip install json2xls

用法:

根据json字符串生成excel

json2xls cmd_str_test.xls '{"a":"a", "b":"b"}'

json2xls cmd_str_test1.xls '[{"a":"a", "b":"b"},{"a":1, "b":2}]'

根据json文件生成excel

json2xls cmd_list_test.xls "`cat list_data.json`"

根据每行一个json的文件生成excel

json2xls cmd_line_test.xls line_data.json

根据返回json的url生成excel

json2xls cmd_get_test.xls http://httpbin.org/get

json2xls cmd_post_test.xls http://httpbin.org/post -m post -d '"hello json2xls"' -h "{'X-Token': 'bolobolomi'}"

顺便推荐一个叫做csvkit的命令行工具,命令行处理csv的大杀器,jq,命令行处理json的大杀器。

其他的还有类似yapf,isort,autopep8之类的也是值得推荐的,我多用于vim中,不在这里写了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值