【Python】pip - 安装第三方包的神器

本文介绍了如何使用pip安装Python第三方库。首先,从官方网址下载并安装pip,然后通过命令行运行Python get-pip.py进行安装。在安装过程中可能会遇到环境变量配置问题,需要将pip的安装路径添加到系统路径中。接着,演示了安装numpy和matplotlib等库的过程,以及在Windows和Mac OS上使用pip的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Python,肯定希望使用大量成熟的第三方包,从而快速用到自己的项目。


还在一个个寻找,手动下载吗?


pip就是这样一个神器,自动搜索、下载和安装第三方包:


1,首先请安装pip自己,下载地址(也可以通过附件下载):

http://www.pip-installer.org/en/latest/installing.html#python-os-support

2,安装pip,在命令行运行Python get-pip.py

3, 增加环境变量:C:/Python27/Scripts(pip安装的路径)(windows经常出此问题,可以通过在cmd输入pip看是否如下回复)


Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.


C:\Documents and Settings\Administrator>python pip install beautifulsoup4
python: can't open file 'pip': [Errno 2] No such file or directory


C:\Documents and Settings\Administrator>pip
'pip' 不是内部或外部命令,也不是可运行的程序
或批处理文件。


C:\Documents and Settings\Administrator>path
PATH=C:\Python27\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Pro
gram Files\Anite\LogViewer;C:\Program Files\Common Files\Sony Shared\FeliCaLibra
ry;C:\Program Files\Common Files\Sony Shared\FeliCaNFCLibrary


C:\Documents and Settings\Administrator>python
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()


C:\Documents and Settings\Administrator>python "\\vmware-host\Shared Folders\桌
面\get-pip.py" install
Traceback (most recent call last):
  File "\\vmware-host\Shared Folders\桌面\get-pip.py", line 19154, in <module>
    main()
  File "\\vmware-host\Shared Folders\桌面\get-pip.py", line 194, in main
    bootstrap(tmpdir=tmpdir)
  File "\\vmware-host\Shared Folders\桌面\get-pip.py", line 82, in bootstrap
    import pip
  File "c:\docume~1\admini~1\locals~1\temp\tmpbzsjty\pip.zip\pip\__init__.py", l
ine 13, in <module>
  File "c:\docume~1\admini~1\locals~1\temp\tmpbzsjty\pip.zip\pip\utils\__init__.
py", line 27, in <module>
  File "c:\docume~1\admini~1\locals~1\temp\tmpbzsjty\pip.zip\pip\_vendor\pkg_res
ources\__init__.py", line 3130, in <module>
  File "c:\docume~1\admini~1\locals~1\temp\tmpbzsjty\pip.zip\pip\_vendor\pkg_res
ources\__init__.py", line 3116, in _call_aside
  File "c:\docume~1\admini~1\locals~1\temp\tmpbzsjty\pip.zip\pip\_vendor\pkg_res
ources\__init__.py", line 3143, in _initialize_master_working_set
  File "c:\docume~1\admini~1\locals~1\temp\tmpbzsjty\pip.zip\pip\_vendor\pkg_res
ources\__init__.py", line 633, in _build_master
  File "c:\docume~1\admini~1\locals~1\temp\tmpbzsjty\pip.zip\pip\_vendor\pkg_res
ources\__init__.py", line 626, in __init__
  File "c:\docume~1\admini~1\locals~1\temp\tmpbzsjty\pip.zip\pip\_vendor\pkg_res
ources\__init__.py", line 682, in add_entry
  File "c:\docume~1\admini~1\locals~1\temp\tmpbzsjty\pip.zip\pip\_vendor\pkg_res
ources\__init__.py", line 2110, in find_on_path
WindowsError: [Error 234] : '\\\\vmware-host\\shared folders\\\xd7\xc0\xc3\xe6/*
.*'


C:\Documents and Settings\Administrator>python C:\get-pip.py install
Collecting install
c:\docume~1\admini~1\locals~1\temp\tmpjuymjr\pip.zip\pip\_vendor\requests\packag
es\urllib3\util\ssl_.py:315: SNIMissingWarning: An HTTPS request has been made,
but the SNI (Subject Name Indication) extension to TLS is not available on this
platform. This may cause the server to present an incorrect TLS certificate, whi
ch can cause validation failures. For more information, see https://urllib3.read
thedocs.org/en/latest/security.html#snimissingwarning.
c:\docume~1\admini~1\locals~1\temp\tmpjuymjr\pip.zip\pip\_vendor\requests\packag
es\urllib3\util\ssl_.py:120: InsecurePlatformWarning: A true SSLContext object i
s not available. This prevents urllib3 from configuring SSL appropriately and ma
y cause certain SSL connections to fail. For more information, see https://urlli
b3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  Could not find a version that satisfies the requirement install (from versions
: )
No matching distribution found for install


