python二进制文件的操作步骤是什么_Python(23)常用指引:将 Python 2 代码迁移到 Python 3...

Python 常用指引

Python常用指引的形式来源于Linux文档项目的常用指引章节,是一系列独立、指定主题并尝试完全覆盖该主题的文章集合。致力于提供比Python库参考帮助更详尽的文档。目前,提供了如下指引:
  • 将 Python 2 代码迁移到 Python 3
  • 将扩展模块移植到 Python 3
  • 用 Python 进行 Curses 编程
  • 描述器使用指南
  • 函数式编程指引
  • 日志常用指引
  • 日志操作手册
  • 正则表达式HOWTO
  • 套接字编程指南
  • 排序指南
  • Unicode 指南
  • HOWTO 使用 urllib 包获取网络资源
  • Argparse 教程
  • ipaddress模块介绍
  • Argument Clinic How-To
  • 使用 DTrace 和 SystemTap 检测CPython

将 Python 2 代码迁移到 Python 3

  • 作者
  • Brett Cannon
摘要Python 3 是 Python 的未来,但 Python 2 仍处于活跃使用阶段,最好让您的项目在两个主要版本的Python 上都可用。本指南旨在帮助您了解如何最好地同时支持 Python 2 和 3。如果您希望迁移扩展模块而不是纯 Python 代码,请参阅  将扩展模块移植到 Python 3。如果你想了解核心 Python 开发者对于 Python 3 的出现有何看法,你可以阅读 Nick Coghlan 的 Python 3 Q & A( https://ncoghlan-devs-python-notes.readthedocs.io/en/latest/python3/questions_and_answers.html)或 Brett Cannon 的 为什么要有 Python 3( https://snarky.ca/why-python-3-exists)。有关迁移的帮助,您可以通过电子邮件向 python-porting 邮件列表发送问题。

简要说明

To make your project be single-source Python 2/3 compatible, the basic steps are:
  1. Only worry about supporting Python 2.7【译:仅关心支持Python 2.7】
  2. 确保有良好的测试覆盖率(可以用 coverage.py 来完成此任务;用 pip install coverage 安装)
  3. 了解Python 2 和 3之间的区别
  4. 使用 Futurize (或 Modernize) 升级你的代码 (例如. pip install future)
  5. Use Pylint to help make sure you don't regress on your Python 3 support (pip install pylint)【使用Pylint帮助确保不会在Python 3支持上倒退】
  6. Use caniusepython3 to find out which of your dependencies are blocking your use of Python 3 (pipinstall caniusepython3)【使用caniusepython3找出哪些依赖项阻碍了Python 3的使用】
  7. Once your dependencies are no longer blocking you, use continuous integration to make sure you stay compatible with Python 2 & 3 (tox can help test against multiple versions of Python; pip install tox)【一旦依赖项不再阻碍您,请使用持续集成来确保您与Python2&3保持兼容(tox可以帮助测试Python的多个版本)】
  8. Consider using optional static type checking to make sure your type usage works in both Python 2 & 3 (e.g. use mypy to check your typing under both Python 2 & Python 3).【考虑使用可选的静态类型检查,以确保您的类型用法在Python 2和Python 3中都有效(例如使用mypy检查Python 2和Python 3下的输入)】

详情

A key point about supporting Python 2 & 3 simultaneously is that you can start  today! Even if your dependencies are not supporting Python 3 yet that does not mean you can't modernize your code  now to support Python 3. Most changes required to support Python 3 lead to cleaner code using newer practices even in Python 2 code.关于同时支持Python 2和Python 3的一个关键点是您可以从今天开始!即使您的依赖项还不支持Python3,但这并不意味着您现在不能对代码进行现代化以支持Python 3。支持Python 3所需的大多数更改都会导致使用更新实践的更干净的代码,即使在Python 2代码中也是如此。Another key point is that modernizing your Python 2 code to also support Python 3 is largely automated for you. While you might have to make some API decisions thanks to Python 3 clarifying text data versus binary data, the lower-level work is now mostly done for you and thus can at least benefit from the automated changes immediately.另一个关键点是,将Python 2代码现代化以支持Python 3在很大程度上是自动化的。与之相比,你现在可以从Python的自动数据修改中得到一些好处,而现在你可以从Python的自动修改中得到一些好处。Keep those key points in mind while you read on about the details of porting your code to support Python 2 & 3 simultaneously.在继续阅读移植代码以同时支持Python 2 和 Python 3的详细信息时,请记住这些要点。

删除对Python 2.6及更早版本的支持

While you can make Python 2.5 work with Python 3, it is  much easier if you only have to work with Python 2.7. If dropping Python 2.5 is not an option then the six project can help you support Python 2.5 & 3 simultaneously ( pip install six). Do realize, though, that nearly all the projects listed in this HOWTO will not be available to you.虽然您可以让Python 2.5与Python 3一起工作,但如果您只需要使用Python 2.7,则会更加容易。如果不能删除Python 2.5,那么 six 项目可以帮助您同时支持Python 2.5 和 Python 3(pip install six)。不过,请务必意识到,本指南中列出的几乎所有项目都将无法提供给您。【☆】言外之意,你必须放弃Python 2.5,除非你不在做任何版本升级。If you are able to skip Python 2.5 and older, then the required changes to your code should continue to look and feel like idiomatic Python code. At worst you will have to use a function instead of a method in some instances or have to import a function instead of using a built-in one, but otherwise the overall transformation should not feel foreign to you.如果您能够跳过Python 2.5及更高版本,那么对代码所做的所需更改应该看起来和感觉像惯用的Python代码。最坏的情况是,某些情况你必须使用函数而不是方法,或者必须导入函数而不是使用内置函数,但否则整体转换对您来说不应该感到陌生。But you should aim for only supporting Python 2.7. Python 2.6 is no longer freely supported and thus is not receiving bugfixes. This means  you will have to work around any issues you come across with Python 2.6. There are also some tools mentioned in this HOWTO which do not support Python 2.6 (e.g., Pylint), and this will become more commonplace as time goes on. It will simply be easier for you if you only support the versions of Python that you have to support.但您应该只支持Python 2.7。Python 2.6不再受到免费支持,因此没有收到错误修复。这意味着您必须解决使用Python 2.6遇到的任何问题。本指南中还提到了一些不支持Python 2.6的工具(例如Pylint),随着时间的推移,这将变得越来越常见。如果您只支持您必须支持的Python版本,这对您来说会更简单。

Make sure you specify the proper version support in your setup.py file

确保在setup.py文件中指定了正确的版本支持

In your  setup.py file you should have the proper trove classifier specifying what versions of Python you support. As your project does not support Python 3 yet you should at least have  Programming Language :: Python :: 2 ::Only specified. Ideally you should also specify each major/minor version of Python that you do support, e.g. Programming Language :: Python :: 2.7.在setup.py文件中应有适当的分类描述来指定支持的Python版本。如果项目不支持Python 3,那么至少应指定: Programming Language :: Python :: 2 ::Only 最好指定支持的每个Python版本的主版本号和次版本的,例如: Programming Language :: Python :: 2.7 良好的测试覆盖率Once you have your code supporting the oldest version of Python 2 you want it to, you will want to make sure your test suite has good coverage. A good rule of thumb is that if you want to be confident enough in your test suite that any failures that appear after having tools rewrite your code are actual bugs in the tools and not in your code. If you want a number to aim for, try to get over 80% coverage (and don't feel bad if you find it hard to get better than 90% coverage). If you don't already have a tool to measure test coverage then coverage.py is recommended.一旦您的代码支持您所希望的Python 2的最旧版本,您将需要确保您的测试套件具有良好的覆盖率。一个好的经验法则是,如果你想对你的测试套件有足够的信心,让工具重写你的代码之后出现的任何故障都是工具中的实际错误,而不是代码中的错误。如果你想要一个目标数字,试着获得超过80%的覆盖率(如果你发现很难获得超过90%的覆盖率,也不要难过)。如果您还没有一个工具来测量测试覆盖率,那么推荐 coverage.py(https://pypi.org/project/coverage)。

了解Python 2 和 3之间的区别

Once you have your code well-tested you are ready to begin porting your code to Python 3! But to fully understand how your code is going to change and what you want to look out for while you code, you will want to learn what changes Python 3 makes in terms of Python 2. Typically the two best ways of doing that is reading the "What's New" doc for each release of Python 3 and the Porting to Python 3 book (which is free online). There is also a handy cheat sheet from the Python-Future project.一旦您的代码经过了良好的测试,您就可以开始将代码移植到Python3!但是为了完全理解代码将如何变化,以及在编写代码时需要注意的内容,您需要了解Python 3对Python 2所做的更改。通常最好的两种方法是阅读Python 3的每个版本的“ What's New”文档和 移植到Python 3(在线免费 http://python3porting.com/)。Python Future项目中还有一个方便的 cheat sheet( http://python-future.org/compatible_idioms.html)。

更新代码

Once you feel like you know what is different in Python 3 compared to Python 2, it's time to update your code! You have a choice between two tools in porting your code automatically: Futurize and Modernize. Which tool you choose will depend on how much like Python 3 you want your code to be. Futurize does its best to make Python 3 idioms and practices exist in Python 2, e.g. backporting the  bytes type from Python 3 so that you have semantic parity between the major versions of Python. Modernize, on the other hand, is more conservative and targets a Python 2/3 subset of Python, directly relying on six to help provide compatibility. As Python 3 is the future, it might be best to consider Futurize to begin adjusting to any new practices that Python 3 introduces which you are not accustomed to yet.一旦您觉得自己知道了Python 3与Python2的不同,就应该更新代码了!在自动移植代码时,您可以在两种工具之间进行选择: Futurize(http://python-future.org/automatic_conversion.html)和Modernize(https://python-modernize.readthedocs.io/)。您选择哪种工具取决于您希望代码与Python 3有多相似。Futurize尽力使Python 3的习惯用法和实践存在于Python 2中,例如,从Python 3向后移植bytes类型,以便在Python的主要版本之间实现语义对等。另一方面,Modernize更保守,它的目标是Python 2/3的子集,直接依赖于six来帮助提供兼容性。由于Python 3是未来,最好考虑Futurize,开始适应Python 3引入的任何您还不习惯的新实践。Regardless of which tool you choose, they will update your code to run under Python 3 while staying compatible with the version of Python 2 you started with. Depending on how conservative you want to be, you may want to run the tool over your test suite first and visually inspect the diff to make sure the transformation is accurate. After you have transformed your test suite and verified that all the tests still pass as expected, then you can transform your application code knowing that any tests which fail is a translation failure.无论您选择哪种工具,它们都会更新您的代码以在Python 3下运行,同时与您开始使用的Python 2版本保持兼容。根据您希望的保守程度,您可能希望首先在测试套件上运行该工具,然后目视检查差异,以确保转换是准确的。在转换测试套件并验证所有测试仍按预期通过之后,您可以转换应用程序代码,知道任何失败的测试都是转换失败。Unfortunately the tools can't automate everything to make your code work under Python 3 and so there are a handful of things you will need to update manually to get full Python 3 support (which of these steps are necessary vary between the tools). Read the documentation for the tool you choose to use to see what it fixes by default and what it can do optionally to know what will (not) be fixed for you and what you may have to fix on your own (e.g. using  io.open() over the built-in  open() function is off by default in Modernize). Luckily, though, there are only a couple of things to watch out for which can be considered large issues that may be hard to debug if not watched for.不幸的是,这些工具并不能使您的代码在Python 3下完全自动实现正常工作,因此您需要手动更新一些东西来获得Python 3的完全支持(这些步骤中哪些是必需的,因工具而异)。阅读您选择使用的工具的文档,了解它在默认情况下修复了什么,以及它可以选择性地执行哪些操作,以了解哪些将(不)为您修复,以及您可能需要自己修复的内容(例如,默认情况下,在Modernize中使用 io.open(),将会使内置 open()函数无效)。不过,幸运的是,只有几件事需要注意,它们可以被认为是大问题,如果不注意,这些问题可能很难调试。
除法
In Python 3,  5 / 2 == 2.5 and not  2; all division between  int values result in a  float. This change has actually been planned since Python 2.2 which was released in 2002. Since then users have been encouraged to add  from __future__ import division to any and all files which use the  / and  // operators or to be running the interpreter with the  -Q flag. If you have not been doing this then you will need to go through your code and do two things:在Python 3中, 5   /   2   ==   2.5而不是2; int值之间的所有除法都会产生一个float。这个变化实际上是从2002年发布的python2.2开始的。从那时起,我们鼓励用户将 from __future__ import division添加到使用/和//运算符的任何和所有文件中,或者使用  -Q 标志运行解释器。如果您没有执行此操作,则需要检查代码并执行以下两项操作:
  1. Add from __future__ import division to your files【在文件中增加语句 from __future__ import division 】
  2. Update any division operator as necessary to either use // to use floor division or continue using / and expect a float【使用地板除运算符//获得整数结果,或者继续使用除号/得到浮点数】
The reason that  / isn't simply translated to  // automatically is that if an object defines a  __truediv__ method but not  __floordiv__ then your code would begin to fail (e.g. a user-defined class that uses  / to signify some operation but not  // for the same thing or at all).这是因为,  / 不能简单地自动转换为  // 。如果一个对象定义了一个  __truediv__ 方法而不是一个  __floordiv__ 方法,那么代码就会失败(例如,一个用户定义的类,它使用  / 来表示某个操作,而不是  // 用于同一个事物,或者根本就不是  // )。
文本与二进制数据
In Python 2 you could use the  str type for both text and binary data. Unfortunately this confluence of two different concepts could lead to brittle code which sometimes worked for either kind of data, sometimes not. It also could lead to confusing APIs if people didn't explicitly state that something that accepted  str accepted either text or binary data instead of one specific type. This complicated the situation especially for anyone supporting multiple languages as APIs wouldn't bother explicitly supporting  unicode when they claimed text data support.在Python 2中,可以对文本和二进制数据使用  str 类型。不幸的是,这两个不同概念的融合可能导致脆弱的代码,有时适用于任何一种类型的数据,有时不适用。如果人们没有明确声明接受  str 的东西接受文本或二进制数据而不是一种特定的类型,这也可能导致API混乱。这使情况复杂化,尤其是对于那些支持多种语言作为API的人来说,当他们声称支持文本数据时,不会费心显式地支持  unicode 。To make the distinction between text and binary data clearer and more pronounced, Python 3 did what most languages created in the age of the internet have done and made text and binary data distinct types that cannot blindly be mixed together (Python predates widespread access to the internet). For any code that deals only with text or only binary data, this separation doesn't pose an issue. But for code that has to deal with both, it does mean you might have to now care about when you are using text compared to binary data, which is why this cannot be entirely automated.为了使文本和二进制数据之间的区别更清楚、更明显,Python 3做了互联网时代大多数语言所做的事情,并使文本和二进制数据具有不同的类型,不能盲目地混合在一起(Python早于互联网的广泛访问)。对于任何只处理文本或二进制数据的代码,这种分离并不构成问题。但是对于必须同时处理这两个问题的代码来说,这确实意味着您现在可能需要关心何时使用文本而不是二进制数据,这就是为什么这不能完全自动化。To start, you will need to decide which APIs take text and which take binary (it is  highly recommended you don't design APIs that can take both due to the difficulty of keeping the code working; as stated earlier it is difficult to do well). In Python 2 this means making sure the APIs that take text can work with  unicode and those that work with binary data work with the  bytes type from Python 3 (which is a subset of  str in Python 2 and acts as an alias for  bytes type in Python 2). Usually the biggest issue is realizing which methods exist on which types in Python 2 & 3 simultaneously (for text that's  unicode in Python 2 and  str in Python 3, for binary that's  str/ bytesin Python 2 and  bytes in Python 3). The following table lists the  unique methods of each data type across Python 2 & 3 (e.g., the  decode() method is usable on the equivalent binary data type in either Python 2 or 3, but it can't be used by the textual data type consistently between Python 2 and 3 because  str in Python 3 doesn't have the method). Do note that as of Python 3.5 the  __mod__ method was added to the bytes type.首先,您需要决定哪些API接受文本,哪些API采用二进制( 强烈建议您不要设计既可以接受文本又可以接受二进制的API,因为很难保持代码的正常工作;正如前面所述,很难做好这两项工作)。在Python 2中,这意味着确保接受文本的API可以使用  unicode ,而那些处理二进制数据的API使用Python 3的  bytes 类型(Python 3是Python 2中  str 的子集,在Python 2中充当  bytes 类型的别名)。通常,最大的问题是实现在Python 2和Python 3中哪些类型同时存在哪些方法(对于Python 2中是  unicode 的文本,在Python 3中是  str/ bytes ,在Python 3中是  bytes )。下表列出了Python 2和3中每种数据类型的唯一方法(例如,  decode() 方法在Python 2或3中的等效二进制数据类型上都可用,但它不能被Python 2和3之间的文本数据类型一致地使用,因为Python 3中的  str 没有该方法)。请注意,从Python 3.5开始,  __mod__ 方法被添加到  bytes 类型中。
文本数据二进制数据
decode
encode
format
isdecimal
isnumeric
Making the distinction easier to handle can be accomplished by encoding and decoding between binary data and text at the edge of your code. This means that when you receive text in binary data, you should immediately decode it. And if your code needs to send text as binary data then encode it as late as possible. This allows your code to work with only text internally and thus eliminates having to keep track of what type of data you are working with.通过对二进制数据和代码边缘的文本进行编码和解码,可以使区分更容易处理。这意味着当您接收到二进制数据中的文本时,应该立即对其进行解码。如果你的代码需要以二进制数据的形式发送文本,那么就 尽可能晚地对它进行编码。这允许您的代码只在内部处理文本,因此无需跟踪您正在处理的数据类型。The next issue is making sure you know whether the string literals in your code represent text or binary data. You should add a  b prefix to any literal that presents binary data. For text you should add a  u prefix to the text literal. (there is a  __future__ import to force all unspecified literals to be Unicode, but usage has shown it isn't as effective as adding a  b or  u prefix to all literals explicitly)下一个问题是确保您知道代码中的字符串表示文本还是二进制数据。您应该在任何表示二进制数据的文本中添加一个b前缀。对于文本,您应该在文本文本中添加一个u前缀。(有一个 __fu ture__导入来强制所有未指定的文本都是Unicode,但使用表明,它不如在所有文本中显式添加  b 或  u 前缀那样有效。)As part of this dichotomy you also need to be careful about opening files. Unless you have been working on Windows, there is a chance you have not always bothered to add the  b mode when opening a binary file (e.g.,  rb for binary reading). Under Python 3, binary files and text files are clearly distinct and mutually incompatible; see the  io module for details. Therefore, you  must make a decision of whether a file will be used for binary access (allowing binary data to be read and/or written) or textual access (allowing text data to be read and/or written). You should also use  io.open() for opening files instead of the built-in  open() function as the  io module is consistent from Python 2 to 3 while the built-in  open() function is not (in Python 3 it's actually  io.open()). Do not bother with the outdated practice of using  codecs.open() as that's only necessary for keeping compatibility with Python 2.5.作为这种二分法的一部分,您还需要小心打开文件。除非您一直在Windows上工作,否则在打开一个二进制文件(例如,用于二进制读取的  rb )时,您可能并不总是费心添加  b 模式。在Python 3中,二进制文件和文本文件明显不同,并且互不兼容;有关详细信息,请参阅  io 模块。因此,您必须决定文件是用于二进制访问(允许读取和/或写入二进制数据)还是用于文本访问(允许读取和/或写入文本数据)。你也应该使用  io.open() 打开文件,而不是内置的  open() 函数,因为从Python 2到3,  io 模块是一致的,而内置的  open() 函数则不一致(在Python 3中实际上是  io.open() )。不要为过时的使用习惯而使用  codecs.open() ,因为这只是保持与Python 2.5兼容所必需的。The constructors of both  str and  bytes have different semantics for the same arguments between Python 2 & 3. Passing an integer to  bytes in Python 2 will give you the string representation of the integer:  bytes(3) == '3'. But in Python 3, an integer argument to  bytes will give you a bytes object as long as the integer specified, filled with null bytes:  bytes(3) == b'\x00\x00\x00'. A similar worry is necessary when passing a bytes object to  str. In Python 2 you just get the bytes object back:  str(b'3') == b'3'. But in Python 3 you get the string representation of the bytes object:  str(b'3') == "b'3'". s t r和 bytes的构造函数对于Python 2和Python 3之间的相同参数具有不同的语义。在Python 2中,将一个整数传递给 bytes将得到整数的字符串表示: bytes(3) == '3'。但是在Python 3中,只要指定的整数是空字节, bytes的整型参数就会给您一个 bytes 对象:  bytes(3) == b'\x00\x00\x00'。当向 s t r传递一个 bytes对象时,同样的担心也是必要的。但是在Python 3中,您可以得到 bytes对象的字符串表示: str(b'3') == "b'3'"。Finally, the indexing of binary data requires careful handling (slicing does  not require any special handling). In Python 2,  b'123'[1] == b'2' while in Python 3  b'123'[1] == 50. Because binary data is simply a collection of binary numbers, Python 3 returns the integer value for the byte you index on. But in Python 2 because  bytes ==str, indexing returns a one-item slice of bytes. The six project has a function named  six.indexbytes() which will return an integer like in Python 3:  six.indexbytes(b'123', 1).最后,二进制数据的索引需要小心处理(切片不需要任何特殊处理)。在Python 2中,  b'123'[1] == b'2' ,而在Python 3中  b'123'[1] == 50。因为二进制数据只是二进制数的集合,所以Python 3返回索引所在字节的整数值。但是在Python 2中,因为 bytes==str,索引返回一个字节的一项切片。六个项目有一个名为  six.indexbytes() 将返回一个整数,如Python 3中所示:  six.indexbytes(b'123', 1)。To summarize:
  1. Decide which of your APIs take text and which take binary data
  2. Make sure that your code that works with text also works with unicode and code for binary data works with bytes in Python 2 (see the table above for what methods you cannot use for each type)
  3. Mark all binary literals with a b prefix, textual literals with a u prefix
  4. Decode binary data to text as soon as possible, encode text as binary data as late as possible
  5. Open files using io.open() and make sure to specify the b mode when appropriate
  6. Be careful when indexing into binary data

小结:

  1. 决定哪些API接受文本,哪些接受二进制数据

  2. 确保处理文本的代码也可以使用unicode,二进制数据的代码也可以使用Python2中的字bytes(请参阅上表了解不能用于每种类型的方法)

  3. b前缀标记所有二进制文字,用u前缀标记文本文字

  4. 尽快将二进制数据解码为文本,尽可能晚地将文本编码为二进制数据

  5. 使用 io.open() 打开文件并确保在适当时指定b模式

  6. 索引二进制数据时要小心

Use feature detection instead of version detection
使用功能检测代替版本检测Inevitably you will have code that has to choose what to do based on what version of Python is running. The best way to do this is with feature detection of whether the version of Python you're running under supports what you need. If for some reason that doesn't work then you should make the version check be against Python 2 and not Python 3. To help explain this, let's look at an example.不可避免地,您的代码必须根据运行的Python版本来选择要执行的操作。在Python版本下,你需要做的最好的检测功能是什么。如果出于某种原因,这不起作用,那么应该针对Python 2而不是Python 3进行版本检查。为了帮助解释这一点,让我们看一个例子。Let's pretend that you need access to a feature of  importlib that is available in Python's standard library since Python 3.3 and available for Python 2 through importlib2 on PyPI. You might be tempted to write code to access e.g. the  importlib.abc module by doing the following:假设您需要访问  importlib 的一个特性,该特性在Python 3.3之后的Python标准库中可用,并且通过PyPI上的importlib2可用于Python 2。您可能会想编写代码来访问,例如  importlib.abc 通过如下操作实现模块化:
import sysif sys.version_info[0] == 3:from importlib import abcelse:from importlib2 import abc
The problem with this code is what happens when Python 4 comes out? It would be better to treat Python 2 as the exceptional case instead of Python 3 and assume that future Python versions will be more compatible with Python 3 than Python 2:
import sysif sys.version_info[0] > 2:from importlib import abcelse:from importlib2 import abc
The best solution, though, is to do no version detection at all and instead rely on feature detection. That avoids any potential issues of getting the version detection wrong and helps keep you future-compatible:不过,最好的解决方案是根本不进行版本检测,而是依赖于特征检测。这样可以避免版本检测出错的任何潜在问题,并有助于保持将来的兼容性:
try:from importlib import abcexcept ImportError:from importlib2 import abc

Prevent compatibility regressions

避免兼容性倒退Once you have fully translated your code to be compatible with Python 3, you will want to make sure your code doesn't regress and stop working under Python 3. This is especially true if you have a dependency which is blocking you from actually running under Python 3 at the moment.一旦您将代码完全翻译为与Python 3兼容,您将需要确保您的代码不会倒退并停止在Python 3下工作。如果您有一个依赖项,它会阻止您在Python 3下实际运行,这一点尤其正确。To help with staying compatible, any new modules you create should have at least the following block of code at the top of it:为了帮助保持兼容,您创建的任何新模块的顶部至少应包含以下代码块:
from __future__ import absolute_importfrom __future__ import divisionfrom __future__ import print_function
You can also run Python 2 with the  -3 flag to be warned about various compatibility issues your code triggers during execution. If you turn warnings into errors with  -Werror then you can make sure that you don't accidentally miss a warning.您还可以运行带有  -3 标志的Python 2,以警告您在执行期间代码触发的各种兼容性问题。如果使用  -Werror 将警告转换为错误,则可以确保不会意外错过警告。You can also use the Pylint project and its  --py3k flag to lint your code to receive warnings when your code begins to deviate from Python 3 compatibility. This also prevents you from having to run Modernize or Futurizeover your code regularly to catch compatibility regressions. This does require you only support Python 2.7 and Python 3.4 or newer as that is Pylint's minimum Python version support.您还可以使用Pylint项目及其  --py3k 标志来lint代码,以便在代码开始偏离Python 3兼容性时收到警告。这也避免了您必须定期运行代码现代化或未来化来捕捉兼容性倒退。这需要您只支持Python 2.7和Python 3.4或更高版本,因为这是Pylint的最低Python版本支持。

Check which dependencies block your transition

检查哪些依赖阻止了版本迁移

After you have made your code compatible with Python 3 you should begin to care about whether your dependencies have also been ported. The caniusepython3 project was created to help you determine which projects -- directly or indirectly -- are blocking you from supporting Python 3. There is both a command-line tool as well as a web interface at https://caniusepython3.com.在您使代码与Python 3兼容之后,您应该开始关心您的依赖项是否也被移植了。创建caniusepython3(https://pypi.org/project/caniusepython3)项目是为了帮助您确定哪些项目(直接或间接)阻碍了对Python 3的支持。有一个命令行工具和一个web界面在 https://caniusepython3.com。The project also provides code which you can integrate into your test suite so that you will have a failing test when you no longer have dependencies blocking you from using Python 3. This allows you to avoid having to manually check your dependencies and to be notified quickly when you can start running on Python 3.该项目还提供了可以集成到测试套件中的代码,这样当不再有依赖项阻止您使用Python 3时,您将有一个失败的测试。这使您可以避免手动检查依赖项,并在可以开始运行Python 3时得到快速通知。

Update your setup.py file to denote Python 3 compatibility

更新setup.py文件标明对Python 3的兼容性

Once your code works under Python 3, you should update the classifiers in your  setup.py to contain  ProgrammingLanguage :: Python :: 3 and to not specify sole Python 2 support. This will tell anyone using your code that you support Python 2  and 3. Ideally you will also want to add classifiers for each major/minor version of Python you now support.一旦代码在Python 3下运行,就应该更新  setup.py 文件,使其包含  ProgrammingLanguage :: Python :: 3,而不指定Python 2的唯一支持。这将告诉任何使用您的代码的人您支持Python 2和3。最好为支持的每个Python版本的主/次版本号增加类别说明。

Use continuous integration to stay compatible

使用持续集成来保持兼容性

Once you are able to fully run under Python 3 you will want to make sure your code always works under both Python 2 & 3. Probably the best tool for running your tests under multiple Python interpreters is tox. You can then integrate tox with your continuous integration system so that you never accidentally break Python 2 or 3 support.一旦您能够在Python 3下完全运行,您将需要确保您的代码始终在Python 2和3下运行。在多个Python解释器下运行测试的最佳工具可能是 tox(https://pypi.org/project/tox)。然后,您可以将 tox与您的持续集成系统集成,这样您就不会意外地破坏Python 2或3的支持。You may also want to use the  -bb flag with the Python 3 interpreter to trigger an exception when you are comparing bytes to strings or bytes to an int (the latter is available starting in Python 3.5). By default type-differing comparisons simply return  False, but if you made a mistake in your separation of text/binary data handling or indexing on bytes you wouldn't easily find the mistake. This flag will raise an exception when these kinds of comparisons occur, making the mistake much easier to track down.您可能还希望在Python3解释器中使用 - bb标志,以便在将字节与字符串或字节与int进行比较时触发异常(后者从Python 3.5开始提供)。默认情况下,类型差异比较只返回 False,但是如果在文本/二进制数据处理或字节索引的分离过程中出错,那么就很难找到这个错误。当这些类型的比较发生时,此标志将引发一个异常,使错误更容易追踪。And that's mostly it! At this point your code base is compatible with both Python 2 and 3 simultaneously. Your testing will also be set up so that you don't accidentally break Python 2 or 3 compatibility regardless of which version you typically run your tests under while developing.基本上就是这样!此时,您的代码库同时与Python 2和3兼容。您的测试也将被设置,这样您就不会意外地破坏Python 2或3的兼容性,而不管您在开发时通常在哪个版本下运行测试。

考虑使用可选的静态类型检查

Another way to help port your code is to use a static type checker like mypy or pytype on your code. These tools can be used to analyze your code as if it's being run under Python 2, then you can run the tool a second time as if your code is running under Python 3. By running a static type checker twice like this you can discover if you're e.g. misusing binary data type in one version of Python compared to another. If you add optional type hints to your code you can also explicitly state whether your APIs use textual or binary data, helping to make sure everything functions as expected in both versions of Python.另一种帮助移植代码的方法是在代码上使用 mypy(http://mypy-lang.org/)或 pytype(https://github.com/google/pytype)之类的静态类型检查器。这些工具可以用来分析代码,就好像它是在Python 2下运行一样,然后您可以像代码在Python 3下运行一样再次运行该工具。通过像这样运行两次静态类型检查器,您可以发现在Python的一个版本与另一个版本相比是否误用了二进制数据类型。如果在代码中添加可选类型提示,还可以显式地声明api是使用文本数据还是二进制数据,这有助于确保两个版本的Python中的所有功能都符合预期。
楼猪说明python.org发布的当前文档,大部分内容是英文,楼猪在此编排为中英文对照形式。如果仅出现中文而没有英文原文,则表示该中文出自python.org。

b5ad615154a9c0b4574ea157a3e327c9.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值