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?

phihag
  • 245,801
  • 63
  • 407
  • 443

1 Answers1

3

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.

Lennart Regebro
  • 147,792
  • 40
  • 207
  • 241