• Stars
    star
    1,234
  • Rank 38,045 (Top 0.8 %)
  • Language
    Python
  • License
    BSD 2-Clause "Sim...
  • Created over 10 years ago
  • Updated 5 months ago

Reviews

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

Repository Details

Enhanced cookiecutter template for Python libraries.

cookiecutter-pylibrary

Cookiecutter template for a Python library.

Notes:

Features

This is an "all inclusive" sort of template.

  • Choice of various licenses.
  • Tox for managing test environments for Python 2.7, 3.7+, PyPy etc.
  • Pytest or Nose for testing Python 2.7, 3.7+, PyPy etc.
  • Optional support for creating a tests matrix out of dependencies and python versions.
  • Travis-CI and AppVeyor for continuous testing.
  • Coveralls or Codecov for coverage tracking (using Tox).
  • Documentation with Sphinx, ready for ReadTheDocs.
  • Configurations for:
  • Support for C extensions (including coverage measurement for the C code). See c_extension_support.
  • Packaging and code quality checks. This template comes with a tox environment (check) that will:
    • Check if your README.rst is valid.
    • Check if the MANIFEST.in has any issues.

Requirements

Projects using this template have these minimal dependencies:

  • Cookiecutter - just for creating the project
  • Tox - for running the tests
  • Setuptools - for building the package, wheels etc. Now-days Setuptools is widely available, it shouldn't pose a problem :)

To get quickly started on a new system, just install setuptools and then install pip. That's the bare minimum to required install Tox and Cookiecutter. To install them, just run this in your shell or command prompt:

pip install tox cookiecutter

Usage and options

This template is more involved than the regular cookiecutter-pypackage.

First generate your project:

cookiecutter gh:ionelmc/cookiecutter-pylibrary

You will be asked for these fields:

Note

Fields that work together usually use the same prefix. If you answer "no" on the first one then the rest won't have any effect so just ignore them. Maybe in the future cookiecutter will allow option hiding or something like a wizard.

Field Default Description
full_name
"Ionel Cristian Maries"

Main author of this library or application (used in AUTHORS.rst and setup.py).

Can be set in your ~/.cookiecutterrc config file.

email

Contact email of the author (used in AUTHORS.rst and setup.py).

Can be set in your ~/.cookiecutterrc config file.

website
"https://blog.ionelmc.ro"

Website of the author (used in AUTHORS.rst).

Can be set in your ~/.cookiecutterrc config file.

repo_username
"ionelmc"

GitHub user name of this project (used for GitHub link).

Can be set in your ~/.cookiecutterrc config file.

