python_tools

切换python源

old way

pip 或者easy_install安装的时候会用国外的源,这个
东西不时会被墙掉,因此换豆瓣的源

linux下,修改~/.pip/pip.conf,如果没这文件则创建。
windows下,修改%HOMEPATH%\pip\pip.ini。
内容为:
[global]
index-url = http://pypi.douban.com/simple

[install]
trusted-host=pypi.douban.com

new

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple

virtualenv

http://www.virtualenv.org
类似虚拟机的一种环境,控制你python安装的好东西

保持你python的版本干净,例如一次开发,项目一django
使用1.5.0版本,项目二版本是用1.6,你同时负责这两个
项目的时候会非常痛苦,传统的方式可以说是无解的(莫非
装了卸卸了装?) 而用virtualenv可以完美解决这个问题,
虚拟环境1用1.5.0,虚拟环境二使用1.6,需要用那个环境
直接切过去就ok~

安装
sudo pip install virtualenv

新建总的虚拟环境文件夹
mkdir ~/python_env
sudo ln -s ~/python_env /opt

cd到你python虚拟环境的文件夹下
cd /opt/python_env

virtualenv ENV # 新建名字叫ENV的虚拟环境
virtualenv django1.6.1  # 新建名字叫django1.6.1的虚拟环境

例如我想用django这个环境
source django1.6.1/bin/activate

结束使用
deactivate

参考自
http://docs.python-guide.org/en/latest/dev/virtualenvs/

virtualenvwrapper

sudo pip install virtualenvwrapper

vim ~/.zshrc

即使之前 ~/venv下建立了对应的virtualenv也没关系,执行下面的命令会补全部分脚本

mkvirtualenv your_venv_name workon your_venv_name

pip安装

PIL 安装

pip 在zsh下tab补全

pip completion --zsh >> ~/.zprofile source ~/.zprofile

用交互模式执行脚本

python -i XXX.py ipython -i XXX.py 或者开启ipython后 %run 脚本路径

pylint代码评估

ipython

  • Inspect variables and functions

IPython:

  1. ?, look up the docs i.e.

  2. ??, look up the source code i.e.

  • Reuse variables

Type _iline# to reuse the input of that variable.

fabric(ssh工具)

突然发现了一篇好博客arrow-up-right

fabric命令需要在一个module里面,因此在文件夹里需要有__init__.py

fab 命令默认找此模块下的fabfile.py文件,如果没有这个文件需要使用 -f 指明脚本路径

fabfile.py

可以直接fab hello,执行fabfile.py里面的hello方法 如果没有fabfile.py而是有一个hello.py 则fab hello -f hello.py

如果有多个参数得话用,隔开,可以直接指明参数,例如下面的代码

可以 fab hello:name=1,say=2 fab hello:1,2 fab hello:1,say=2 # 与python的用法一样,kwarg要在arg后面

demo

supervisor

supervisor中文博客arrow-up-right unix domian socketarrow-up-right

supervisor配置文件,文档arrow-up-right

###demo(用了python的SimpleHTTPServer)

####文件结构

####supervisord.conf

####用法

在这个目录下运行supervisord,会发现多出几个文件 运行supervisorctl,进入交互模式,输入help可以看到能用的指令

###另一个长一点的配置文件

蛋疼的lxml安装问题

碰到过不止一次了

sudo apt-get install libxslt1-dev libxslt1.1 libxml2-dev libxml2 libssl-dev

pip install lxml

watchdog监管文件变化

watchdogarrow-up-right

edx-platform/scripts/run_watch_data.py 中的例子

用graphite diamond做监控

文章

开局先贴两个文章,值得一读

很赞的blogarrow-up-right

另一篇介绍graphite的文章arrow-up-right

恩怨

无论是什么系统,只要上线,就需要运维,这时候很想看一些监控的图表,graphite就很方便的实现了这个需求。

而graphite采用metrics的方式,又有很多其他的tool为他做支持,所监控的不仅仅是机器的一些东西,你可以监控你爬虫的指标, log的INFO,ERROR频次,nginx网站的访问数量等等,基本是你需要监控什么,很容易的就可以做到。

