Decoupling the version of the file from the name of the module.

Raymond Hettinger python at
Sun Jan 29 03:07:29 EST 2006

[bobueland at]
> I'm a newbie experimenting with Python. I want to incrementally develop
> a module called 'circle'.
 . . .
> Basically I want to decouple the version of my file from the name of
> the module.
> Is there a *simple* way out of this dilemma.

In the client code, use an import/as statement and update that single
line as needed:

    import circle_b as circle

If you don't want to edit the client code every time, the import can be
automated to smartly find the most recently updated version.  Build a
list of filenames using your naming convention.  Sort them by
modification date.  Then, import the most recent one as circle:

   names = glob.glob('circle_*.py')
   names.sort(key=lambda f: os.stat(f).st_mtime)
   newest_name = names[-1]
   newest_module, ext = os.path.splitext(newest_name)
   circle = __import__(newest_module)

Of course, the right answer is to do what everyone else does.  Use a
version control system instead of multiple files.


More information about the Python-list mailing list