DEV Community

Fernando B 🚀
Fernando B 🚀

Posted on • Originally published at on

Part 2 A Basic Package

In the previous article we’ve talked about what packages are, and while that was a nice intro. I’d rather show you how to build one, we won’t get to crazy with it, just enough to show you the basics.

Barebones structure

In order to create a package we need a folder structure, for a single .py project a package is technically not needed py-modules doc. However most folks will probably have several packages in their project to keep things organized. (Note: In a later article I will talk about cookiecutter and templates)

├── awesome_package
│   ├──
│   └──

Enter fullscreen mode Exit fullscreen mode

Here’s the link for the above package structure


The / file is the most important file for the package. It contains all the information your package needs. We will discuss this in more detail in a later article. For now the below will suffice to get us up and running.

from setuptools import setup, find_packages

    packages=find_packages(exclude=['contrib', 'docs', 'tests*']),

Enter fullscreen mode Exit fullscreen mode

Let’s add some python code

We won’t get crazy we will add one function to our hello_world file

def say_hello(name):
    print("hello %s" % (name))

Enter fullscreen mode Exit fullscreen mode

Adding our function to init

from .hello_world import *

Enter fullscreen mode Exit fullscreen mode

We are importing all functions found in hello_world, in our case is only one. Ok, we are ready to test our package.

Install as development package

A few things to take note. Installing a package with the development argument allows us to make changes, without having to keep installing or updating through a pip --upgrade command. Which could get tedius if you are working on something that takes days or weeks, or longer.

Another thing worth mentioning is that if you make changes and you’ve already launched python in the terminal, you’ll have to exit(), and re-launch python. Don’t ask me how I know this.

Inside the package, install it by running the below command.

python3 develop

Enter fullscreen mode Exit fullscreen mode

Check to make sure the package installed.

pip list
my-tool 0.0.1 /Users/terminalX/git/A_Basic_Package

Enter fullscreen mode Exit fullscreen mode

Test our package

Launch python in terminal, and test our package as shown below. Too easy, mate. As the aussies say.

>>> from awesome_package import *
>>> say_hello("fer")
hello fer

Enter fullscreen mode Exit fullscreen mode

Install Package from Github

Now that we have a fully working package, we can uninstall the development install, and install straight from Github

pip3 uninstall my_tool
pip3 install git+

Enter fullscreen mode Exit fullscreen mode

Ensure you can still use the say_hello() function.

Tip: If you want to know which methods are available in a module:

>>> dir(hello_world)
[' __builtins__', ' __cached__', ' __doc__', ' __file__', ' __loader__', ' __name__', ' __package__', ' __spec__', 'say_hello']

Enter fullscreen mode Exit fullscreen mode

Next Article

Make sure you play around with the package, add more functions, more packages, and see what happens. Remember if you decide to play around some more with this package uninstall it, and install as explained in the development section.

In the next article we will create a more advanced example, while learning how to efficiently create the package with the help of other python tools. Thanks for reading.

Top comments (0)