• Stars
    star
    542
  • Rank 81,982 (Top 2 %)
  • Language Smarty
  • License
    Other
  • Created over 12 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A template Python project with a focus on best practices.

Python Project Template

https://travis-ci.org/seanfisk/python-project-template.png

This project provides a best-practices template Python project which integrates several different tools. It saves you work by setting up a number of things, including documentation, code checking, and unit test runners.

As it is an all-in-one solution, the tools used are rather opinionated. They include:

  • Paver for running miscellaneous tasks
  • Setuptools for distribution (Setuptools and Distribute have merged)
  • Sphinx for documentation
  • flake8 for source code checking
  • pytest for unit testing
  • mock for mocking (not required by the template, but included anyway)
  • tox for testing on multiple Python versions

If you are new to Python or new to creating Python projects, see Kenneth Reitz's Hitchhiker's Guide to Python for an explanation of some of the tools used here.

Project Setup

This will be the README for your project. For now, follow these instructions to get this project template set up correctly. Then, come back and replace the contents of this README with contents specific to your project.

Instructions

  1. Clone the template project, replacing my-project with the name of the project you are creating:

    git clone https://github.com/seanfisk/python-project-template.git my-project
    cd my-project
    
  2. Edit the metadata file my_module/metadata.py to correctly describe your project.

  3. Generate files based upon the project metadata you just entered:

    python internal/generate.py
    

    The generation script will remove all the template files and generate real files, then self-destruct upon completion.

  4. Delete the old git history and optionally re-initialize the repository:

    rm -rf .git # or `ri -recurse -force .git' for PowerShell
    git init
    
  5. Change the license in setup.py and replace the generated LICENSE file with the one of your choice. If you would like to use the MIT license, no change is necessary.

  6. Change the classifiers keyword in setup.py. This will require modification.

  7. Replace this README with your own text.

  8. (Optional, but good practice) Create a new virtual environment for your project:

    With pyenv and pyenv-virtualenv:

    pyenv virtualenv my-project
    pyenv local my-project
    

    With virtualenvwrapper:

    mkvirtualenv my-project
    

    With plain virtualenv:

    virtualenv /path/to/my-project-venv
    source /path/to/my-project-venv/bin/activate
    

    If you are new to virtual environments, please see the Virtual Environment section of Kenneth Reitz's Python Guide.

  9. Install the project's development and runtime requirements:

    pip install -r requirements-dev.txt
    
  10. Install argparse package when developing for Python 2.6:

    pip install argparse
    
  11. Run the tests:

    paver test_all
    

    You should see output similar to this:

    $ paver test_all
    ---> pavement.test_all
    No style errors
    ========================================= test session starts =========================================
    platform darwin -- Python 2.7.3 -- pytest-2.3.4
    collected 5 items
    
    tests/test_main.py .....
    
    ====================================== 5 passed in 0.05 seconds =======================================
      ___  _   ___ ___ ___ ___
     | _ \/_\ / __/ __| __|   \
     |  _/ _ \\__ \__ \ _|| |) |
     |_|/_/ \_\___/___/___|___/
    

    The substitution performed is rather naive, so some style errors may be reported if the description or name cause lines to be too long. Correct these manually before moving to the next step. If any unit tests fail to pass, please report an issue.

Project setup is now complete!

Using Paver

The pavement.py file comes with a number of tasks already set up for you. You can see a full list by typing paver help in the project root directory. The following are included:

Tasks from pavement:
lint             - Perform PEP8 style check, run PyFlakes, and run McCabe complexity metrics on the code.
doc_open         - Build the HTML docs and open them in a web browser.
coverage         - Run tests and show test coverage report.
doc_watch        - Watch for changes in the Sphinx documentation and rebuild when changed.
test             - Run the unit tests.
get_tasks        - Get all paver-defined tasks.
commit           - Commit only if all the tests pass.
test_all         - Perform a style check and run all unit tests.

For example, to run the both the unit tests and lint, run the following in the project root directory:

paver test_all

To build the HTML documentation, then open it in a web browser:

paver doc_open

Using Tox

Tox is a tool for running your tests on all supported Python versions. Running it via tox from the project root directory calls paver test_all behind the scenes for each Python version, and does an additional test run to ensure documentation generation works flawlessly. You can customize the list of supported and thus tested Python versions in the tox.ini file.

Pip requirements[-dev].txt files vs. Setuptools install_requires Keyword

The difference in use case between these two mechanisms can be very confusing. The pip requirements files is the conventionally-named requirements.txt that sits in the root directory of many repositories, including this one. The Setuptools install_requires keyword is the list of dependencies declared in setup.py that is automatically installed by pip or easy_install when a package is installed. They have similar but distinct purposes:

install_requires keyword
Install runtime dependencies for the package. This list is meant to exclude versions of dependent packages that do not work with this Python package. This is intended to be run automatically by pip or easy_install.
pip requirements file
Install runtime and/or development dependencies for the package. Replicate an environment by specifying exact versions of packages that are confirmed to work together. The goal is to ensure repeatability and provide developers with an identical development environment. This is intended to be run manually by the developer using pip install -r requirements-dev.txt.

