DEV Community

Cover image for Part 3 Another Package with Cookiecutter
Fernando B πŸš€
Fernando B πŸš€

Posted on • Originally published at kodaman2.github.io on

Part 3 Another Package with Cookiecutter

Cover Image Credit

Set your oven to 350F, or 175C in this post we will be baking a python package. This is the third installment in the series. In the previous post we've setup a basic package by manually creating the structure. In this post we will leverage a very cool package Cookiecutter. Cookiecutter can clone the directory of another repo or even a local directory. You'll have a python package ready in no time. There is a large database of templates too, not just for python.

Installing Cookiecutter

You can install cookiecutter with a pip command(Note: I am using pip3 because I also have py2 installed):

pip3 install cookiecutter
Enter fullscreen mode Exit fullscreen mode

Create Package

On my git directory I can then run:

cookiecutter https://github.com/audreyr/cookiecutter-pypackage
Enter fullscreen mode Exit fullscreen mode

Since github is the defacto for repositories you can also run:

cookiecutter gh:audreyr/cookiecutter-pypackage
Enter fullscreen mode Exit fullscreen mode

Note: Make sure to say 2 for cli
prompts

All set and done:

contents

In less than a minute a whole project was created with all necessary files for a python package.

Adding a Function to Test the Package

I use vscode, so inside the main package directory run:

code .
Enter fullscreen mode Exit fullscreen mode

We'll reuse our say_hello function to make sure the project works as expected:

# -*- coding: utf-8 -*-

"""Main module."""
def say_hello(name):
    print("hello %s" % (name))
Enter fullscreen mode Exit fullscreen mode

Installing the Package

Installing with dev argument same as before:

python3 setup.py develop
Enter fullscreen mode Exit fullscreen mode

Testing say_hello function

At this point we can test our function. Launch python3 in the terminal or launch idle.

>>> from another_package import *
>>> say_hello("fer")
Enter fullscreen mode Exit fullscreen mode

Won't run huh? We forgot to do one thing, and that is to add our python module to the init file. The reason it fails is that it doesn't know what functions to include in the package.

>>> from another_package import *
>>> say_hello("fer")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'say_hello' is not defined
Enter fullscreen mode Exit fullscreen mode

Adding a py module to init

Open _init_.py inside the package folder, and below the version insert the following (note the dot before the module name):

from .another_package import *
Enter fullscreen mode Exit fullscreen mode

Here we tell init to import all our functions.

Test 2

Exit python, and relaunch to reload another_package.

>>> from another_package import *
>>> say_hello("fer")
hello fer
Enter fullscreen mode Exit fullscreen mode

With this process in place you won't be forgetting adding a readme file or a license ever again.

A word on cookiecutter

If you have a project that needs certain structure you can do another template. I usually just do python utilities and use the python template. If you need a specific template that doesn't exist already, make sure to read the docs on how to create a template.

Next Article

We have a working package, but it doesn't have any functionality at the moment. In the next post we'll add some functionality to run like a script from bash or cmd prompt. We will also learn how to use arguments with the argparse module. More fun to come, stay tuned!

For example:

say_hello -n "fer" -l "balandran"
output: hello fer balandran
Enter fullscreen mode Exit fullscreen mode

Top comments (0)