C:\Documents and Settings\Administrator>pip
'pip' 不是内部或外部命令,也不是可运行的程序
或批处理文件。


C:\Documents and Settings\Administrator>python C:\get-pip.py
Collecting pip
c:\docume~1\admini~1\locals~1\temp\tmpvgujaq\pip.zip\pip\_vendor\requests\packag
es\urllib3\util\ssl_.py:315: SNIMissingWarning: An HTTPS request has been made,
but the SNI (Subject Name Indication) extension to TLS is not available on this
platform. This may cause the server to present an incorrect TLS certificate, whi
ch can cause validation failures. For more information, see https://urllib3.read
thedocs.org/en/latest/security.html#snimissingwarning.
c:\docume~1\admini~1\locals~1\temp\tmpvgujaq\pip.zip\pip\_vendor\requests\packag
es\urllib3\util\ssl_.py:120: InsecurePlatformWarning: A true SSLContext object i
s not available. This prevents urllib3 from configuring SSL appropriately and ma
y cause certain SSL connections to fail. For more information, see https://urlli
b3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  Downloading pip-8.1.1-py2.py3-none-any.whl (1.2MB)
    35% |███████████▏                    | 419kB 409kB/s eta 0:00:02
    35% |███████████▌                    | 430kB 409kB/s eta 0:00:02
    36% |███████████▊                    | 440kB 409kB/s eta 0:00:02
    37% |████████████                    | 450kB 297kB/s eta 0:00:03
    38% |████████████▎                   | 460kB 297kB/s eta 0:00:0
    39% |████████████▋                   | 471kB 344kB/s eta 0:00:0
    40% |████████████▉                   | 481kB 344kB/s eta 0:00:0
    41% |█████████████▏                  | 491kB 328kB/s eta 0:00:
    41% |█████████████▍                  | 501kB 595kB/s eta 0:00:
    42% |█████████████▊                  | 512kB 595kB/s eta 0:00:
    43% |██████████████                  | 522kB 595kB/s eta 0:00:
    44% |██████████████▎                 | 532kB 656kB/s eta 0:00
    45% |██████████████▌                 | 542kB 435kB/s eta 0:00
    46% |██████████████▊                 | 552kB 731kB/s eta 0:00
    47% |███████████████                 | 563kB 656kB/s eta 0:00
    47% |███████████████▎                | 573kB 595kB/s eta 0:0
    48% |███████████████▋                | 583kB 595kB/s eta 0:0
    49% |███████████████▉                | 593kB 595kB/s eta 0:0
    50% |████████████████▏               | 604kB 595kB/s eta 0:
    51% |████████████████▍               | 614kB 595kB/s eta 0:
    52% |████████████████▊               | 624kB 547kB/s eta 0:
    53% |█████████████████               | 634kB 595kB/s eta 0:
    53% |█████████████████▎              | 645kB 939kB/s eta 0
    54% |█████████████████▌              | 655kB 930kB/s eta 0
    55% |█████████████████▉              | 665kB 939kB/s eta 0
    56% |██████████████████              | 675kB 939kB/s eta 0
    57% |██████████████████▎             | 686kB 939kB/s eta
    58% |██████████████████▋             | 696kB 930kB/s eta
    58% |██████████████████▉             | 706kB 819kB/s eta
    59% |███████████████████▏            | 716kB 939kB/s eta
    60% |███████████████████▍            | 727kB 930kB/s eta
    61% |███████████████████▊            | 737kB 819kB/s eta
    62% |████████████████████            | 747kB 939kB/s eta
    63% |████████████████████▎           | 757kB 819kB/s et
    64% |████████████████████▌           | 768kB 930kB/s et
    64% |████████████████████▉           | 778kB 1.1MB/s et
    65% |█████████████████████           | 788kB 819kB/s et
    66% |█████████████████████▍          | 798kB 939kB/s e
    67% |█████████████████████▋          | 808kB 930kB/s e
    68% |█████████████████████▉          | 819kB 819kB/s e
    69% |██████████████████████▏         | 829kB 939kB/s
    70% |██████████████████████▍         | 839kB 930kB/s
    70% |██████████████████████▊         | 849kB 930kB/s
    71% |███████████████████████         | 860kB 939kB/s
    72% |███████████████████████▎        | 870kB 819kB/s
    73% |███████████████████████▌        | 880kB 819kB/s
    74% |███████████████████████▉        | 890kB 939kB/s
    75% |████████████████████████        | 901kB 819kB/s
    76% |████████████████████████▍       | 911kB 939kB/
    76% |████████████████████████▋       | 921kB 819kB/
    77% |█████████████████████████       | 931kB 819kB/
    78% |█████████████████████████▏      | 942kB 939kB
    79% |█████████████████████████▌      | 952kB 819kB
    80% |█████████████████████████▊      | 962kB 1.1MB
    81% |██████████████████████████      | 972kB 1.1MB
    82% |██████████████████████████▎     | 983kB 819k
    82% |██████████████████████████▌     | 993kB 930k
    83% |██████████████████████████▉     | 1.0MB 939k
    84% |███████████████████████████     | 1.0MB 939k
    85% |███████████████████████████▍    | 1.0MB 1.1
    86% |███████████████████████████▋    | 1.0MB 930
    87% |████████████████████████████    | 1.0MB 930
    88% |████████████████████████████▏   | 1.1MB 93
    88% |████████████████████████████▌   | 1.1MB 81
    89% |████████████████████████████▊   | 1.1MB 93
    90% |█████████████████████████████   | 1.1MB 93
    91% |█████████████████████████████▎  | 1.1MB 9
    92% |█████████████████████████████▌  | 1.1MB 9
    93% |█████████████████████████████▉  | 1.1MB 8
    94% |██████████████████████████████  | 1.1MB 9
    94% |██████████████████████████████▍ | 1.1MB
    95% |██████████████████████████████▋ | 1.1MB
    96% |███████████████████████████████ | 1.2MB
    97% |███████████████████████████████▏| 1.2MB
    98% |███████████████████████████████▌| 1.2MB
    99% |███████████████████████████████▊| 1.2MB
    100% |████████████████████████████████| 1.2M
