歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> CentOS中Python pip包管理工具的安裝和使用

CentOS中Python pip包管理工具的安裝和使用

日期:2017/2/28 13:47:39   编辑:Linux教程

Python有很多第三方的模塊或包,要管理這些包,有很多方法。python的pip包管理工具,類似與CentOS中yum,使用該工具安裝Python包非常方便。

從Python內部來說,pip 是一個安裝和管理 Python 包的工具 , 是 easy_install 的一個替換品。

distribute是setuptools的取代(Setuptools包後期不再維護了),pip是easy_install的取代。

pip的安裝前需要setuptools 或者 distribute,如果你使用的是Python3.x那麼就只能使用distribute因為Python3.x不支持setuptools。

我的CentOS環境是Python 2.6,所以在之前已經按照好了setuptools,安裝下面的步驟安裝pip包管理工具。

1.pip工具的下載安裝

pip下載命令為:

# cd /usr/local/src --路徑可以根據情況修改

# wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate

pip 安裝命令為:

# tar -xzvf pip-1.5.4.tar.gz

# cd pip-1.5.4
# python setup.py install

2.pip工具使用方法

pip安裝一個包

# pip install SomePackage
[...]
Successfully installed SomePackage

pip列出已經安裝的包
# pip list

pip查看已經安裝的包
# pip show --files SomePackage
Name: SomePackage
Version: 1.0
Location: /my/env/lib/pythonx.x/site-packages
Files:
../somepackage/__init__.py
[...]

pip檢查哪些包需要更新
# pip list --outdated
SomePackage (Current: 1.0 Latest: 2.0)


pip升級包
# pip install --upgrade SomePackage
[...]
Found existing installation: SomePackage 1.0
Uninstalling SomePackage:
Successfully uninstalled SomePackage
Running setup.py install for SomePackage
Successfully installed SomePackage

pip卸載包
$ pip uninstall SomePackage
Uninstalling SomePackage:
/my/env/lib/pythonx.x/site-packages/somepackage
Proceed (y/n)? y
Successfully uninstalled SomePackage

3.pip命令參數解釋

# pip --help


Usage:
pip <command> [options]

Commands:
install 安裝包.
uninstall 卸載包.
freeze 按著一定格式輸出已安裝包列表
list 列出已安裝包.
show 顯示包詳細信息.
search 搜索包,類似yum裡的search.
wheel Build wheels from your requirements.
zip 不推薦. Zip individual packages.
unzip 不推薦. Unzip individual packages.
bundle 不推薦. Create pybundles.
help 當前幫助.

General Options:
-h, --help 顯示幫助.
-v, --verbose 更多的輸出,最多可以使用3次
-V, --version 現實版本信息然後退出.
-q, --quiet 最少的輸出.
--log-file <path> 覆蓋的方式記錄verbose錯誤日志,默認文件:/root/.pip/pip.log
--log <path> 不覆蓋記錄verbose輸出的日志.
--proxy <proxy> Specify a proxy in the form [user:passwd@]proxy.server:port.
--timeout <sec> 連接超時時間 (默認15秒).
--exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup.
--cert <path> 證書.

4.pip管理包實際執行過程:

#使用pip 安裝Python操作Excel的模塊 XlsxWriter

# pip install XlsxWriter
Downloading/unpacking XlsxWriter
Downloading XlsxWriter-0.7.3-py2.py3-none-any.whl (132kB): 132kB downloaded
Installing collected packages: XlsxWriter
Successfully installed XlsxWriter
Cleaning up...
[root@centos01 pip-1.5.4]#
[root@centos01 pip-1.5.4]# pip list
Beaker (1.3.1)
cas (0.15)
decorator (3.0.1)
distribute (0.6.10)
ethtool (0.6)
firstboot (1.110)
FormEncode (1.3.0)
freeipa (2.0.0.alpha.0)
iniparse (0.3.1)
iotop (0.3.2)
ipapython (3.0.0)
iwlib (1.0)
kerberos (1.0)
lxml (2.2.3)
Mako (0.3.4)
MarkupSafe (0.9.2)
matplotlib (0.99.1.1)
netaddr (0.7.5)
nose (0.10.4)
numpy (1.4.1)
ordereddict (1.2)
paramiko (1.7.5)
pip (1.5.4)
psutil (2.0.0)
pycrypto (2.0.1)
pycurl (7.19.0)
PyDispatcher (2.0.5)
pygpgme (0.1)
pyOpenSSL (0.10)
python-dateutil (1.4.1)
python-default-encoding (0.1)
python-ldap (2.3.10)
python-meh (0.11)
python-nss (0.13)
pytz (2010h)
pyxdg (0.18)
scdate (1.9.60)
sckdump (2.0.5)
scservices (0.99.45)
scservices.dbus (0.99.45)
setuptools (18.2)
slip (0.2.20)
slip.dbus (0.2.20)
slip.gtk (0.2.20)
Warning: cannot find svn location for SQLObject==3.0.0a1dev-20150327
SQLObject (3.0.0a1dev-20150327)
SSSDConfig (1.9.2)
urlgrabber (3.9.1)
XlsxWriter (0.7.3)
yum-metadata-parser (1.1.2)
#

