Skip to main content

lib2to3import is a utility to apply Python 2 to 3 code translation on import.

Project description

lib2to3import is a utility to apply Python 2 to 3 code translation on import.

w/o lib2to3import

>>> from py2codes import py2_print
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "py2codes/py2_print.py", line 1
    print "Written when Python 2 was majority."
                                              ^
SyntaxError: Missing parentheses in call to 'print'

With lib2to3import

>>> from lib2to3import import lib2to3importer, prepending
>>> fixers = ["lib2to3.fixes.fix_print"]
>>> with prepending(lib2to3importer(fixers, "py2codes.")):
...     from py2codes import py2_print
...
Written when Python 2 was majority.

Limitation

There’s no way to apply fixes to 2 different roots concurrently.

When you apply fixes to both ‘foo.module’ and ‘bar.module’ at one time, you have to leave out prefix parameter, that makes fixes applied to all of modules and packages to be imported.

Concurrent import:

from lib2to3import import lib2to3importer, prepending
fixers = ["lib2to3.fixes.fix_print"]

with prepending(lib2to3importer(fixers)):
    import foo.module  #  import chain: 1. foo.module -> 2. bar.module

2 steps import (Recommended):

from lib2to3import import lib2to3importer, prepending
fixers = ["lib2to3.fixes.fix_print"]

with prepending(lib2to3importer(fixers, "bar.")):
    import bar.module  # ... 2

with prepending(lib2to3importer(fixers, "foo.")):
    import foo.module  # ... 1

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lib2to3import-2017.3.3.zip (6.8 kB view details)

Uploaded Source

File details

Details for the file lib2to3import-2017.3.3.zip.

File metadata

  • Download URL: lib2to3import-2017.3.3.zip
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for lib2to3import-2017.3.3.zip
Algorithm Hash digest
SHA256 df63a3eeac8c1156a1bffc98ae86e54d03aa8de878177cb0fc74ed425eba4fd7
MD5 0131196a9d319b009a84c0b358c24f45
BLAKE2b-256 e37a8833628f6c76d4051aeedcb7f1b435cf3fc9c56570bb9e216723fac43332

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page