tags:

views:

125

answers:

3

I'm currently maintaining two of my own applications. They both share some common aspects, and as a result, share some code. So far, I've just copied the modules from one project to the other, but now it's becoming a maintenance issue. I'd rather have the common code in one place, outside of both of the projects, which they can both import. Then, any changes to the common code would be reflected in both project.

My question is: how can I do this? Do I create a library out of this code? If so, how do the dependent projects use the library? I think one thing I struggle with here is that the common code isn't really useful to anyone else, or at least, I don't want to make it a supported modules that other people can use.

If my question isn't clear, please let me know.

+8  A: 

There is nothing special you have to do, Python just needs to find your module. This means that you have to put your common module into your PYTHONPATH, or you add their location to sys.path. See this.

Say you have

~/python/project1
~/python/project2
~/python/libs/stuff.py
~/python/libs/other.py

You can either set PYTHONPATH='~/python/libs' in your os enviroment, or you can do

import sys, os
sys.path.append(os.path.expanduser('~/python/libs')) # or give the full path

After that you can do import stuff, other anywhere.

You can also package your stuff, then you need a layout like this:

~/python/project1
~/python/project2
~/python/libs/mylibname/__init__.py
~/python/libs/mylibname/stuff.py
~/python/libs/mylibname/other.py

~/python/libs/mylibname/__init__.py must exist, but it can be a empty file. It turns mylibname into a package.

After adding the libs folder to your path as above, you can do from mylibname import stuff, other.

THC4k
That sounds very simple. So, I have my projects on disk like so:- Projects - Project 1 - Project 2 - CommonSo 'Common' would go in `PYTHONPATH`? Does that mean that setup.py also needs to set `PYTHONPATH`? Finally, what can I called the common code? I worry that `import common.something` isn't very informative.Maybe I'm worrying about small things.
rioch
+2  A: 

There are a lot of ways to factor code so it is reusable. It really depends on your specific situation as far as what will work best. Factoring your code into separate packages and modules is always a good idea, so related code stays bundled together and can be reused from other packages and modules. Factoring your code into classes within a module can also help in keeping related code grouped together.

I would say that putting common code into a module or package that is on your PYTHONPATH and available to both applications would probably be your best solution.

Matthew J Morrison
A: 

Here's how I would do it:

  • make an EGG archive of your common project:

    ~:zip common.egg common
    
  • make the egg file part of your libraries

    cp common.egg PROJECT_PATH/lib/
    
  • in your projects:

    import glob
    import os
    
    
    def main():
        path_lib=os.path.abspath(os.path.split(os.path.abspath(sys.modules['__main__'].__file__))[0] + '/../lib')
        sys.path += glob.glob(path_lib + '/*.egg')
        from common import stuff
        stuff.doCommonStuff()
    
Tommy