#使用pip 顯示Python操作Excel的模塊 XlsxWriter
# pip show --files XlsxWriter
---
Name: XlsxWriter
Version: 0.7.3
Location: /usr/lib/python2.6/site-packages
Requires:
Files:
Cannot locate installed-files.txt
#

#使用pip 顯示已經安裝的Python的包
# pip list
Beaker (1.3.1)
cas (0.15)
decorator (3.0.1)
distribute (0.6.10)
ethtool (0.6)
firstboot (1.110)
FormEncode (1.3.0)
freeipa (2.0.0.alpha.0)
iniparse (0.3.1)
iotop (0.3.2)
ipapython (3.0.0)
iwlib (1.0)
kerberos (1.0)
lxml (2.2.3)
Mako (0.3.4)
MarkupSafe (0.9.2)
matplotlib (0.99.1.1)
netaddr (0.7.5)
nose (0.10.4)
numpy (1.4.1)
ordereddict (1.2)
paramiko (1.7.5)
pip (1.5.4)
psutil (2.0.0)
pycrypto (2.0.1)
pycurl (7.19.0)
PyDispatcher (2.0.5)
pygpgme (0.1)
pyOpenSSL (0.10)
python-dateutil (1.4.1)
python-default-encoding (0.1)
python-ldap (2.3.10)
python-meh (0.11)
python-nss (0.13)
pytz (2010h)
pyxdg (0.18)
scdate (1.9.60)
sckdump (2.0.5)
scservices (0.99.45)
scservices.dbus (0.99.45)
setuptools (18.2)
slip (0.2.20)
slip.dbus (0.2.20)
slip.gtk (0.2.20)
Warning: cannot find svn location for SQLObject==3.0.0a1dev-20150327
SQLObject (3.0.0a1dev-20150327)
SSSDConfig (1.9.2)
urlgrabber (3.9.1)
yum-metadata-parser (1.1.2)
[root@centos01 pip-1.5.4]