For more information, see the answer provided by Ian Bicking (author of pip) to this StackOverflow question.

Supported Python Versions

Python Project Template supports the following versions out of the box:

  • CPython 2.6, 2.7, 3.3
  • PyPy 1.9

CPython 3.0-3.2 may also work but are at this point unsupported. PyPy 2.0.2 is known to work but is not run on Travis-CI.

Jython and IronPython may also work, but have not been tested. If there is interest in support for these alternative implementations, please open a feature request!

Licenses

The code which makes up this Python project template is licensed under the MIT/X11 license. Feel free to use it in your free software/open-source or proprietary projects.

The template also uses a number of other pieces of software, whose licenses are listed here for convenience. It is your responsibility to ensure that these licenses are up-to-date for the version of each tool you are using.

Project License
Python itself Python Software Foundation License
argparse (now in stdlib) Python Software Foundation License
Sphinx Simplified BSD License
Paver Modified BSD License
colorama Modified BSD License
flake8 MIT/X11 License
mock Modified BSD License
pytest MIT/X11 License
tox MIT/X11 License

Issues

Please report any bugs or requests that you have using the GitHub issue tracker!

Development

If you wish to contribute, first make your changes. Then run the following from the project root directory:

source internal/test.sh

This will copy the template directory to a temporary directory, run the generation, then run tox. Any arguments passed will go directly to the tox command line, e.g.:

source internal/test.sh -e py27

This command line would just test Python 2.7.

Authors

  • Sean Fisk
  • Benjamin Schwarze

More Repositories

1

diablo2-autohotkey-lib

AutoHotkey macro library for Diablo II: Lord of Destruction
AutoHotkey
13
star
2

juniper-network-connect-vpn-applescript

Automated script for connecting to the VPN.
AppleScript
12
star
3

cmake-flymake

Scripts for using Emacs Flymake with the CMake build system, based on the original scripts from <http://wiki.opencog.org/w/Flymake_help>.
Shell
9
star
4

c-hash-map

Practice implementation of hash map in C.
C
9
star
5

mastering-eos

New manual for GVSU EOS labs.
Python
8
star
6

latex-template

My LaTeX template
TeX
6
star
7

cpg-islands-hmm

Finding CpG Islands using Hidden Markov Models.
Python
4
star
8

personal-chef-repo

Chef repository for my computing environment
Ruby
4
star
9

pyside-async-strategies

Strategies for asynchronous networking in PySide.
Python
4
star
10

dotfiles

Configuration files for my shells and other software
Python
4
star
11

emacs-prelude

My Emacs configuration based on Emacs Prelude
Emacs Lisp
3
star
12

diablo2-macros

Personal AutoHotkey macros for Diablo II: Lord of Destruction
PHP
2
star
13

flex-mode

An attempt at writing an Emacs mode for Flex, the fast lexical analyzer.
C
2
star
14

cpg-islands

Finding CpG islands in genetic sequences.
Python
2
star
15

local-install-scripts

Scripts for compiling and installing software from source.
Shell
2
star
16

rpython-stencil-language

A simple experimental programming language implementing finite difference using stencils.
Python
2
star
17

pyside-docs

Script which generates the latest PySide API documentation.
Shell
2
star
18

caesar-cipher-presenter-first

A Presenter First re-implementation of my Caesar cipher program.
Python
2
star
19

open-chord-chat-demo

A simple chat application written using the Open Chord distributed hash table.
Java
2
star
20

combootcha

Set up my personal computing devices
Rust
2
star
21

buzzword-bingo-server

Buzzword Bingo REST API server written in Django for Google App Engine 2.7 runtime.
Python
1
star
22

gvsu-den-ga-fall2012

My graduate assistanship proposal for the DEN at GVSU.
1
star
23

homebrew-dmgbuild

Homebrew tap for dmgbuild
Ruby
1
star
24

c-lib-skeleton

Basic code for creating a program that spans multiple files.
C
1
star
25

rct2-game-objects

Custom game objects for RollerCoaster Tycoon 2.
PowerShell
1
star
26

latest

Find your latest files, at the speed of light!
C
1
star
27

guitar-tabs

Guitar tabs written by Sean Fisk and friends
1
star
28

resume

My résumé!
TeX
1
star
29

buzzword-bingo-client

Client for CIS 658 Web Architectures buzzword bingo game
Python
1
star
30

caesar-cipher

Caesar cipher implementation with GUI
Python
1
star
31

database-serializability-graph

Generate database serializability graphs from operation schedule using Graphviz.
Python
1
star
32

gv-percussion-marching-fundamentals

Marching fundamentals packet for the drum line in which I participate.
1
star
33

pyside-program-config

Command-line argument parsing combined with the power of QSettings.
Python
1
star
34

gitconfig-parser

Parse for Gitconfig files
Python
1
star
35

rct2-saved-games

Collaborative saved games for RollerCoaster Tycoon 2.
PowerShell
1
star
36

user-defaults-rs

A Rust binding to macOS' NSUserDefaults
Rust
1
star
37

sea2015

Packaging Python Projects presentation for SEA 2015
TeX
1
star
38

python-kmer-graph-generator

Annotated source of a kmer graph generator in Python
Python
1
star