project_name
"Nameless"
Verbose project name, used in headings (docs, readme, etc).
repo_hosting_domain
"github.com"
Use "no" for no hosting (various links will disappear). You can also use "gitlab.com" and such but various things will be broken (like Travis configuration).
repo_name
"python-nameless"
Repository name on GitHub (and project's root directory name).
package_name
"nameless"
Python package name (whatever you would import).
distribution_name
"nameless"
PyPI distribution name (what you would pip install).
project_short_description
"An example package [...]"
One line description of the project (used in README.rst and setup.py).
release_date
"today"
Release date of the project (ISO 8601 format) default to today (used in CHANGELOG.rst).
year
"now"
Copyright year (used in Sphinx conf.py).
version
"0.1.0"
Release version (see .bumpversion.cfg and in Sphinx conf.py).
c_extension_support
"no"

Support C extensions (will slightly change the outputted setup.py). Available options:

  • "yes" - to generate a Python C extension
  • "cffi" - to generate CFFI bindings against a C library
  • "cython" - to generate a Cython extension
c_extension_optional
"no"
Make C extensions optional (will allow your package to install even if extensions can't be compiled)
test_matrix_separate_coverage
"no"
Enable this to have a separate env for measuring coverage. Indicated if you want to run doctests or collect tests from src with pytest.
setup_py_uses_setuptools_scm
"no"
Enables the use of setuptools-scm. You can continue using bumpversion with this enabled.
allow_tests_inside_package
"no"

Collect tests that are inside the package (in other works, tests that are installed with the package).

The outside of package tests directory will still exist and be collected.

command_line_interface
"plain"

Option to enable a CLI (a bin/executable file). Available options:

  • plain - a very simple command.
  • argparse - a command implemented with argparse.
  • click - a command implemented with click - which you can use to build more complex commands.
  • no - no CLI at all.
command_line_interface_bin_name
"nameless"
Name of the CLI bin/executable file (set the console script name in setup.py).
license
"BSD license"

License to use. Available options:

  • BSD license
  • MIT license
  • ISC license
  • Apache Software License 2.0

What license to pick? https://choosealicense.com/

coveralls
"no"
Enable pushing coverage data to Coveralls and add badge in README.rst.
codecov
"yes"

Enable pushing coverage data to Codecov and add badge in README.rst.

Note: Doesn't support pushing C extension coverage yet.

scrutinizer
"no"
Add a Scrutinizer badge in README.rst.
codacy
"no"

Add a Codacy badge in README.rst.

Note: After importing the project in Codacy, find the hexadecimal project ID from settings and replace it in badge URL

codeclimate
"no"
Add a CodeClimate badge in README.rst.
sphinx_docs
"yes"
Have Sphinx documentation.
sphinx_theme
"sphinx-rtd-theme"

What Sphinx theme to use.

Suggested alternative: sphinx-py3doc-enhanced-theme for a responsive theme based on the Python 3 documentation.

sphinx_doctest
"no"

Set to "yes" if you want to enable doctesting in the docs environment. Works best with test_matrix_separate_coverage == 'no'.

Read more about doctest support in Sphinx.

sphinx_docs_hosting
"repo_name.readthedocs.io"
Leave as default if your documentation will be hosted on readthedocs. If your documentation will be hosted elsewhere (such as GitHub Pages or GitLab Pages), enter the top-level URL.
pypi_badge
"yes"
By default, this will insert links to your project's page on PyPI.org. Note that if your package is not (yet) on PyPI, this will cause tox -e docs to fail. If you choose "no", then these links will not be created.
pypi_disable_upload
"no"
If you specifically want to be sure your package will never be accidentally uploaded to PyPI, you can pick "yes".

Developing the project

To run all the tests, just run:

tox

To see all the tox environments:

tox -l

To only build the docs:

tox -e docs

To build and verify that the built package is proper and other code QA checks:

tox -e check

Releasing the project

Before releasing your package on PyPI you should have all the tox environments passing.

Version management

This template provides a basic bumpversion configuration. It's as simple as running:

  • bumpversion patch to increase version from 1.0.0 to 1.0.1.
  • bumpversion minor to increase version from 1.0.0 to 1.1.0.
  • bumpversion major to increase version from 1.0.0 to 2.0.0.

You should read Semantic Versioning 2.0.0 before bumping versions.

Building and uploading

Before building dists make sure you got a clean build area:

rm -rf build
rm -rf src/*.egg-info

Note:

Dirty build or egg-info dirs can cause problems: missing or stale files in the resulting dist or strange and confusing errors. Avoid having them around.

Then you should check that you got no packaging issues:

tox -e check

And then you can build the sdist, and if possible, the bdist_wheel too:

python setup.py clean --all sdist bdist_wheel

To make a release of the project on PyPI, assuming you got some distributions in dist/, the most simple usage is:

twine upload --skip-existing dist/*.whl dist/*.gz dist/*.zip

In ZSH you can use this to upload everything in dist/ that ain't a linux-specific wheel (you may need setopt extended_glob):

twine upload --skip-existing dist/*.(whl|gz|zip)~dist/*linux*.whl

For making and uploading manylinux1 wheels you can use this contraption:

docker run --rm -itv $(pwd):/code quay.io/pypa/manylinux1_x86_64 bash -c 'set -eux; cd code; rm -rf wheelhouse; for variant in /opt/python/*; do rm -rf dist build *.egg-info && $variant/bin/python setup.py clean --all bdist_wheel; auditwheel repair dist/*.whl; done; rm -rf dist build *.egg-info'
twine upload --skip-existing wheelhouse/*.whl
docker run --rm -itv $(pwd):/code quay.io/pypa/manylinux1_i686 bash -c 'set -eux; cd code; rm -rf wheelhouse; for variant in /opt/python/*; do rm -rf dist build *.egg-info && $variant/bin/python setup.py clean --all bdist_wheel; auditwheel repair dist/*.whl; done; rm -rf dist build *.egg-info'
twine upload --skip-existing wheelhouse/*.whl

Note:

twine is a tool that you can use to securely upload your releases to PyPI. You can still use the old python setup.py register sdist bdist_wheel upload but it's not very secure - your PyPI password will be sent over plaintext.

Changelog

See CHANGELOG.rst.

Questions & answers

There's no Makefile?

Sorry, no Makefile yet. The Tox environments stand for whatever you'd have in a Makefile.

Why does tox.ini have a passenv = *?

Tox 2.0 changes the way it runs subprocesses - it no longer passes all the environment variables by default. This causes all sorts of problems if you want to run/use any of these with Tox: SSH Agents, Browsers (for Selenium), Appengine SDK, VC Compiler and so on.

cookiecutter-pylibrary errs on the side of convenience here. You can always remove passenv = * if you like the strictness.

Why is the version stored in several files (pkg/__init__.py, setup.py, docs/conf.py)?

We cannot use a metadata/version file [1] because this template is to be used with both distributions of packages (dirs with __init__.py) and modules (simple .py files that go straight in site-packages). There's no good place for that extra file if you're distributing modules.

But this isn't so bad - bumpversion manages the version string quite neatly.

[1]Example, an __about__.py file.

Not Exactly What You Want?

No way, this is the best. 😜

If you have criticism or suggestions please open up an Issue or Pull Request.

More Repositories

1

pytest-benchmark

py.test fixture for benchmarking code
Python
1,159
star
2

python-hunter

Hunter is a flexible code tracing toolkit.
Python
769
star
3

python-redis-lock

Lock context manager implemented via redis SET NX EX and BLPOP.
Python
514
star
4

python-manhole

Debugging manhole for python applications.
Python
356
star
5

django-redisboard

Redis monitoring and inspection tool in django admin.
Python
265
star
6

python-remote-pdb

Remote vanilla PDB (over TCP sockets).
Python
252
star
7

python-lazy-object-proxy

A fast and thorough lazy object proxy.
Python
234
star
8

django-prefetch

Generic model related data prefetch framework for Django.
Python
153
star
9

python-tblib

Serialization library for Exceptions and Tracebacks.
Python
149
star
10

python-nameless

Sample project. Use https://github.com/ionelmc/cookiecutter-pylibrary to make your own project. The purpose of this repo is to test the CI configuration.
Python
145
star
11

python-fields

A totally different take on container boilerplate.
Python
137
star
12

jquery-gp-gallery

jQuery gallery plugin (ala google plus photo galeries)
CSS
126
star
13

python-aspectlib

An aspect-oriented programming, monkey-patch and decorators library. It is useful when changing behavior in existing code is desired. It includes tools for debugging and testing: simple mock/record and a complete capture/replay framework.
Python
108
star
14

django-monkey-team

Django middleware and userscript that displays debug tracebacks on production sites (where you would have DEBUG = False) only to developers.
Python
57
star
15

django-uwsgi-cache

uWSGI Django cache backend.
Python
38
star
16

django-admin-customizer

Django admin customizing interface
Python
36
star
17

python-holdup

A tool to wait for services and execute command. Useful in Docker containers.
Python
33
star
18

projectskel

Project skeleton for python 2.7 projects with fabric and virtualenv. It's intended for django projects but can be customized for other types of projects.
Python
28
star
19

sphinx-py3doc-enhanced-theme

A theme based on the theme of https://docs.python.org/3/ with some responsive enhancements.
JavaScript
25
star
20

django-admin-utils

Utility code for easier django admin development
Python
25
star
21

tox-wheel

A Tox plugin that builds and installs wheels instead of sdist. Note that this plugin is obsolte as tox 4.0 already has wheel support.
Python
23
star
22

docker-webdav

NGINX WebDAV container
Shell
23
star
23

python-cookiepatcher

Just a small shim around cookiecutter that alters a bit the CLI to work better when reapplying templates to existing projects.
Python
18
star
24

nose-htmloutput

Python
14
star
25

nose-timelimit

Nose plugin that allows you automatically skip tests that are too slow.
Python
13
star
26

cookiecutter-pylibrary-minimal

This has been merged into https://github.com/ionelmc/cookiecutter-pylibrary - use that instead!
Python
12
star
27

pypi-alias

A small utility to make alias distributions on PyPI.
Python
11
star
28

python-su

Python
9
star
29

python-process-tests

Testcase classes and assertions for testing processes.
Python
9
star
30

python-packaging-blunders

Python
8
star
31

polymer-select-box

Tagging widget implemented as a Polymer webcomponent
HTML
7
star
32

django-badbrowser

Browser detection (including browser upgrade notices) for Django
Python
7
star
33

python-mongoql-conv

Library to convert those MongoDB queries to something else, like a python expresion, a function or a django query (Q) object tree
Python
7
star
34

python-signalfd

CFFI bindings for signalfd.
Python
6
star
35

python-appengine-sdk

Un-official `pip install`-able AppEngine SDK.
Python
6
star
36

python-redis-throttled-queue

WIP
Python
6
star
37

python-unlzw

Python
6
star
38

python-cogen

Automatically exported from https://code.google.com/p/cogen
Python
6
star
39

python-stampede

Event-loop based, miniature job queue and worker that runs the task in a subprocess (via fork).
Python
6
star
40

django-easyfilters

Fork of https://bitbucket.org/evildmp/django-easyfilters/
Python
5
star
41

pytest-cover

Merged into https://github.com/schlamar/pytest-cov - use that instead!
Python
5
star
42

javascript-userscripts

Automatically exported from code.google.com/p/webmonkey-userscripts
JavaScript
4
star
43

python-mongosizeof

Python
4
star
44

pylint-fields

Pylint plugin for python-fields
Python
4
star
45

python-pygaljs

Python package providing assets from https://github.com/Kozea/pygal.js
Python
4
star
46

python-tax

2021 update: use tox-direct instead. This was a variant of Tox that didn't use virtualenvs at all - just installed everything in the current environment.
Python
4
star
47

django-secdownload-storage

Django storage backend that can be used to serve files via lighttpd's mod_secdownload module.
Python
4
star
48

docker-in-docker

An actually usable DIND. Includes a bunch of debug tools and docker-compose.
3
star
49

django-customfields

Couple of custom model fields for django: CachedManyToManyField and InheritedField
Python
3
star
50

python-pth

Simple and brief path traversal and filesystem access library.
Python
3
star
51

python-nameless-minimal

Python
3
star
52

python-ftpd-example

Python
2
star
53

dotfiles

My zsh setup
Shell
2
star
54

docker-manylinux

https://hub.docker.com/r/ionelmc/manylinux
Shell
2
star
55

python-matrix

Python
2
star
56

docker-buildpack-deps

Just buildpack-deps with some extras
Dockerfile
2
star
57

polymer-query-box

Query editor widget implemented as a Polymer webcomponent
JavaScript
2
star
58

polymer-json-box

Simple json edit widget implemented as a Polymer webcomponent
JavaScript
2
star
59

docker-fakebuntu

Ubuntu Xenial image running minimal services: systemd, journald, sshd, dind (docker in docker)
C
2
star
60

python-css-sprite

Python
1
star
61

django-image-editor

Allows to edit images in the browser
JavaScript
1
star
62

ppa-socat

Shell
1
star
63

pytest-benchmark-elasticsearch

Elasticseach storage backend for pytest-benchmark.
Python
1
star
64

t.ionelmc.ro

Google analytics to __utm.gif redirector.
Python
1
star
65

dockerskel

Abandoned. Check out https://github.com/evozon/django-docker
Shell
1
star