A Simple Guide for Python Packaging

Jie Feng
Little Big Engineering
3 min readFeb 19, 2017

--

Code reuse is a very common need. It saves you time for writing the same code multiple times, enables leveraging other smart people’s work to make new things happen. Even just for one project, it helps organize code in a modular way so you can maintain each part separately. When it comes to python, it means format your project so it can be easily packaged. This is a simple instruction on how to go from nothing to a package that you can proudly put it in your portfolio to be used by other people.

Package Structure

When you create a ‘.py’ file, that is a module. You can define classes, functions, variables in that module named as the filename. When you put one or more modules in a folder and add a ‘__init__.py’ file, you create a package named as the folder. So a common package folder structure could be like this:

Example package structure from official python document.

Build a Package

Now that you have put your code in a package structure, let’s take a look at how to build a package. To do that, the most important thing is the setup configure file ‘setup.py’. You use that to specify metadata. Here is an example of the file:

from setuptools import setup, find_packages

setup(name='funniest',
version='0.1',
description='The funniest joke in the world',
long_description='Really, the funniest around.'…

--

--