Nov. 5 2011
0

Your First Python Package

Creating a Python package, as I’ve found out while writing this post, is much more easy than I expected. Or, atleast, as far as I’m concerned. This “guide” will get you as far as your first distributable. Other meta data, registering and uploading to PyPi, and such can be found at various resources across the internet. Most notably: The Hitchhiker’s Guide to Packaging.

Package Layout

Just to make sure we’re all on the same page, let’s do a quick discussion of package layout. Let’s say I’ve already created my module called personalitywipe* in a folder of the same name. The name of the root&emdash;or package&emdash;folder (which will be one level up from the module folder) will be the name of your package: PersonalityWipe, if you follow PEP 8. Throw in a README and (preferably) LICENSE and we’ll be good to go.

PersonalityWipe/
    README
    LICENSE
    personalitywipe/
        __init__.py
        ...

The setup.py File

In the package folder, you’ll create the setup.py in which we will put the package’s meta information. It relies on setuptools to do all the fancy things it does, but it’s been included in Python for a few versions. This file will also be invoked for package building and installation.

Sidebar: setuptools just extends the functionality of distutils.

from setuptools import setup
 
setup(name='PersonalityWipe',
      version='1.0',
      description='Wipes the imprinted personality from an Active.',
      author='Topher Brink',
      author_email='topher@dollhouse.rossum.com',
      url='http://dollhouse.rossum.com/~topher/personalitywipe/',
      packages=['personalitywipe'],
     )

That data is just the base that is needed to install (and probably only packages is actually needed), but including it will make it much easier to list yourself in pypi..

Note: All modules will need to be listed in packages for the distribution to bundle correctly.

Bundling It Up

Once you’ve created your upper level files (setup.py, README, and LICENSE), you can create a distributable for the current version by calling python setup.py sdist. It will create a compressed version of your package which you can distribute in the dist folder under the root folder.

* – Don’t judge me because I like Topher from Dollhouse. :P

Leave a Reply

Previous post:

Next post:

Last.fm interface from fmTuner (modified by me).

Twitter interface from HL Twitter.

Dedicated to my mom, Sharon. May she rest peacefully.
Love you always. (March 23, 1965 - November 12, 2011)

Unsupported Browser!

My site is designed for and currently displays best with modern browsers (and it looks like you don't have one!).

Everything should still function properly, but you won't be able to see all the fancy effects, text, or the way I intended my design to look.

Consider trying the latest version of either Firefox or Chrome... I think you'll like it!

Logan Bibby

P.S.: You can continue viewing my site by clicking the "close" link and you'll never see this again. :)