Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Python 2 packages in Python 3 project?

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.

like image 216
Y.N Avatar asked Mar 14 '23 02:03

Y.N


2 Answers

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.
like image 112
Anton Tarasenko Avatar answered Mar 23 '23 04:03

Anton Tarasenko


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.

like image 31
viraptor Avatar answered Mar 23 '23 03:03

viraptor