概述

Python应用程序通常会使用不在标准库内的软件包和模块。应用程序有时需要特定版本的库,因为应用程序可能需要修复特定的错误,或者可以使用库的过时版本的接口编写应用程序。

这意味着一个Python安装可能无法满足每个应用程序的要求。如果应用程序A需要特定模块的1.0版本但应用程序B需要2.0版本,则需求存在冲突,安装版本1.0或2.0将导致某一个应用程序无法运行。

这个问题的解决方案是创建一个 virtual environment,一个目录树,其中安装有特定Python版本,以及许多其他包。

然后,不同的应用可以使用不同的虚拟环要解决先前的冲突需求示例,应用程序A可以拥有自己的1.0版本安装虚拟环境,而应用程序B则具有2.0版本的另一个虚拟环境。如果应用程序B要求将库升级到3.0版本,也不会影响应用程序A的环境。

使用pip管理包

你可以使用一个名为 pip 的程序来安装、升级和移除软件包。

默认情况下 pip 将从 Python Package Index https://pypi.org 安装软件包。

windows 安装

执行下面的命令:

  • 安全下载
  [plaintext]
1
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

日志信息如下:

  [plaintext]
1
2
3
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1659k 100 1659k 0 0 184k 0 0:00:09 0:00:09 --:--:-- 190k
  • 执行
  [plaintext]
1
python get-pip.py

日志信息

  [plaintext]
1
2
3
4
5
6
7
8
9
10
Collecting pip Downloading https://files.pythonhosted.org/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df8e8583ea85385d51125dceff/pip-19.0.3-py2.py3-none-any.whl (1.4MB) 100% |████████████████████████████████| 1.4MB 257kB/s Collecting wheel Downloading https://files.pythonhosted.org/packages/96/ba/a4702cbb6a3a485239fbe9525443446203f00771af9ac000fa3ef2788201/wheel-0.33.1-py2.py3-none-any.whl Installing collected packages: pip, wheel Found existing installation: pip 19.0.3 Uninstalling pip-19.0.3: Successfully uninstalled pip-19.0.3 Successfully installed pip-19.0.3 wheel-0.33.1
  • 测试版本
  [plaintext]
1
2
> pip --version pip 19.0.3 from c:\users\${username}\appdata\local\programs\python\python37\lib\site-packages\pip (python 3.7)
  • windows 更新
  [plaintext]
1
python -m pip install -U pip

对比 Java

很多语言都会有一个包的管理工具,比如 js 的 npm,java 的 maven/gradle。

使用包管理工具可以为我们带来很大的方便。

pip 的使用

搜索

  [plaintext]
1
pip query ${XXX}

安装

  [plaintext]
1
pip install ${XXX}

更新

  [plaintext]
1
pip install --upgrade ${XXX}

删除

  [plaintext]
1
pip uninstall ${XXX}

信息展现

  [plaintext]
1
pip show ${XXX}
  [plaintext]
1
pip list ${XXX}

实战例子

安装一下 pytest 作为测试

安装

  [plaintext]
1
pip install -U pytest
  • 日志信息
  [plaintext]
1
2
3
4
5
6
7
8
Collecting pytest ... Requirement already satisfied, skipping upgrade: setuptools in c:\users\binbin.hou\appdata\local\programs\python\python37\lib\site-packages (from pytest) (40.8.0) Collecting colorama; sys_platform == "win32" (from pytest) Downloading https://files.pythonhosted.org/packages/4f/a6/728666f39bfff1719fc94c481890b2106837da9318031f71a8424b662e12/colorama-0.4.1-py2.py3-none-any.whl .... Installing collected packages: colorama, attrs, atomicwrites, six, pluggy, py, more-itertools, pytest Successfully installed atomicwrites-1.3.0 attrs-19.1.0 colorama-0.4.1 more-itertools-7.0.0 pluggy-0.9.0 py-1.8.0 pytest-4.4.0 six-1.12.0

版本信息

  [plaintext]
1
2
pytest --version This is pytest version 4.4.0, imported from c:\users\${username}\appdata\local\programs\python\python37\lib\site-packages\pytest.py

包含 data 文件

  • MANIFEST.in
  [plaintext]
1
2
include LICENSE.txt recursive-include openccpy/db *
  • setup.py
  [py]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import setuptools from io import open with open("README.md", "r", encoding='utf8') as fh: long_description = fh.read() setuptools.setup( # 别人搜索会用到的项目名称 name="openccpy", version="0.0.4.2", keywords = ["tool","opencc", "opencc-py", "opencc-python", "Chinese Convert", "中文繁简体转换"], author="houbb", author_email="houbinbin.echo@gmail.com", description="Open Chinese Convert is an opensource project for conversion between Traditional Chinese and Simplified Chinese for python.", long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/houbb/pycc", # 这个参数是导入目录下的所有__init__.py包 packages=setuptools.find_packages(), # 把文件打包到包里 package_data={'db': ['openccpy/db/*']}, include_package_data = True, # 这是一个数组,里边包含的是咱的pip项目引用到的第三方库,如果你的项目有用到第三方库,要在这里添上第三方库的包名,如果用的第三方版本不是最新版本,还要有版本号。 install_requires = [], classifiers=[ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ] )

参考资料

https://docs.python.org/zh-cn/3/tutorial/venv.html

  • pip

pip 使用文档

PIP 安装

python 配置查询页面