A Project Skeleton for Python

This article is edited from Learn Pthon: The Hard Way 3rd Edition Exercise 46 A Project Skeleton.

Preface

This will be where you start learning how to set up a good project “skeleton” directory. This skeleton directory will have all the basics you need to get a new project up and running. It will have your project layout, automated tests, modules, and install scripts. When you go to make a new project, just copy this directory to a new name and edit the files to get started.

Installing Python Packages (Ubuntu)

Installing pip (root)

pip: A tool for installing and managing Python packages.

apt-get update

apt-get upgrade

apt-get install python-pip

Installing nose (root)

pip install nose

Installing virtualenv (root)

pip install virtualenv

Installing distribute (root)

pip install distribute

NOTE: Sometimes the Python installer does not add the C:\Python27\Script to the system PATH. If this is the case for you, go back and add this to the path just like you did for C:\Python27, with [Environment]::SetEnvironmentVariable(“Path”, “$env:Path;C:\Python27\Scripts”, “User”)

Creating the Skeleton Project Directory

First, create the structure of your skeleton directory like this:

setup.py
NAME/
            __init__.py
bin/
docs/
tests/
           NAME_tests.py
           __init__.py

Edit setup.py with the following content.

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

config = [
        'description' : 'My Project',
        'author' : 'My Name',
        'url' : 'URL to get it at.',
        'download_url' : 'Where to download it.',
        'author_email' : 'My email.',
        'version' : '0.1',
        'install_requires' : ['nose'],
        'packages' : ['NAME'],
        'scripts' : [],
        'name' : 'projectname'
        ]

setup(**config)

Edit tests/NAME_tests.py with the following content.

from nose.tools import *
import NAME

def setup():
    print "SETUP!"

def teardown():
    print "TEAR DOWN!"

def test_basic():
    print "I RAN!"

And, from now on, you should run your commands that work with this directory from this point.

Testing Your Setup

After you get all that installed, you should be able to do this nosetests:

And here is the output:

.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK

Using the Skeleton

You are now done with most of your yak shaving. Whenever you want to start a new project, just do this:

  1. Make a copy of your skeleton directory. Name it after your new project.
  2. Rename (move) the NAME module to be the name of your project or whatever you want to call your root module.
  3. Edit your setup.py to have all the information for your project.
  4. Rename tests/NAME_tests.py to also have your module name.
  5. Double check it’s all working by using nosetests again.
  6. Start coding.

Required Quiz

This exercise doesn’t have Study Drills but a quiz you should complete:

  1. Read about how to use all the things you installed.
  2. Read about the setup.py file and all it has to offer. Warning: it is not a very well- written piece of software, so it will be very strange to use.
  3. Make a project and start putting code into the module, then get the module working.
  4. Put a script in the bin directory that you can run. Read about how you can make a Python script that’s runnable for your system.
  5. Mention the bin script you created in your setup.py so that it gets installed.
  6. Use your setup.py to install your own module and make sure it works, then use pip to uninstall it.

Why do we need a bin/ folder at all?

This is just a standard place to put scripts that are run on the command line, not a place to put modules.

发表评论

电子邮件地址不会被公开。 必填项已用*标注

14 − 8 =

+ 69 = 77