Linux中Python的pip命令使用

    Python安装完成之后,想要下载相关的包,例如:paramiko、numpy、pandas等Python中这些基础的包,这里以安装paramiko包为例,说明如何使用pip安装
    paramiko模块,基于SSH用于连接远程服务器并执行相关操作。

进入Python命令行模式,引入paramiko模块,不存在时会输出提示信息

[root@centos ~]# python 
Python 2.7.5 (default, Nov 20 2015, 02:00:19) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import paramiko
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named paramiko


如果没有配置pip的环境变量,可用which查出pip的安装位置

[root@centos bin]# which pip
/usr/bin/pip


查看所有的pip相关命令

[root@centos bin]# ls *pip*
lesspipe.sh  pip  pip2  pip2.7


运行pip命令,提示参数用法

[root@centos bin]# pip

Usage:   
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  config                      Manage local and global configuration.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
  -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL
                              logging levels).
  --log <path>                Path to a verbose appending log.
  --proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort).
  --trusted-host <hostname>   Mark this host as trusted, even though it does not have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --client-cert <path>        Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with
                              --no-index.
  --no-color                  Suppress colored output


  
  这里使用pip2.7,来下载安装paramiko模块

  [root@centos bin]# pip2.7 install paramiko
Collecting paramiko
  Downloading https://files.pythonhosted.org/packages/cf/ae/94e70d49044ccc234bfdba20114fa947d7ba6eb68a2e452d89b920e62227/paramiko-2.4.2-py2.py3-none-any.whl (193kB)
    100% |████████████████████████████████| 194kB 326kB/s 
Collecting pyasn1>=0.1.7 (from paramiko)
  Downloading https://files.pythonhosted.org/packages/d1/a1/7790cc85db38daa874f6a2e6308131b9953feb1367f2ae2d1123bb93a9f5/pyasn1-0.4.4-py2.py3-none-any.whl (72kB)
    100% |████████████████████████████████| 81kB 345kB/s 
Collecting bcrypt>=3.1.3 (from paramiko)
  Downloading https://files.pythonhosted.org/packages/94/1d/e0caa84ed34524ecf85a86f33ae7588da6b5877ee9e7c279113e29e29b1d/bcrypt-3.1.5-cp27-cp27mu-manylinux1_x86_64.whl (59kB)
    100% |████████████████████████████████| 61kB 420kB/s 
Collecting cryptography>=1.5 (from paramiko)
  Downloading https://files.pythonhosted.org/packages/7f/ba/383b51cc26e3141c689ce988814385c7659f5ba01c4b5f2de38233010b5f/cryptography-2.4.2-cp27-cp27mu-manylinux1_x86_64.whl (2.1MB)
    100% |████████████████████████████████| 2.1MB 247kB/s 
Collecting pynacl>=1.0.1 (from paramiko)
  Downloading https://files.pythonhosted.org/packages/b3/25/e605574f24948a8a53b497744e93f061eb1dbe7c44b6465fc1c172d591aa/PyNaCl-1.3.0-cp27-cp27mu-manylinux1_x86_64.whl (762kB)
    100% |████████████████████████████████| 768kB 182kB/s 
Requirement already satisfied: six>=1.4.1 in /usr/lib/python2.7/site-packages (from bcrypt>=3.1.3->paramiko) (1.9.0)
Collecting cffi>=1.1 (from bcrypt>=3.1.3->paramiko)
  Downloading https://files.pythonhosted.org/packages/14/dd/3e7a1e1280e7d767bd3fa15791759c91ec19058ebe31217fe66f3e9a8c49/cffi-1.11.5-cp27-cp27mu-manylinux1_x86_64.whl (407kB)
    100% |████████████████████████████████| 409kB 128kB/s 
Collecting idna>=2.1 (from cryptography>=1.5->paramiko)
  Downloading https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl (58kB)
    100% |████████████████████████████████| 61kB 149kB/s 
Requirement already satisfied: enum34; python_version < "3" in /usr/lib/python2.7/site-packages (from cryptography>=1.5->paramiko) (1.0.4)
Collecting ipaddress; python_version < "3" (from cryptography>=1.5->paramiko)
  Downloading https://files.pythonhosted.org/packages/fc/d0/7fc3a811e011d4b388be48a0e381db8d990042df54aa4ef4599a31d39853/ipaddress-1.0.22-py2.py3-none-any.whl
Collecting asn1crypto>=0.21.0 (from cryptography>=1.5->paramiko)
  Downloading https://files.pythonhosted.org/packages/ea/cd/35485615f45f30a510576f1a56d1e0a7ad7bd8ab5ed7cdc600ef7cd06222/asn1crypto-0.24.0-py2.py3-none-any.whl (101kB)
    100% |████████████████████████████████| 102kB 136kB/s 
Requirement already satisfied: pycparser in /usr/lib/python2.7/site-packages (from cffi>=1.1->bcrypt>=3.1.3->paramiko) (2.14)
Installing collected packages: pyasn1, cffi, bcrypt, idna, ipaddress, asn1crypto, cryptography, pynacl, paramiko
  Found existing installation: pyasn1 0.1.6
    Uninstalling pyasn1-0.1.6:
      Successfully uninstalled pyasn1-0.1.6
  Found existing installation: cffi 0.8.6
    Uninstalling cffi-0.8.6:
      Successfully uninstalled cffi-0.8.6
  Found existing installation: cryptography 0.8.2
    Uninstalling cryptography-0.8.2:
      Successfully uninstalled cryptography-0.8.2
Successfully installed asn1crypto-0.24.0 bcrypt-3.1.5 cffi-1.11.5 cryptography-2.4.2 idna-2.8 ipaddress-1.0.22 paramiko-2.4.2 pyasn1-0.4.4 pynacl-1.3.0


安装完成后,再次在Python命令中引入paramiko,没有报错说明安装成功

>>> import paramiko
>>> 


下面做一个例子,通过ssh登录到一台服务器,执行ls命令,验证paramiko可用
脚本名称 sshclient.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import paramiko

# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='centos', port=22, username='root', password='123456')

# 执行命令
stdin, stdout, stderr = ssh.exec_command('ls')
# 获取命令结果
result = stdout.read()
print result
# 关闭连接
ssh.close()


运行脚本 python sshclient.py,脚本运行成功,登录到了centos这个服务器上,取得当前目录中的目录和文件名称,运行结果如下

anaconda-ks.cfg
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos
 

猜你喜欢

转载自blog.csdn.net/zhigang0529/article/details/85097352