Sharing code between different projects?

Steven D'Aprano steve+comp.lang.python at
Tue Aug 14 17:35:53 CEST 2012

On Mon, 13 Aug 2012 17:53:32 +0100, andrea crotti wrote:

> I started to work on project A, then switched completely to project B
> and in the transiction I copied over a lot of code with the
> corresponding tests, and I started to modify it.
> Now it's time to work again on project A, but I don't want to copy
> things over again.
> I would like to design a simple and nice way to share between projects,
> where the things I want to share are simple but useful things as for
> example:
> The problem is that there are functions/classes from many domains, so it
> would not make much sense to create a real project, and the only name I
> could give might be "utils or utilities"..

I feel your pain. "Copy and paste" is perhaps the simplest, most 
seductive, and *worst* of the developer anti-patterns.

I wish I had a solution to share with you, because I'm in the same 
position, but I don't.

The best I have come up with is a combination of:

1) For really small code snippets, just forget about sharing code. If you 
need a five-line function Foo in ten projects, just re-write them (well, 
copy and paste them...) each time. Forget about trying to keep them 

2) For bigger functions/classes, put them in their own module which you 
can share across multiple projects.

3) Consider having a "utilities" module to include all the assorted bits 
and pieces.

If your projects are in-house, then a utilities module makes more sense. 
If you're writing libraries for independent release, not so much.


More information about the Python-list mailing list