pythonutility-methodrequirements-management

Where to put a small utility function that I would like to use across multiple packages/projects that I develop?


Right now I have one function that would be useful in a number of distinct packages that I work on. The function is only a handful of lines. But I would like to be able to use this code in a number of packages/projects that I work on and are deployed, I would like this code to be version controlled etc. There isn't, for example, one package that all the other packages already have as a requirement, otherwise I could put this code inside of that one and import it that way.

During my time coding I've come across this issue a couple times. For concreteness some functions f that have this characteristic might be:

Basically the options I see are:

What would be the suggested way to do this? Are there other approaches I haven't mentioned?


Solution

  • The entire packaging system is designed to solve exactly this problem - sharing code between different applications. So yes, the ideally you'd want to create a package out of this and add it as a dependency to all the other packages that use this code. There are a few upsides to this option:

    But this also comes with some (potential) downsides:

    Having said that, the option of copying code to each of the packages that use f is still an option. Consider these points:


    Recommendation


    PS: Someone may recommend using a git submodule for sharing this code - DO NOT do it, managing versions isn't clean and will soon get out of hand - you'd rather just create a new package instead