Take your skeletons out of the closet – make a python project skeleton

Today, children, well do some housekeeping. We’ll make a python project skeleton. It will make easy for you to jump straight into coding – for Python of course.

Step 1: Install  the following Python packages:

  1. pip from http://pypi.python.org/pypi/pip
  2. distribute from http://pypi.python.org/pyp/distribute
  3. nose from http://pypi.python.org/pypi/nose
  4. virtualenv from http://pypi.python.org/pypi/virtualenv

 Step 2: We’ll put our skeleton in a projects folder, for easy copying and editing. Create the folder structure entering the following commands in the terminal:

    $ mkdir projects

    $ cd projects/

    $ mkdir skeleton

    $ cd skeleton

    $ mkdir bin

    $ mkdir NAME

    $ mkdir tests

    $ mkdir docs

Step 3: Make initial files. Those files are used to tell python (yes they are left empty) that a folder is treated as a python module.

    $ touch NAME/__init__.py

    $ touch tests/__init__.py

Step 4: Now we make a setup.py file we can use to install our project later:

  1.     try:
  2.         from setuptools import setup
  3.     except ImportError:
  4.         from distutils.core import setup
  5.     config = {
  6.         "description": "My Project",
  7.         "author": "My Name",
  8.         "url": "URL to get it at",
  9.         "download_url": "Where to download it",
  10.         "author_email": "My Email",
  11.         "version": "0.1",
  12.         "install_requires": ["nose"],
  13.         "packages": ["NAME"],
  14.         "scripts": [],
  15.         "name": "projectname"
  16.     }
  17.  
  18.     setup(**config)

Step 5: Finally we make a simple skeleton file for tests named tests/NAME_tests.py:

  1.     from nose.tools import *
  2.     import NAME
  3.  
  4.     def setup():
  5.         print "SETUP!"
  6.    
  7.     def teardown():
  8.         print "TEAR DOWN!"
  9.    
  10.     def test_basic():
  11.         print "I RAN!"

Now if you have done it correctly, you should have a directory structure like this:

    NAME/

        __init__.py

    bin/

    docs/

    tests/

        NAME_tests.py

        __init__.py

    setup.py

Time for tests! Change dir to your skeleton folder and type: nosetests. You should see something like this:

    $ nosetests

    .

    ------------------------------------------------

    Ran 1 tests in 0.004s

    OK

Using your skeleton:

  1.  Make a copy of your skeleton directory and name it after your   project
  2.  Rename the NAME folder to the name of your project
  3.  Enter info in your config in you setup.py
  4.  Rename tests/NAME_tests.py to also have your module name.
  5.  Start coding!

 

You may also like...

Leave a Reply