Automatically Rewriting Imports

For instance, with Werkzeug < 0.7 the recommended way to use the escape function was this:

from werkzeug import escape

With Werkzeug 0.7, the recommended way to import this function is directly from the utils module (and with 1.0 this will become mandatory). To automatically rewrite all imports one can use thewerkzeug-import-rewrite script.

You can use it by executing it with Python and with a list of folders with Werkzeug based code. It will then spit out a hg/git compatible patch file. Example patch file creation:

$ python . > new-imports.udiff

To apply the patch one of the following methods work:


hg import new-imports.udiff


git apply new-imports.udiff


patch -p1 < new-imports.udiff
© RemiZOffAlex