This article is edited from Learn Pthon: The Hard Way 3rd Edition Exercise 46 A Project Skeleton.
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 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 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)
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
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:
- Make a copy of your skeleton directory. Name it after your new project.
- Rename (move) the NAME module to be the name of your project or whatever you want to call your root module.
- Edit your
setup.pyto have all the information for your project.
tests/NAME_tests.pyto also have your module name.
- Double check it’s all working by using nosetests again.
- Start coding.
This exercise doesn’t have Study Drills but a quiz you should complete:
- Read about how to use all the things you installed.
- Read about the
setup.pyfile 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.
- Make a project and start putting code into the module, then get the module working.
- Put a script in the
bindirectory that you can run. Read about how you can make a Python script that’s runnable for your system.
- Mention the bin script you created in your setup.py so that it gets installed.
- 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.