B 435kB/s
Collecting setuptools
  Downloading setuptools-20.6.7-py2.py3-none-any.whl (508kB)
    36% |███████████▋                    | 184kB 386kB/s eta 0:00:01
    38% |████████████▎                   | 194kB 409kB/s eta 0:00:0
    40% |████████████▉                   | 204kB 437kB/s eta 0:00:0
    42% |█████████████▌                  | 215kB 409kB/s eta 0:00:
    44% |██████████████▏                 | 225kB 437kB/s eta 0:00
    46% |██████████████▉                 | 235kB 3.3MB/s eta 0:00
    48% |███████████████▌                | 245kB 3.3MB/s eta 0:0
    50% |████████████████                | 256kB 6.4MB/s eta 0:0
    52% |████████████████▊               | 266kB 6.4MB/s eta 0:
    54% |█████████████████▍              | 276kB 6.4MB/s eta 0
    56% |██████████████████              | 286kB 6.4MB/s eta 0
    58% |██████████████████▊             | 296kB 3.2MB/s eta
    60% |███████████████████▎            | 307kB 3.2MB/s eta
    62% |████████████████████            | 317kB 6.4MB/s eta
    64% |████████████████████▋           | 327kB 6.4MB/s et
    66% |█████████████████████▎          | 337kB 6.4MB/s e
    68% |██████████████████████          | 348kB 6.4MB/s e
    70% |██████████████████████▌         | 358kB 6.4MB/s
    72% |███████████████████████▏        | 368kB 3.3MB/s
    74% |███████████████████████▉        | 378kB 2.2MB/s
    76% |████████████████████████▌       | 389kB 1.6MB/
    78% |█████████████████████████▏      | 399kB 1.3MB
    80% |█████████████████████████▊      | 409kB 1.3MB
    82% |██████████████████████████▍     | 419kB 1.1M
    84% |███████████████████████████     | 430kB 656k
    86% |███████████████████████████▊    | 440kB 656
    88% |████████████████████████████▍   | 450kB 31
    90% |█████████████████████████████   | 460kB 29
    92% |█████████████████████████████▋  | 471kB 3
    94% |██████████████████████████████▎ | 481kB
    96% |███████████████████████████████ | 491kB
    98% |███████████████████████████████▋| 501kB
    100% |████████████████████████████████| 512k
