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?

5
  • How does 2to3 work? That must have some way of handling Python 2 and 3 code. Commented Nov 27, 2011 at 17:58
  • What do you need it for? Commented Nov 27, 2011 at 18:02
  • @LennartRegebro I want to preprocess source code (similar to minification, for example resolving local imports and applying compatibility code for Python 2.5 and even older platforms), and possibly implement 2to6. Commented Nov 27, 2011 at 18:07
  • Well, Implementing 2to6 is exactly what lib2to3 is for. Commented Nov 27, 2011 at 20:04
  • I could also note that I really do want a 2to6 set of fixers, so please make that. :-) Put it on github and I might even help. Commented Nov 30, 2011 at 11:23

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.