开发者

How to maintain different version of a python module?

开发者 https://www.devze.com 2023-01-30 07:22 出处:网络
I have this core python module we use in our facility called mfxLib. I need to be able to keep different version of this module without breaking all the other modules/plugin that are importing this mo

I have this core python module we use in our facility called mfxLib. I need to be able to keep different version of this module without breaking all the other modules/plugin that are importing this module.

My solution was keep a duplicate of my module by renaming them mfxLib01 and mfxLib02 then to replace the original mfxLib module with an empty module containing only a __init__.py file that import the latest version.

# content of mfxLib.__init__.py
from mfxLib02 import *

This seems lo开发者_如何学编程gical and seems to work but I was wondering if there was a common practice for doing this? guidelines to follow? etc

Thanks


You can import a module as another name. Commonly people use this to save typing in a long module name, for example:

import numpy as np
np.array([1,2,3,4])

Hence you could do:

import mfxLib01 as mfxLib

or

import mfxLib02 as mfxLib

then your code uses mfxLib everywhere.

That might help...


If you have different scripts requiring different versions, your current approach should be the the best, but I'd suggest using a version control system like Git or SVN. That would allow you to commit and revert to earlier versions easily, as well as share the module with other users.


Version control will almost certainly make your life easier. In addition to Petterson's recommendations, consider Mercurial. Like git and SVN, it's free. It's also written in Python and should run without difficulty on any of your systems.

Spacedman's recommendations are also useful, especially if the differences between the versions represent customizations for particular systems and the customizations are relatively stable. Note that you can use that approach in combination with a version control system.

Finally, it's always worthwhile to make a strong effort to write your module so that it can work without modification everywhere. Often, you can accomplish this by adding some optional arguments to a few key functions to handle the different requirements. Python is really convenient in that regard because keyword arguments at the end of the arg list are always optional, so you can easily arrange to provide the existing behavior by giving them suitable default values.

def foo(oldarg1, oldarg2, newarg1=None):
    if newarg1 != None:
        ## behave differently
    else:
        ## behave as usual 
0

精彩评论

暂无评论...
验证码 换一张
取 消