B 211kB/s
Collecting wheel
  Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
    45% |██████████████▊                 | 30kB 1.9MB/s eta 0:00:
    61% |███████████████████▋            | 40kB 1.3MB/s eta
    76% |████████████████████████▌       | 51kB 1.1MB/s
    91% |█████████████████████████████▍  | 61kB 1.
    100% |████████████████████████████████| 71kB
 1.1MB/s
Installing collected packages: pip, setuptools, wheel
Successfully installed pip-8.1.1 setuptools-20.6.7 wheel-0.29.0
c:\docume~1\admini~1\locals~1\temp\tmpvgujaq\pip.zip\pip\_vendor\requests\packag
es\urllib3\util\ssl_.py:120: InsecurePlatformWarning: A true SSLContext object i
s not available. This prevents urllib3 from configuring SSL appropriately and ma
y cause certain SSL connections to fail. For more information, see https://urlli
b3.readthedocs.org/en/latest/security.html#insecureplatformwarning.


C:\Documents and Settings\Administrator>


3,在命令行输入你想要下载的包,比如bumpy,输入pip install numpy,开始happy吧


Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.


C:\Documents and Settings\Administrator>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.
  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.
  --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.
  --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.


C:\Documents and Settings\Administrator>pip install bs4
Collecting bs4
c:\python27\lib\site-packages\pip\_vendor\requests\packages\urllib3\util\ssl_.py
:315: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Na
me Indication) extension to TLS is not available on this platform. This may caus
e the server to present an incorrect TLS certificate, which can cause validation
 failures. For more information, see https://urllib3.readthedocs.org/en/latest/s
ecurity.html#snimissingwarning.
  SNIMissingWarning
c:\python27\lib\site-packages\pip\_vendor\requests\packages\urllib3\util\ssl_.py
:120: InsecurePlatformWarning: A true SSLContext object is not available. This p
revents urllib3 from configuring SSL appropriately and may cause certain SSL con
nections to fail. For more information, see https://urllib3.readthedocs.org/en/l
atest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Downloading bs4-0.0.1.tar.gz
Collecting beautifulsoup4 (from bs4)
  Downloading beautifulsoup4-4.4.1-py2-none-any.whl (81kB)
    50% |████████████████                | 40kB 201kB/s eta 0:00
    62% |████████████████████▏           | 51kB 233kB/s eta
    75% |████████████████████████▏       | 61kB 280kB/s
    88% |████████████████████████████▏   | 71kB 306
    100% |████████████████████████████████| 81kB
 309kB/s
Building wheels for collected packages: bs4
  Running setup.py bdist_wheel for bs4 ... done
  Stored in directory: C:\Documents and Settings\Administrator\Local Settings\Ap
plication Data\pip\Cache\wheels\b1\f2\eb\f3818069481f80504f404cc6fed41095a814205
9472c226590
Successfully built bs4
Installing collected packages: beautifulsoup4, bs4
Successfully installed beautifulsoup4-4.4.1 bs4-0.0.1


Mac OS:

C:\Documents and Settings\Administrator>

bogon:~ myRMBP$ pip install virtualenv
Collecting virtualenv
  Downloading virtualenv-13.1.2-py2.py3-none-any.whl (1.7MB)
    100% |████████████████████████████████| 1.7MB 66kB/s 
Installing collected packages: virtualenv
Successfully installed virtualenv-13.1.2
bogon:~ myRMBP$ pip matplot
ERROR: unknown command "matplot"
bogon:~ myRMBP$ pip insatll matplotlib
ERROR: unknown command "insatll" - maybe you meant "install"
bogon:~ myRMBP$ pip install numpy
Collecting numpy
  Downloading numpy-1.10.1-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.7MB)
    100% |████████████████████████████████| 3.7MB 121kB/s 
Installing collected packages: numpy
Successfully installed numpy-1.10.1
bogon:~ myRMBP$ pip install matplotlib
Collecting matplotlib
  Downloading matplotlib-1.4.3-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (47.6MB)
    48% |███████████████▋                | 23.2MB 1.0MB/s eta 0:00:24


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值