Revision 4 as of 2006-08-30 20:02:04

Clear message

Introduction to distutils

I have recently used distutils for the first time and thought I would share what I have learned.

Please note: I am not a pro. All this is based on my own hacking and struggling to get things to work. I am sure there will be alternative ways to do things and that I will make mistakes. Caveat Emptor.

(Based on Gnu/Linux)

The layout of folders

A proper layout of your files and folders can really save you trouble later on. I use this layout:

top
|-- package
|   |-- __init__.py
|   |-- module.py
|   `-- things
|       |-- cross.png
|       |-- fplogo.png
|       `-- tick.png
|-- runner
|-- MANIFEST.in
|-- README
`-- setup.py

The files

I assume that there will be a single script file that you will use to start your Python app. I call it 'runner' (without the .py) in this example. It will import the actual package. Here is the code:

runner

##This will work in development on a relative folder basis
##It will then work when installed in site-packages on a target system
##where the runner script is in /usr/bin (or wherever)
##
##So, you don't need anything special - no fancy path tricks.

import package.module

package.module.start ()


Next is the package. In this case there is only one module in the package (import package.module), so here is the module: module.py {{{#!/usr/bin/env python2.4

import os, sys

def determine_path ():

def start ():

if name == "main":

}}}

This uses the magic variable __file__ that will return the path and name of the module. Without it you would have to jump through some hoops to locate the module on any given distro or O/S.

It shows that it can find the data files in the things folder.


The __init__.py file is empty


The README file is named that way because it's one of the ways that distutils automatically looks for it. You should put details about your app into it. It's not a required file.


Unable to edit the page? See the FrontPage for instructions.