Python本身自带了一套工具distutils ,用于发布 Python 应用程序。但 distutils 没有提供定义其它依赖包的功能,setuptools 的真正优点并不在于实现distutils 所能实现的功能——尽管它的确增强了distutils的功能并简化了setup.py 脚本中的内容。setuptools 最大的优势是它在包管理能力方面的增强。它可以使用一种更加透明的方法来查找、下载并安装依赖包;并可以在一个包的多个版本中自由进行切换,这些版本都安装在同一个系统上;也可以声明对某个包的特定版本的需求;还可以只使用一个简单的命令就能更新到某个包的最新版本。
简单来说,Python setuptools可以看做是增强版的distutils,用来管理Python的各种包。 一般在CentOS系统安装完成后,Python编译器就已经会安装好了,对于setuptools工具的安装,可以用ea_setup.py脚本的方法,自动下载安装包并安装;也可以先下载完整的setuptools安装包,然后再单独安装。 一、以ez_setup.py脚本方式安装setuptools工具 使用ez_setup.py脚本的方法比较简单,但要注意服务器要能够联网下载软件包,安装命令为:
# cd /usr/local/src/
# wget --no-check-certificate 安装的过程为:
# wget --no-check-certificate
Resolving bootstrap.pypa.io... 103.245.222.175
Connecting to bootstrap.pypa.io|103.245.222.175|:443... connected.
WARNING: certificate common name “*.c.ssl.fastly.net” doesn’t match requested host name “bootstrap.pypa.io”.
HTTP request sent, awaiting response... 200 OK
Length: 11432 (11K) [text/x-python]
Saving to: “ez_setup.py” 100%[===================================================================>] 11,432      --.-K/s   in 0s      2015-08-27 10:43:49 (692 MB/s) - “ez_setup.py” saved [11432/11432] # python ez_setup.py --insecure
Downloading
Installed /usr/lib/python2.6/site-packages/setuptools-18.2-py2.6.egg
Processing dependencies for setuptools==18.2
Finished processing dependencies for setuptools==18.2
total 888
drwxr-xr-x.  3 root root   4096 Aug 27 11:11 .
drwxr-xr-x. 12 root root   4096 Apr 15 01:16 ..
-rw-r--r--   1 root root  11432 Aug 20 01:00 ez_setup.py
drwxr-xr-x   9 1000 1000   4096 Aug 27 11:18 psutil-2.0.0
-rw-r--r--   1 root root 207168 May 15 01:30 psutil-2.0.0.tar.gz
-rw-r--r--   1 root root 674659 Aug 27 10:45 setuptools-18.2.zip
# which easy_install
/usr/bin/easy_install
# easy_install --help Global options:
--verbose (-v)  run verbosely (default)
--quiet (-q)    run quietly (turns verbosity off)
--dry-run (-n)  don't actually do anything
--help (-h)     show detailed help message Options for 'easy_install' command:
--prefix                   installation prefix
--zip-ok (-z)              install package as a zipfile
--multi-version (-m)       make apps have to require() a version
--upgrade (-U)             force upgrade (searches PyPI for latest versions)
--install-dir (-d)         install package to DIR
--script-dir (-s)          install scripts to DIR
--exclude-scripts (-x)     Don't install scripts
--always-copy (-a)         Copy all needed packages to install dir
--index-url (-i)           base URL of Python Package Index
--find-links (-f)          additional URL(s) to search for packages
--build-directory (-b)     download/extract/build in DIR; keep the results
--optimize (-O)            also compile with optimization: -O1 for "python -
O", -O2 for "python -OO", and -O0 to disable
[default: -O0]
--record                   filename in which to record list of installed
files
--always-unzip (-Z)        don't install as a zipfile, no matter what
--site-dirs (-S)           list of directories where .pth files work
--editable (-e)            Install specified packages in editable form
--no-deps (-N)             don't install dependencies
--allow-hosts (-H)         pattern(s) that hostnames must match
--local-snapshots-ok (-l)  allow building eggs from local checkouts
--version                  print version information and exit
--no-find-links            Don't load find-links defined in packages being
installed
--user                     install in user site-package
'/root/.local/lib/python2.6/site-packages' usage: easy_install [options] requirement_or_url ...
or: easy_install --help 注:此操作会从easy-install.pth文件里把MySQL-python的相关信息抹去,剩下的egg文件,你可以手动删除。
上面这些具体细节可以参考easy install的官方网站:
例如,用第一种方法安装SQLObject模块:
# easy_install -f
Reading https://pypi.python.org/packages/2.6/S/SQLObject/SQLObject-3.0.0a1dev_20150327-py2.6.egg#md5=de05545626c8c4179ebb787fb2b0cf96
Processing SQLObject-3.0.0a1dev_20150327-py2.6.egg
creating /usr/lib/python2.6/site-packages/SQLObject-3.0.0a1dev_20150327-py2.6.egg
Extracting SQLObject-3.0.0a1dev_20150327-py2.6.egg to /usr/lib/python2.6/site-packages
Adding SQLObject 3.0.0a1.dev20150327 to easy-install.pth file
Installing sqlobject-admin script to /usr/bin
Installing sqlobject-convertOldURI script to /usr/bin Installed /usr/lib/python2.6/site-packages/SQLObject-3.0.0a1dev_20150327-py2.6.egg
Processing dependencies for SQLObject
Searching for PyDispatcher>=2.0.4
Reading Processing PyDispatcher-2.0.5.zip
Writing /tmp/easy_install-CKT2_x/PyDispatcher-2.0.5/setup.cfg
Running PyDispatcher-2.0.5/setup.py -q bdist_egg --dist-dir /tmp/easy_install-CKT2_x/PyDispatcher-2.0.5/egg-dist-tmp-wHQRh1
warning: no previously-included files matching '*.bat' found anywhere in distribution
warning: no previously-included files matching './CVS' found anywhere in distribution
warning: no previously-included files matching '.cvsignore' found anywhere in distribution
zip_safe flag not set; analyzing archive contents...
Moving PyDispatcher-2.0.5-py2.6.egg to /usr/lib/python2.6/site-packages
Adding PyDispatcher 2.0.5 to easy-install.pth file Installed /usr/lib/python2.6/site-packages/PyDispatcher-2.0.5-py2.6.egg
Searching for FormEncode>=1.1.1
Reading
Processing FormEncode-1.3.0.zip
Writing /tmp/easy_install-wGGheC/FormEncode-1.3.0/setup.cfg
Running FormEncode-1.3.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-wGGheC/FormEncode-1.3.0/egg-dist-tmp-msPx0T
warning: no files found matching '*.py' under directory '.'
warning: no files found matching '*.html' under directory 'docs'
warning: no files found matching '*.py' under directory 'formencode/i18n'
warning: no previously-included files matching '*.pyc' found under directory '.'
warning: no previously-included files matching '*.pyo' found under directory '.'
warning: no previously-included files matching '*~' found under directory '.'
no previously-included directories found matching 'docs/_build'
no previously-included directories found matching '**/.svn'
no previously-included directories found matching '.hg'
no previously-included directories found matching '.git'
creating /usr/lib/python2.6/site-packages/FormEncode-1.3.0-py2.6.egg
Extracting FormEncode-1.3.0-py2.6.egg to /usr/lib/python2.6/site-packages
Adding FormEncode 1.3.0 to easy-install.pth file Installed /usr/lib/python2.6/site-packages/FormEncode-1.3.0-py2.6.egg
Finished processing dependencies for SQLObject
这样 SQLObject模块就安装成功了。 三、以源码方式安装psutil模块
Python中的psutil模块能够获取系统运行进程和使用率,对于系统监控还是比较有意义的。
下面用easy_install以源码方式安装psutil模块:
# cd /usr/local/src
# wget
使用easy_install进行源码安装时,可能会有如下报错:
用安装python模块出现error: command 'gcc' failed with exit status 1 ,明明装了gcc的,怎么会不行呢,然后发觉是failed不是not found,这说明这个错误个gcc没多
大关系,应该是缺少某些功能模块,然后谷歌了一下,先后安装了python-devel,libffi-devel后还是不行,最后发觉要安装openssl-devel才行 可如下命令行安装: yum install gcc libffi-devel python-devel openssl-devel 过程如下:
# yum install gcc libffi-devel python-devel openssl-devel
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: mirrors.btte.net
* updates: mirrors.btte.net
Setting up Install Process
Package gcc-4.4.7-16.el6.x86_64 already installed and latest version
Package libffi-devel-3.0.5-3.2.el6.x86_64 already installed and latest version
Package openssl-devel-1.0.1e-42.el6.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package python-devel.x86_64 0:2.6.6-64.el6 will be installed
--> Processing Dependency: python-libs(x86-64) = 2.6.6-64.el6 for package: python-devel-2.6.6-64.el6.x86_64
--> Processing Dependency: python = 2.6.6-64.el6 for package: python-devel-2.6.6-64.el6.x86_64
--> Running transaction check
---> Package python.x86_64 0:2.6.6-51.el6 will be updated
---> Package python.x86_64 0:2.6.6-64.el6 will be an update
---> Package python-libs.x86_64 0:2.6.6-51.el6 will be updated
---> Package python-libs.x86_64 0:2.6.6-64.el6 will be an update
--> Finished Dependency Resolution Dependencies Resolved =============================================================================================================
Package                      Arch                   Version                      Repository            Size
=============================================================================================================
Installing:
python-devel                 x86_64                 2.6.6-64.el6                 base                 172 k
Updating for dependencies:
python                       x86_64                 2.6.6-64.el6                 base                  76 k
python-libs                  x86_64                 2.6.6-64.el6                 base                 5.3 M Transaction Summary
=============================================================================================================
Install       1 Package(s)
Upgrade       2 Package(s) Total download size: 5.6 M
Is this ok [y/N]: y
Downloading Packages:
(1/3): python-2.6.6-64.el6.x86_64.rpm                                                 |  76 kB     00:00
(2/3): python-devel-2.6.6-64.el6.x86_64.rpm                                           | 172 kB     00:00
(3/3): python-libs-2.6.6-64.el6.x86_64.rpm                                            | 5.3 MB     00:17
-------------------------------------------------------------------------------------------------------------
Total                                                                        317 kB/s | 5.6 MB     00:17
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Updating   : python-2.6.6-64.el6.x86_64                                                                1/5
Updating   : python-libs-2.6.6-64.el6.x86_64                                                           2/5
Installing : python-devel-2.6.6-64.el6.x86_64                                                          3/5
Cleanup    : python-2.6.6-51.el6.x86_64                                                                4/5
Cleanup    : python-libs-2.6.6-51.el6.x86_64                                                           5/5
Verifying  : python-devel-2.6.6-64.el6.x86_64                                                          1/5
Verifying  : python-libs-2.6.6-64.el6.x86_64                                                           2/5
Verifying  : python-2.6.6-64.el6.x86_64                                                                3/5
Verifying  : python-libs-2.6.6-51.el6.x86_64                                                           4/5
Verifying  : python-2.6.6-51.el6.x86_64                                                                5/5 Installed:
python-devel.x86_64 0:2.6.6-64.el6                                                                         Dependency Updated:
python.x86_64 0:2.6.6-64.el6                       python-libs.x86_64 0:2.6.6-64.el6                      Complete! 再次安装,就可以正常安装psutil模块了:
# easy_install .
Processing .
Writing /usr/local/src/psutil-2.0.0/setup.cfg
Running setup.py -q bdist_egg --dist-dir /usr/local/src/psutil-2.0.0/egg-dist-tmp-wshhD1
warning: no previously-included files matching '*' found under directory 'docs/_build'
psutil/_psutil_linux.c: In function ‘psutil_proc_cpu_affinity_set’:
psutil/_psutil_linux.c:327: warning: suggest explicit braces to avoid ambiguous ‘else’
zip_safe flag not set; analyzing archive contents...
Moving psutil-2.0.0-py2.6-linux-x86_64.egg to /usr/lib/python2.6/site-packages
Adding psutil 2.0.0 to easy-install.pth file Installed /usr/lib/python2.6/site-packages/psutil-2.0.0-py2.6-linux-x86_64.egg
Processing dependencies for psutil==2.0.0
Finished processing dependencies for psutil==2.0.0  psutil模块安装完毕后,可以在Python终端中调用各种命令来获取系统状态:
# python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
/usr/lib/python2.6/site-packages/psutil-2.0.0-py2.6-linux-x86_64.egg/_psutil_linux.py:3: UserWarning: Module _psutil_linux was already imported from
/usr/lib/python2.6/site-packages/psutil-2.0.0-py2.6-linux-x86_64.egg/_psutil_linux.pyc, but /usr/local/src/psutil-2.0.0 is being added to sys.path
>>> psutil.cpu_times()
scputimes(user=43.100000000000001, nice=0.0, system=36.119999999999997, idle=10653.4, iowait=285.0, irq=1.0600000000000001, softirq=3.1000000000000001,
steal=0.0, guest=0.0)
>>> psutil.cpu_times().user
43.130000000000003
>>> psutil.cpu_count()
>>> psutil.cpu_count(logical=False)
>>> mem = psutil.virtual_memory()
svmem(total=1036648448L, available=733806592L, percent=29.199999999999999, used=888340480L, free=148307968L, active=244699136, inactive=500060160,
buffers=47853568L, cached=537645056)
>>> mem.total
1036648448L
>>> mem.free
148307968L
>>> psutil.swap_memory()
sswap(total=2080366592L, used=8192L, free=2080358400L, percent=0.0, sin=0, sout=0)
>>> psutil.disk_partition()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'ModuleWrapper' object has no attribute 'disk_partition'
>>> psutil.disk_partitions()
[sdiskpart(device='/dev/mapper/vg_centos01-lv_root', mountpoint='/', fstype='ext4', opts='rw'), sdiskpart(device='/dev/sda1', mountpoint='/boot',
fstype='ext4', opts='rw')]
>>> psutil.disk_usage('/')
sdiskusage(total=18569568256, used=4755951616, free=12870320128, percent=25.600000000000001)
>>> psutil.disk_io_counters()
sdiskio(read_count=37622, write_count=230886, read_bytes=688139264, write_bytes=1625135104, read_time=664928, write_time=22065267)
>>> psutil.disk_io_counters(perdisk=True)
{'dm-1': sdiskio(read_count=322, write_count=0, read_bytes=1318912, write_bytes=0, read_time=653, write_time=0), 'sda2': sdiskio(read_count=14739,
write_count=32509, read_bytes=343094272, write_bytes=812621824, read_time=282044, write_time=1616651), 'dm-0': sdiskio(read_count=21964,
write_count=198394, read_bytes=341320704, write_bytes=812621824, read_time=382104, write_time=20448581), 'sda1': sdiskio(read_count=597, write_count=7,
read_bytes=2405376, write_bytes=14336, read_time=127, write_time=93)}
>>> psutil.net_io_counters()
snetio(bytes_sent=3186898, bytes_recv=119382022, packets_sent=43509, packets_recv=84308, errin=0, errout=0, dropin=0, dropout=0)
>>> psutil.net_io_counters(pernic=True)
{'lo': snetio(bytes_sent=960, bytes_recv=960, packets_sent=16, packets_recv=16, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio
(bytes_sent=3192258, bytes_recv=119388892, packets_sent=43535, packets_recv=84361, errin=0, errout=0, dropin=0, dropout=0)}
>>> psutil.users()
[suser(name='root', terminal='tty1', host=':0', started=1440643328.0), suser(name='root', terminal='pts/0', host='localhost', started=1440643328.0),
suser(name='root', terminal='pts/1', host='192.168.226.1', started=1440643328.0)]
>>> import psutil,datetime
>>> psutil.boot_time()
1440642945.0
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
'2015-08-27 10:35:45'
多年数据库运维和架构经验,对 MySQL、Oracle、PostgreSQL、Greenplum、MongoDB 等多种常见数据库有丰富的运维实践经验,擅长数据库架构设计、维护优化、数据流转、Shell 和 Python 开发;乐于技术交流,网名 yumushui 进行了大量的技术总结和思考分享。

注册时间: 2015-05-21

48435

广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员