How to use Python 2 packages in Python 3 project?

谁都会走 提交于 2020-12-01 13:12:27

问题


How to use python 2 packages in python 3 project?

I have a Python 3 project, but I need some packages which are written in Python 2.

I do not want to rewrite these python-2 packages, so forking / 2to3 is not an option.


回答1:


You can't. Any module you import in py3 codebase needs to be py3 compatible. If you can't make the upstream project do it for you, you'll have to do it yourself. As mentioned in the comments, 2to3 utility should help you with that.




回答2:


A lot of the previous questions concern using Python 2 modules in Python 3 projects. Here's the most complete explanation. It also works the other way around and suggests the following alternatives:

  1. Using the subprocess module for requests and getting the response through the CLI. So far the most straightforward way to make cross-references. python-fire can quickly add a CLI to a Python 3 library. You'll have a reasonable API to call from the Python 2 project.
  2. 3to2. This utility converts Python 3 code to Python 2. It remains unclear how it handles complex Python 3 projects with dependencies and Python 3-only features.
  3. six and __future__. It seems both of these modules are intended for writing the code compatible with Python 2-3, not for using Python 3 libraries in Python 2 projects.


来源:https://stackoverflow.com/questions/33885975/how-to-use-python-2-packages-in-python-3-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!