Python 2和Python 3双重开发 [英] Python 2 and Python 3 dual development

查看:44
本文介绍了Python 2和Python 3双重开发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始一个新的 Python 项目,理想情况下,我希望从一开始就提供 Python 2 和 3 支持,同时将开发开销降至最低.我的问题是,对于全新的项目,这样做的最佳方法是什么?

I'm just starting a new Python project, and ideally I'd like to offer Python 2 and 3 support from the start, with minimal developmental overhead. My question is, what is the best way of doing this for brand new projects?

我遇到过运行 2to3 甚至 3to2 的项目,作为其安装脚本的一部分.这似乎是一种很常见的方式.但是,似乎有几种不同的方法可以做到这一点.我还遇到了Distribute.

I have come across projects that run 2to3, or even 3to2, as part of their installation script. This seems to be a very common way. However, there seems to be several different ways of doing this. I also came across Distribute.

还可以选择尝试编写多语言 Python 2/Python 3 代码.尽管这似乎是一个可怕的想法,但我注意到我最近倾向于编写更像 Python 3 代码那样惯用的代码,即使我仍然将它作为 Python 2 运行.我有一种感觉,这只会帮助我自己的转换,当这一天终于到来了,但并没有提供或至少帮助双重支持.

There is also the option of trying to write polyglot Python 2/Python 3 code. Even though this seems like a horrible idea, I have noticed that I tend to write code lately that is more idiomatic as Python 3 code, even though I still run it as Python 2. I have a feeling this only helps my own transition when the day finally arrives, and doesn't do much for offering or at least helping dual support though.

我看到的大多数提供双重支持的项目都在后期添加了 Python 3 支持,所以我特别好奇是否有更适合新项目的更好方法,您可以在新项目中受益.

Most of the projects offering dual support that I have seen added Python 3 support late, so I'm especially curious if there is a better way that is more suited for new projects, where you have the benefit of a clean slate.

谢谢!

推荐答案

以我的经验,这取决于项目的类型.

In my experience, it depends on the kind of project.

如果它是一个库或非常独立的应用程序,一个常见的选择是在 Python 2.7 中开发,尽可能避免在 Python 3.x 中弃用的构造,并求助于自动化测试来识别 py2to3 留下的漏洞,你将不得不这样做手动修复.

If it is a library or very self contained application, a common choice is develop in Python 2.7 avoiding constructs deprecated in Python 3.x as much as possible and resort to automated tests to identify holes left by py2to3 that you will have to fix manually.

另一方面,对于现实生活中的应用程序,准备好不断偶然发现尚未移植到 py3k 的库(有时是重要的库).大多数时候你别无选择,只能将库移植到 Python 3,所以如果你能负担得起,那就去吧.通常我不能,这就是为什么我不支持 Python 3 用于此类项目(但我努力编写在合适的时候更容易移植的代码).

On the other side, for real life applications, be prepared to constantly stumble upon libraries that are not ported to py3k yet (sometimes important ones). Most of the time you will have no choice but port the library to Python 3, so if you can afford that, go for it. Usually I can't, that is why I'm not supporting Python 3 for this kind of project (but I struggle to write code that will be easier to port when opportune).

对于unicode处理,我发现了这个PyCon 2012 视频 非常有用.该建议对 Python 2.x 和 3.x 都有好处:将来自外部的每个字符串视为字节并尽快转换为 unicode,并尽可能晚地将输出字符串转换为字节.还有另一个关于日期/时间处理.

For unicode handling, I found this PyCon 2012 video very informative. The advice is good for both Python 2.x and 3.x: treat every string coming from outside as bytes and convert to unicode as soon as possible and output strings converting to bytes as late as possible. There is another very informative video about date/time handling.

[更新]

这是一个旧答案.截至今天(2019 年),使用 Python 2.x 启动项目没有充分的理由,并且有几个令人信服的理由将旧项目移植到 Python 3.7+ 并放弃对 Python 2.x 的支持.

这篇关于Python 2和Python 3双重开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