Linux:安装pip客户端以安装Python软件包

时间:2020-01-09 10:37:58  来源:igfitidea点击:

我想使用pip command安装python软件包,这是easy_install command的替代品。
如何在RHEL/CentOS/Debian/Ubuntu Linux服务器系统下安装pip 命令?

pip命令是一个用于安装和管理Python软件包的工具,例如Python软件包索引中的软件包。它替代了easy_install。 pip对于网站开发以及管理基于云计算的资源(由Openstack,Rackspace,AWS,Google和其他云计算服务提供商创建)的系统管理员非常有用。 pip命令可以通过两种不同的方法安装:

  • 方法1:为具有root特权的所有用户设置pip。建议您使用软件包管理器来安装pip。
  • 方法2:无需root特权即可为单个用户点子。建议您使用virtualenv脚本,该脚本在$HOME目录中创建隔离的Python环境。

警告!只能使用以下任何一种方法来安装pip。
请勿同时使用两种方法安装pip。

方法1:为所有用户安装pip

默认情况下未安装pip命令。
您需要根据发行版进行安装。

RHEL/CentOS/Fedora Linux安装

首先,为RHEL/CentOS Linux打开EPEL仓库。
接下来,在RHEL/CentOS/Fedora Linux下执行以下yum命令以安装python-pip软件包:

# yum -y install python-pip

输出示例:

 
Total download size: 513 k
Installed size: 764 k
Is this ok [y/N]: y
Downloading Packages:
(1/2): python-pip-0.8-1.el6.noarch.rpm                   | 178 kB     00:01     
(2/2): python-setuptools-0.6.10-3.el6.noarch.rpm         | 336 kB     00:00     
-------------------------------------------------------------------------------
Total                                            63 kB/s | 513 kB     00:08     
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
Importing GPG key 0x0608B895:
Userid : EPEL (6) <[email protected]>
Package: epel-release-6-7.noarch (installed)
From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
  Installing : python-setuptools-0.6.10-3.el6.noarch                        1/2 
  Installing : python-pip-0.8-1.el6.noarch                                  2/2 
Installed products updated.
  Verifying  : python-setuptools-0.6.10-3.el6.noarch                        1/2 
  Verifying  : python-pip-0.8-1.el6.noarch                                  2/2 
 
Installed:
  python-pip.noarch 0:0.8-1.el6                                                 
 
Dependency Installed:
  python-setuptools.noarch 0:0.6.10-3.el6                                       
 
Complete!

请注意,该命令的名称是RHEL和friends下的pyton-pip
我建议您将以下别名添加到~/.bashrc文件中,输入:

$ echo 'alias pip="/usr/bin/pip-python"' >> $HOME/.bashrc
$ . $HOME/.bashrc

Debian/Ubuntu Linux安装

执行以下apt-get命令进行安装

# apt-get install python-pip

或者

$ sudo apt-get install python-pip

输出示例:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  python-pip
0 upgraded, 1 newly installed, 0 to remove and 14 not upgraded.
Need to get 67.8 kB of archives.
After this operation, 332 kB of additional disk space will be used.
Get:1 http://debian.osuosl.org/debian/ squeeze/main python-pip all 0.7.2-1 [67.8 kB]
Fetched 67.8 kB in 1s (40.4 kB/s)                     
Selecting previously deselected package python-pip.
(Reading database ... 274928 files and directories currently installed.)
Unpacking python-pip (from .../python-pip_0.7.2-1_all.deb) ...
Processing triggers for man-db ...
Setting up python-pip (0.7.2-1) ...
Processing triggers for python-support ...

方法2:为单个用户安装pip

此方法不需要root用户访问权限或修改系统Python安装。
执行以下命令:

curl -O https://raw.github.com/pypa/virtualenv/master/virtualenv.py

输出示例:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  107k  100  107k    0     0  35120      0  0:00:03  0:00:03 --:--:-- 49618

创建自己的虚拟环境py_virtual:

python virtualenv.py py_virtual

输出示例:

New python executable in py_virtual/bin/python
Installing setuptools............................done.
Installing pip.....................done.

激活新的虚拟环境,运行:

. py_virtual/bin/activate

示例输出(注释提示已更改):

(py_virtual)Hyman@wks01:~$

如何使用pip命令?

要安装新的python软件包类型:

pip install packageName

要卸载通过pip安装的python软件包,请执行以下操作:

pip uninstall packageName

要搜索python包类型:

pip search packageName

要查看所有命令的列表,请输入:

pip help

输出示例

Usage: pip COMMAND [OPTIONS]
--version                    show program's version number and exit
-h, --help                   Show help
-v, --verbose                Give more output
-q, --quiet                  Give less output
--log              Log file where a complete (maximum verbosity) record will be kept
--proxy               Specify a proxy in the form user:[email protected]:port. Note that the user:password@ is optional and required only if you
                              are behind an authenticated proxy. If you provide [email protected]:port then you will be prompted for a password.
--timeout           Set the socket timeout (default 15 seconds)
--exists-action 
                              Default action when a path already exists. Use this option more than one time to specify another action if a certain option is
                              not available. Choices: (s)witch, (i)gnore, (w)ipe, (b)ackup

Commands available:
  bundle: Create pybundles (archives containing multiple packages)
  freeze: Output all currently installed packages (exact versions) to stdout
  help: Show available commands
  install: Install packages
  search: Search PyPI
  uninstall: Uninstall packages
  unzip: Unzip individual packages
  zip: Zip individual packages

例子

搜索所有Openstack Cloud相关工具:

pip search openstack

输出示例:

ftp-cloudfs               - FTP interface to Rackspace Cloud Files and OpenStack Swift
vaporize                  - A clean and consistent library for the Rackspace Cloud / OpenStack
openstack.nose_plugin     - openstack run_tests.py style output for nosetests
nova-adminclient          - client for administering OpenStack Nova
python-glanceclient       - Client library for OpenStack Image API
python-novaclient         - Client library for OpenStack Nova API.
python-quantumclient      - CLI and python client library for OpenStack Quantum
python-melangeclient      - Client library for OpenStack Melange API.
txAWS                     - Async library for EC2, OpenStack, and Eucalyptus
python-swiftclient        - Client Library for OpenStack Object Storage API
python-keystoneclient     - Client library for OpenStack Keystone API
django_openstack_auth     - A Django authentication backend for use with the OpenStack Keystone Identity backend.
python-cinderclient       - Client library for OpenStack Cinder API.
rackspace-auth-openstack  - Rackspace Auth Plugin for OpenStack Clients.
openstackocci             - OCCI interface for Openstack.
django-openstack          - A Django interface for OpenStack.
futuregrid_passwdstack    - Password Stack is a simple tool that allows normal users to reset their own password in the OpenStack Dashboard
cloudenvy                 - Fast provisioning on openstack clouds.
reddwarf                  - PaaS services for Openstack
python-openstackclient    - OpenStack command-line client
reviewday                 - Report generator for OpenStack code reviews.
horizon                   - The OpenStack Dashboard.
hpcloud-auth-openstack    - HP Cloud Auth Plugin for OpenStack Clients.

要安装称为Horizon的OpenStack仪表板软件包,请输入:

pip install horizon