phihag
phihag

Reputation: 288298

Library to parse Python 2 and 3

I'm looking for a library that allows parsing and modification of Python 3 source code. There is the built-in ast module, but that doesn't allow parsing Python 3 code from Python 2 and vice versa.

Is there such a library, or a way to make the ast module recognize Python 2 code on Python 3?

Upvotes: 3

Views: 449

Answers (1)

Lennart Regebro
Lennart Regebro

Reputation: 172447

Python's lib2to3 library includes a code parser can can parse both Python 2 and Python 3 code. It's not well documented though. This chapter from the porting book provides an introduction. If you want to modify code (refactoring, maybe) then 2to3 could very well be exactly what you are looking for.

Upvotes: 3

Related Questions