#使用pip 顯示需要更新的包
# pip list --outdated
Warning: cannot find svn location for SQLObject==3.0.0a1dev-20150327
psutil (Current: 2.0.0 Latest: 3.1.1)
pip (Current: 1.5.4 Latest: 7.1.2)
matplotlib (Current: 0.99.1.1 Latest: 1.4.3)
iwlib (Current: 1.0 Latest: 1.5)
pycrypto (Current: 2.0.1 Latest: 2.6.1)
pyOpenSSL (Current: 0.10 Latest: 0.15.1)
Could not find any downloads that satisfy the requirement python-default-encoding
Some externally hosted files were ignored (use --allow-external python-default-encoding to allow).
python-nss (Current: 0.13 Latest: 0.16.0)
MarkupSafe (Current: 0.9.2 Latest: 0.23)
lxml (Current: 2.2.3 Latest: 3.4.4)
pygpgme (Current: 0.1 Latest: 0.3)
pycurl (Current: 7.19.0 Latest: 7.19.5.1)
Could not find any downloads that satisfy the requirement ethtool
Some externally hosted files were ignored (use --allow-external ethtool to allow).
numpy (Current: 1.4.1 Latest: 1.9.2)
kerberos (Current: 1.0 Latest: 1.2.2)
Could not find any downloads that satisfy the requirement yum-metadata-parser
Some externally hosted files were ignored (use --allow-external yum-metadata-parser to allow).
python-ldap (Current: 2.3.10 Latest: 2.4.20)
Could not find any downloads that satisfy the requirement sckdump
Some externally hosted files were ignored (use --allow-external sckdump to allow).
Could not find any downloads that satisfy the requirement ipapython
Some externally hosted files were ignored (use --allow-external ipapython to allow).
Could not find any downloads that satisfy the requirement slip.gtk
Some externally hosted files were ignored (use --allow-external slip.gtk to allow).
Could not find any downloads that satisfy the requirement firstboot
Some externally hosted files were ignored (use --allow-external firstboot to allow).
Could not find any downloads that satisfy the requirement scdate
Some externally hosted files were ignored (use --allow-external scdate to allow).
Mako (Current: 0.3.4 Latest: 1.0.2)
Beaker (Current: 1.3.1 Latest: 1.7.0)
cas (Current: 0.15 Latest: 1.22)
distribute (Current: 0.6.10 Latest: 0.7.3)
Could not find any downloads that satisfy the requirement scservices.dbus
Some externally hosted files were ignored (use --allow-external scservices.dbus to allow).
python-dateutil (Current: 1.4.1 Latest: 2.4.2)
Could not find any downloads that satisfy the requirement sssdconfig
Some externally hosted files were ignored (use --allow-external sssdconfig to allow).
decorator (Current: 3.0.1 Latest: 4.0.2)
nose (Current: 0.10.4 Latest: 1.3.7)
Could not find any downloads that satisfy the requirement freeipa
Some externally hosted files were ignored (use --allow-external freeipa to allow).
iniparse (Current: 0.3.1 Latest: 0.4)
Could not find any downloads that satisfy the requirement slip.dbus
Some externally hosted files were ignored (use --allow-external slip.dbus to allow).
netaddr (Current: 0.7.5 Latest: 0.7.15)
paramiko (Current: 1.7.5 Latest: 1.15.2)
Could not find any downloads that satisfy the requirement python-meh
Some externally hosted files were ignored (use --allow-external python-meh to allow).
pytz (Current: 2010h Latest: 2015.4)
Could not find any downloads that satisfy the requirement scservices
Some externally hosted files were ignored (use --allow-external scservices to allow).
Could not find any downloads that satisfy the requirement iotop
Some externally hosted files were ignored (use --allow-external iotop to allow).
pyxdg (Current: 0.18 Latest: 0.25)
#

#使用pip 升級psutil模塊
# pip install --upgrade psutil
Downloading/unpacking psutil from https://pypi.python.org/packages/source/p/psutil/psutil-3.1.1.tar.gz#md5=b34360627efb429cb18a4a3143b18c8c
Downloading psutil-3.1.1.tar.gz (247kB): 247kB downloaded
Running setup.py (path:/tmp/pip_build_root/psutil/setup.py) egg_info for package psutil
/tmp/pip_build_root/psutil/setup.py:12: DeprecationWarning: Parameters to load are deprecated. Call .resolve and .require separately.
import os
warning: no previously-included files matching '*' found under directory 'docs/_build'
warning: manifest_maker: MANIFEST.in, line 18: 'recursive-include' expects <dir> <pattern1> <pattern2> ...
Installing collected packages: psutil
Found existing installation: psutil 2.0.0
Uninstalling psutil:
Successfully uninstalled psutil
Running setup.py install for psutil
building 'psutil._psutil_linux' extension
gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DPSUTIL_VERSION=311 -I/usr/include/python2.6 -c psutil/_psutil_linux.c -o build/temp.linux-x86_64-2.6/psutil/_psutil_linux.o
psutil/_psutil_linux.c: In function ‘psutil_proc_cpu_affinity_set’:
psutil/_psutil_linux.c:420: warning: suggest explicit braces to avoid ambiguous ‘else’
gcc -pthread -shared build/temp.linux-x86_64-2.6/psutil/_psutil_linux.o -L/usr/lib64 -lpython2.6 -o build/lib.linux-x86_64-2.6/psutil/_psutil_linux.so
building 'psutil._psutil_posix' extension
gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include/python2.6 -c psutil/_psutil_posix.c -o build/temp.linux-x86_64-2.6/psutil/_psutil_posix.o
gcc -pthread -shared build/temp.linux-x86_64-2.6/psutil/_psutil_posix.o -L/usr/lib64 -lpython2.6 -o build/lib.linux-x86_64-2.6/psutil/_psutil_posix.so
warning: no previously-included files matching '*' found under directory 'docs/_build'
warning: manifest_maker: MANIFEST.in, line 18: 'recursive-include' expects <dir> <pattern1> <pattern2> ...
Successfully installed psutil
Cleaning up...


