Releasing python software basically means you make a zipfile of couple of grouped-together python files. And of course this has to have a name, version, etc. The standard way of doing this in python is with setuptools.
If you use a couple of python files in several projects, you probably do not want to copy them around time and again. Fix a bug in one and the fix should end up in the others, too. It is handier to just use an installed package in each of your projects instead of coping around by hand.
So if you have an existing monolithic project, see if you can extract some useful functionality out of it so you can re-use it in another project. Once you get the hang of it it can give you loads of productivity! The cheaper and easier it is to re-use existing software, the quicker you can get things done. “Cheaper” does mean an initial price in setting up things and understanding the mechanisms. I’d personally say packaging often pays for itself real quick.
Packaging is a no-brainer if you want others to use your software. Or if you have to distribute it to a customer. Non-packaged software is a maintenance nightmare waiting to happen. It needs manual steps to “install”. You cannot reliably override an older installation if all you do is putting *.py files in a directory. Old files will stay put. People will forget to copy all files, leaving an old one in place (good luck debugging that one when the customer phones you). People will copy files into the wrong directory. Just package up your software properly and you can sleep more soundly at night.
The end result you want is:
import sample
sample.utils.init_something('hurray')
bla_bla = sample.database.BlaBla()
bla_bla.calculate()
So: a couple of python files (utils.py and database.py in this case) grouped together under one name so you can import them from somewhere else (import sample or from sample import utils).
Take the python files you want to group together and put them in a directory with a good name:
You’ve probably seen instructions like:
These commands install the “mypackage” package neatly into your python’s so-called “site-packages” directory. Dig around in your python installation, you’ll find a site-packages directory in there somewhere. Start up the python prompt and you can do an import mypackage.
The setup.py file is what makes all of this work. A setup.py uses setuptools (or its replacement distribute) to actually install the software. This means placing a setup.py in a directory and putting your directory of grouped code in a subdirectory. An example:
The setup.py tells a few things like name, version, dependencies:
from setuptools import setup
setup(name='sample',
version='1.0',
description="My sample package",
long_description="",
author='Reinout',
author_email='reinout@vanrees.org',
license='GPL',
packages=['sample'],
zip_safe=False,
install_requires=['dependency1',
'dependency2'],
)
I’ve left out quite a number of possible fields here. That’s one of the problems with setup.py rightfully mentioned by Jacob Kaplan-Moss: you basically have to cargo-cult your config files from other projects.
What this example config file tells setuptools, however, is:
You of course want to install your software. Some ways to do this:
My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.
Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):