支持多种python版本

页面状态

不完整

上次审阅时间

2014-12-24

FIXME

Useful projects/resources to reference:

- DONE six
- DONE python-future (http://python-future.org)
- tox
- DONE Travis and Shining Panda CI (Shining Panda no longer available)
- DONE Appveyor
- DONE Ned Batchelder's "What's in Which Python"
  - http://nedbatchelder.com/blog/201310/whats_in_which_python_3.html
    - http://nedbatchelder.com/blog/201109/whats_in_which_python.html
- Lennart Regebro's "Porting to Python 3"
- Greg Hewgill's script to identify the minimum version of Python
  required to run a particular script:
  https://github.com/ghewgill/pyqver
- the Python 3 porting how to in the main docs
- cross reference to the stable ABI discussion
  in the binary extensions topic (once that exists)
- mention version classifiers for distribution metadata

除了创建一个python包所需的工作之外,通常还需要在不同版本的python上提供该包。不同的python版本可能包含不同的(或重命名的)标准库包,python版本2.x和3.x之间的更改包括语言语法的更改。

手动执行所有测试,以确保包在所有目标python版本(和oss!)可能非常耗时。幸运的是,有几个工具可以用来处理这个问题,这里将简要讨论这些工具。

自动化测试和持续集成

有几个托管服务可用于自动测试。这些服务通常会监视源代码存储库(例如 GithubBitbucket )并在每次进行新的提交时运行项目的测试套件。

这些服务还提供运行项目的测试套件的工具 python的多个版本 提供关于代码是否可以工作的快速反馈,而不需要开发人员自己执行这些测试。

维基百科有一个广泛的 comparison 许多持续集成系统。有两种托管服务,当它们结合使用时,可以跨Linux、Mac和Windows提供自动测试:

  • Travis CI 提供Linux和MacOS环境。Linux环境是Ubuntu 12.04 LTS Server Edition 64位,而MacOS在编写时是10.9.2位。

  • Appveyor 提供Windows环境(Windows Server 2012)。

TODO Either link to or provide example .yml files for these two
services.

TODO How do we keep the Travis Linux and macOS versions up-to-date in this
document?

两个 Travis CIAppveyor 需要 YAML -格式化文件作为测试说明的规范。如果任何测试失败,则可以检查该特定配置的输出日志。

对于打算使用单一源策略部署在Python2和3上的Python项目,有许多选项。

用于单源python包的工具

six 是BenjaminPeterson开发的一个工具,用于包装python 2和python 3之间的差异。这个 six 包已经得到了广泛的使用,并且可能被认为是编写单个源代码python模块的可靠方法,该模块可以在python 2和3中使用。这个 six 模块可以从python 2.5开始使用。一种叫做 modernize 由Armin Ronacher开发,可用于自动应用由 six.

类似 six, python-future 是一个在python 2和python 3源代码之间提供兼容层的包;但是,与 six, 这个包旨在提供Python2和Python3之间的互操作性,其语言语法与两个Python版本之一相匹配:一个版本可以使用

  • python 3项目中的python 2(按语法)模块。

  • 中的python 3(按语法)模块 Python 2 项目。

因为双向性, python-future 提供了一种将python 2包逐模块转换为python 3语法模块的方法。但是,与 six, python-future 仅从python 2.6支持。类似 modernize 对于 six, python-future 附带两个脚本 futurizepasteurize 它可以分别应用于python 2模块或python 3模块。

使用 sixpython-future 将其他运行时依赖项添加到包:与 python-future, 这个 futurize 可以使用调用脚本 --stage1 选项仅应用python 2.6+已经提供的对python 3的前向兼容性的更改。任何剩余的兼容性问题都需要手动更改。

哪条 Python ?

ned batchelder提供每个python版本中的更改列表,用于 Python 2Python 3.0-3.3Python 3.4-3.6 . 这些列表可用于检查Python版本之间的任何更改是否会影响您的包。

TODO These lists should be reproduced here (with permission).

TODO The py3 list should be updated to include 3.4