Creating a Python Package, the Cruel Way

This is how you can create a package of Python code so that you can use the module in various projects.

Let’s assume this is the code your module has:

STUFF_VERSION = "1.1-dev"

def get_stuff():
    return "This is stuff. Use it wisely."

Of course the module can be much more complicated with various functions and classes and whatnot, this is just a short example.

Let’s then create the directory structure for the package. First, start with a working directory dedicated to this package, like stuff. There, create a directory that actually contains the code of your module. Here, that directory is also called stuff. And, in that directory, create file that contains the code above.

So we have these:

\------ stuff

Again, contains the code with variable STUFF_VERSION and function get_stuff().

Now, in the working directory, create with this content:

from setuptools import setup, find_packages

        description="The Stuff package by Me",

Thus, we now have:

\------ stuff

That’s all the files we need in this example.

Now, I can install the package for me to use in all my Python projects with pip. In the working directory stuff (the upper one) I can run:

markku@server:~/stuff$ pip3 install .
Processing /home/markku/stuff
Building wheels for collected packages: stuff
  Running bdist_wheel for stuff ... done
  Stored in directory: /tmp/pip-ephem-wheel-cache-bmviu4af/wheels/4a/34/43/8df98ce9cd0b73a3eb668ac0136e2d4bdca9f63065a2830fa9
Successfully built stuff
Installing collected packages: stuff
Successfully installed stuff-1.1.dev0

I’m using a Debian testing install for this run. Let’s then test with another code in ~/test/

import stuff

print("Stuff version {} is saying: '{}'".format(stuff.STUFF_VERSION, stuff.get_stuff()))

Testing it:

markku@server:~/test$ python3
Stuff version 1.1-dev is saying: 'This is stuff. Use it wisely.'

So, it’s working, but what happened? Pip installed the code there:

markku@server:~/test$ cat ~/.local/lib/python3.7/site-packages/stuff/
STUFF_VERSION = "1.1-dev"

def get_stuff():
     return "This is stuff. Use it wisely."

That’s in a user-specific directory structure that Python searches when importing modules in Note that I didn’t use sudo to run the pip3 install . command, so it was not going anywhere system-wide. (Excercise: run sudo pip3 install . in the stuff directory, and use for example find /usr -name stuff to find the location of the package.)

This is simple way to reuse a module if the code is in a git repository and you can clone the repo on the server you need it. Just clone the repo and run pip3 install . and the module can be used in other projects on that same server.

More sophisticated way involves using source distributions or built distributions. See for more information, and test with python3 sdist or similar commands. If using git, prepare to edit your .gitignore to filter out some of the files that these operations generate in the directory structure.

Updated: May 15, 2019 — 20:58

Leave a Reply