我从2014年初就在自己的TODOList添加了要玩graphite, 陆续玩了3、4次都失败了,原因都是安装里面某些步骤失败, 这两天终于搞成功了,写个博客记录一下。

graphite-webarrow-up-right 大部分的安装方式比较简单,都是用pip就可以安装,但是装完后有个坑, 文档arrow-up-right中说使用pip install graphite-web,但是pip中的graphite-web太老了, 导致有个cairo,库在ubuntu下打死也装不上,在新的源码中此bug已经修复。我已经提了issue 1004arrow-up-right

因为用的graphite-index,直接拿了他的几张图来看最终效果

1
2
3

安装

我用的是ubuntu, 写在最上面, 并且我假设你了解基本的python语法,用过pip, virtualenv, 没用过也没问题。

文档需要翻墙,因此贴出主要的安装步骤. 最好安装到python的virtualenv中,具体virtualenv的使用可以参考这里 首先,查看graphite-web的requirements.txtarrow-up-right,发现需要装一些系统的库, sudo apt-get install libcairo2-dev

这里我先贴下最终整个系统搭起来后的各个python库版本, 其中logster是一个做日志监控的东西,先git clone的本机,然后pip install -e logster项目地址即可

graphite配置与启动

根据文档的步骤安装完成后,你会发现/opt/graphite下多了一堆东西,将/opt/graphite/conf下的*.example,拷贝到去掉example即可

graphite有个服务在2003,2004接口上,你的metrics需要扔到2003上,具体请看文档,现在不用在意这些细节。

metrics就是类似这样的字符串 前缀.前缀.前缀....... blabala, graphite就是根据这种东西画图的,具体请看文档,不用在意这些细节, 因为其他的工具都有封装。

*. 启动carbon, metrics会扔到carbon这个小屋里面

*. 制造一些metrics, 更改host,或者server, 这里只是做测试,之后会用diamond来采集metrics

*. 配置并修改graphite-web的几行代码,启动这个django项目

4. 浏览器打开http://127.0.0.1:12222, http://127.0.0.1:12222/dashboard这两个页面玩一下,你会看到左侧tree那边有一些数据 这些数据存在/opt/graphite/storage/whisper

使用diamond收集metrics

给graphite填数据的方式太多了,这里使用diamond,因为豆瓣有一层graphite+diamond的皮, 下面会说

安装

配置并启动

给graphite换层皮, graphite-index

graphite的界面实在是不敢恭维,因此很多人为它写UI,这里选择豆瓣的graphite-indexarrow-up-right 选择它是因为配置简单

下载

配置

更新metrics

使用logster做日志监控

日志监控还是需要的,出了nginx的访问日志之外,对于application的异常等等可能也需要监控,这时候使用logsterarrow-up-right,就非常方便了,因为他内置了像graphite发metrics的方法,so easy, 这里给了一个我fork的地址,因为我是一个pythoner,logster默认 的parser有apache等等,但是没有python的,我写了一个,提了一个patch.

安装:

用法: logster --output=graphite --graphite-host=graphite的ip已经端口 你的parser 日志绝对路径 logster --output=graphite --graphite-host=127.0.0.1:2003 PythonLogster /var/log/adx/adxsterr.log

如果你需要自己定制parser,参照logster/logster/parsers下的东西写一个就好。

因为logster自带向graphite发metrics,无须向diamond集成(写Collector),只要起一个定时任务即可。

Finally

当然,如果你熟悉django,可以把graphite, graphite-index人给gunicorn和supervisor,这不是重点,需要的可以参考我github上的demoarrow-up-right.

至于定制你的diamond Collector,监控你想监控的东西,请自己翻阅文档 (继承Collector,重写collect方法),将写好的Collector放在collectors_path下.

为项目添加LICENSE

安装pycurl

生成ignore文件

joearrow-up-right

buildout

buildoutarrow-up-right 另一种管理python环境的方式(virtualenv), 有编译的一些操作,可以生成命令 文章arrow-up-right

文档arrow-up-right为例

执行完bin/buildout后会有下面的目录

buildout.cfg, 可以看到定义的parts=,可以定义下面的配置段儿

全新项目

pyenv 管理多个版本的python

安装3.6.10

cut video

Last updated