#使用pip 卸載Python操作Excel的模塊 XlsxWriter
[root@centos01 pip-1.5.4]# pip uninstall XlsxWriter
Uninstalling XlsxWriter:
/usr/bin/vba_extract.py
/usr/bin/vba_extract.pyc
/usr/lib/python2.6/site-packages/XlsxWriter-0.7.3.dist-info/DESCRIPTION.rst
/usr/lib/python2.6/site-packages/XlsxWriter-0.7.3.dist-info/METADATA
/usr/lib/python2.6/site-packages/XlsxWriter-0.7.3.dist-info/RECORD
/usr/lib/python2.6/site-packages/XlsxWriter-0.7.3.dist-info/WHEEL
/usr/lib/python2.6/site-packages/XlsxWriter-0.7.3.dist-info/metadata.json
/usr/lib/python2.6/site-packages/XlsxWriter-0.7.3.dist-info/top_level.txt
/usr/lib/python2.6/site-packages/xlsxwriter/__init__.py
/usr/lib/python2.6/site-packages/xlsxwriter/__init__.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/app.py
/usr/lib/python2.6/site-packages/xlsxwriter/app.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/chart.py
/usr/lib/python2.6/site-packages/xlsxwriter/chart.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/chart_area.py
/usr/lib/python2.6/site-packages/xlsxwriter/chart_area.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/chart_bar.py
/usr/lib/python2.6/site-packages/xlsxwriter/chart_bar.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/chart_column.py
/usr/lib/python2.6/site-packages/xlsxwriter/chart_column.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/chart_doughnut.py
/usr/lib/python2.6/site-packages/xlsxwriter/chart_doughnut.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/chart_line.py
/usr/lib/python2.6/site-packages/xlsxwriter/chart_line.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/chart_pie.py
/usr/lib/python2.6/site-packages/xlsxwriter/chart_pie.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/chart_radar.py
/usr/lib/python2.6/site-packages/xlsxwriter/chart_radar.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/chart_scatter.py
/usr/lib/python2.6/site-packages/xlsxwriter/chart_scatter.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/chart_stock.py
/usr/lib/python2.6/site-packages/xlsxwriter/chart_stock.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/chartsheet.py
/usr/lib/python2.6/site-packages/xlsxwriter/chartsheet.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/comments.py
/usr/lib/python2.6/site-packages/xlsxwriter/comments.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/compat_collections.py
/usr/lib/python2.6/site-packages/xlsxwriter/compat_collections.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/compatibility.py
/usr/lib/python2.6/site-packages/xlsxwriter/compatibility.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/contenttypes.py
/usr/lib/python2.6/site-packages/xlsxwriter/contenttypes.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/core.py
/usr/lib/python2.6/site-packages/xlsxwriter/core.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/drawing.py
/usr/lib/python2.6/site-packages/xlsxwriter/drawing.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/format.py
/usr/lib/python2.6/site-packages/xlsxwriter/format.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/packager.py
/usr/lib/python2.6/site-packages/xlsxwriter/packager.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/relationships.py
/usr/lib/python2.6/site-packages/xlsxwriter/relationships.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/shape.py
/usr/lib/python2.6/site-packages/xlsxwriter/shape.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/sharedstrings.py
/usr/lib/python2.6/site-packages/xlsxwriter/sharedstrings.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/styles.py
/usr/lib/python2.6/site-packages/xlsxwriter/styles.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/table.py
/usr/lib/python2.6/site-packages/xlsxwriter/table.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/theme.py
/usr/lib/python2.6/site-packages/xlsxwriter/theme.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/utility.py
/usr/lib/python2.6/site-packages/xlsxwriter/utility.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/vml.py
/usr/lib/python2.6/site-packages/xlsxwriter/vml.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/workbook.py
/usr/lib/python2.6/site-packages/xlsxwriter/workbook.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/worksheet.py
/usr/lib/python2.6/site-packages/xlsxwriter/worksheet.pyc
/usr/lib/python2.6/site-packages/xlsxwriter/xmlwriter.py
/usr/lib/python2.6/site-packages/xlsxwriter/xmlwriter.pyc
Proceed (y/n)? y
Successfully uninstalled XlsxWriter

Ubuntu 14.04安裝Python 3.3.5 http://www.linuxidc.com/Linux/2014-05/101481.htm

CentOS上源碼安裝Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Copyright © Linux教程網 All Rights Reserved