1. How do I find the location of my site-packages directory?
1)
>>> import site; site.getsitepackages() ['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
(or just first item with site.getsitepackages()[0]
)
2)
python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
3)
$ python -m site --user-site
/home/xulin/.local/lib/python2.7/site-packages
4)This works for me. It will get you both dist-packages and site-packages folders. If the folder is not on Python's path, it won't be doing you much good anyway.
import sys; print [f for f in sys.path if f.endswith('packages')]
2. Where to download Python?
http://legacy.python.org/ftp//python/3.3.0/
3. How to user "re" ?
http://docs.python.org/2/library/re.html
4. issue install fabric failed
it needs to install python-devel
5. how to solve issue: Python.h: No such file or directory
$apt-get install python-dev
If yum doesn't work then use
$apt-get install python-dev
6. how to use fabric ?
from fabric.api import env
from fabric.api import cd
from fabric.api import run
from fabric.api import local
from fabric.api import get
from fabric.api import put
env.hosts = ['192.168.1.10', '192.168.1.11']
env.user = 'root'
env.password = "password"
def get_version():
local('cat /etc/issue')
run('cat /etc/issue')
with cd('/root/'):
put('/home/libaoyin/test.txt', 'test.txt', mode=0755)
get('hello_world.txt')
run('ls')
def get_host_name():
run('hostname')
fab -f fabric.py get_version
[192.168.1.10] Executing task 'get_version'
[localhost] local: cat /etc/issue
Ubuntu 12.04.2 LTS \n \l
[192.168.1.10] run: cat /etc/issue
[192.168.1.10] out: Ubuntu 10.10 \n \l
[192.168.1.10] out:
[192.168.1.10] out:
[192.168.1.10] put: /home/libaoyin/test.txt -> /root/test.txt
[192.168.1.10] download: /home/libaoyin/tmp/58.68.233.167/hello_world.txt /root/test.txt
[192.168.1.11] download: /home/libaoyin/tmp/58.68.233.173/hello_world.txt
可以看到,执行输出了env.hosts里的所有服务器列表,输出内容就是执行get_version函数的结果。local是执行本地命令,run是执行远程命令。其他还有:在 remote 執行需要 sudo 的指令
sudo()
在 remote 執行 change directory,通常會搭配 with 使用
cd()
在 localhost 執行 change directory
lcd()
在执行函数前,我们设置了env环境:
env.hosts是指要执行以下函数的服务器列表env.user是要登录的用户名
env.password是用户密码
fab是命令行工具
-f fabfile.py 是使用的fab文件get_version 是运行的函数