Starting a new Python project using cookies

In this blog post, I talk about how to quickstart a new project in the popular Python programming language. (I use Python 3k and Ubuntu linux distribution)

I use the cookiecutter command line utility, which helps create project structures for many type of languages, including Python, C, C++, CommonLisp, etc. just to name a few.

Creating the project structure from the command line

I first install cookiecutter:

python3 -m pip install --user cookiecutter
# On ubuntu this works too:
# sudo apt install cookiecutter

Now that I have cookiecutter installed, I checkout the template project released by audreyr the original creator of cookiecutter.

git clone git@github.com:audreyr/cookiecutter-pypackage.git

Then I modify it a bit to reflect my wishes for the project I want to create. Here I only modified the project metadata contained in cookiecutter-package/cookiecutter.json

cat cookiecutter-package/cookiecutter.json
#{
#  "full_name": "Adrien Oyono",
#  "email": "adrienoyono@gmail.com",
#  "github_username": "aoyono",
#  "project_name": "Python Boilerplate",
#  "project_slug": "{{ cookiecutter.project_name.lower().replace(' ', '_').replace('-', '_') }}",
#  "project_short_description": "Python Boilerplate contains all the boilerplate you need to create a Python package.",
#  "pypi_username": "{{ cookiecutter.github_username }}",
#  "version": "0.1.0",
#  "use_pytest": "n",
#  "use_pypi_deployment_with_travis": "y",
#  "add_pyup_badge": "n",
#  "command_line_interface": ["Click", "No command-line interface"],
#  "create_author_file": "y",
#  "open_source_license": ["MIT license", "BSD license", "ISC license", "Apache Software License 2.0", "GNU General Public License v3", "Not open source"]
#}

Then I create my new project from this template:

cookiecutter cookiecutter-package/

I accept all the defaults as responses to the questions asked by the cli, as they are set to the values I put earlier in the metadata file.

A new directory named python_boilerplate is now present in the current directory and I have a ready to hack python package project repository

ls
# cookiecutter-package python_boilerplate
tree python_boilerplate
# python_boilerplate
# ├── AUTHORS.rst
# ├── CONTRIBUTING.rst
# ├── docs
# │   ├── authors.rst
# │   ├── conf.py
# │   ├── contributing.rst
# │   ├── history.rst
# │   ├── index.rst
# │   ├── installation.rst
# │   ├── make.bat
# │   ├── Makefile
# │   ├── readme.rst
# │   └── usage.rst
# ├── HISTORY.rst
# ├── LICENSE
# ├── Makefile
# ├── MANIFEST. in
# ├── python_boilerplate
# │   ├── cli.py
# │   ├── __init__.py
# │   └── python_boilerplate.py
# ├── README.rst
# ├── requirements_dev.txt
# ├── setup.cfg
# ├── setup.py
# ├── tests
# │   ├── __init__.py
# │   └── test_python_boilerplate.py
# └── tox.ini
# 
# 3
# directories, 26
# files

Note:

It would be way more easier if I could skip the checkout->modify-metadata phase above. It is possible to achieve just that with cookiecutter. The pre-requisite is to layout a project structure that I upload to a code hosting platform (Github), so that every time I need to start a new project, say, a Python package, I just instruct cookiecutter to check it out for me and create the new project. I would therefore answer the questions one by one to customize the project's metadata. It is straightforward:

cookiecutter gh:audreyr/cookiecutter-pypackage
# gh stands for github

When using this method, the git repository is checked out in ~/.cookiecutters/ by default so that you can manage the layouts on which your projects are based.

That's all folks ! Read the documentation to find out more.

comments powered by Disqus