Programming is fun! But it happens so fast that the code gets messy, and I’ll spend a lot of time tidying up everything. Do you know this problem? It’s easy to code but hard to write good code (and I’m really far from writing good code). If you want to make your code more sustainable and reuse it easily, you could try writing your own Python packages. I will show you the basics of how to get started 🚀
First step: Set up your Folder Structure
I assume you have already created a nice Python script with some functions. In the first step, you have to create the right folder structure for your package. Then, you have to find a name for the package – which can be pretty hard. Let’s assume your package has the excellent name ‘nekotron’ 🐈 and include two modules ‘cat.py’ and ‘squirrel.py’.
- Create a Folder with the name ‘nekotron-home’. This is the folder where everything related to the package goes inside.
- In the ‘nekotron-home’-folder create another folder with the name of your package ‘nekotron’.
- In the ‘nekotron’-folder, you have to put your Python script (one, or two, or more ^^).
- In the ‘nekotron’-folder should be as well a python script called ‘__init__.py’ (here the name must be really ‘__init__.py’).
- The ‘__init__.py’ script will contain all imports of your modules.
- In the folder ‘nekotron-home’, you have to create the script for the setup. You can call it ‘setup.py’.
If you are done, your folder should be structured like this:

Second step: Set up your __init__.py file
In the __init__.py file you have to import all modules 🐈🐿️
from nekotron.cat import pet_cat
from nekotron.cat import feed_cat
from nekotron.squirrel import find_nuts
from nekotron.squirrel import climb_on_tree
Third step: Set up your setup.py file
In the setup.py file, some information about your package has to be included. You can use the following code as a template:
from setuptools import setup, find_packages
VERSION = '0.0.1'
DESCRIPTION = 'Python package with animal functions'
LONG_DESCRIPTION = 'Python package to create a cat and a squirrel. Includes functions with activities for the animals'
setup(
name="nekotron", # has to be the same as the folder name!
version=VERSION,
author="Your Name",
author_email="<my.email@mail.com>",
description=DESCRIPTION,
long_description=LONG_DESCRIPTION,
packages=find_packages(),
install_requires=['numpy', 'xarray'], # add all
# packages that you use in your module (here, I added as
# an example numpy and xarray - you should adapt this)
keywords=['python'],
classifiers= [
"Development Status :: 3 - Alpha",
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
]
)
Last steps 🐾
If you have set up the __init__.py and the setup.py file, go into the ‘nekotron-home’ folder where the setup.py file is located and execute in the terminal the following:
python setup.py sdist bdist_wheel
The very last step is to install your module lokal on your PC. If you are in the ‘nekotron-home’-folder, you can pip-install the package by typing the following:
pip install ./
🎉 Congrats! You are done. You can now import your package and re-use all your nice functions in any Python script.
Leave a comment