We’ll start by learning how to make a small project installable with pip. For a simple one-module project, the minimal configuration is small. I don’t recommend you make it quite this small, but I want to show a minimal structure in order to build up to something more maintainable, and also to show how simple setup.py can be. Here’s a simple directory structure:
| some_module_proj/ |
| ├── setup.py |
| └── some_module.py |
The code we want to share is in some_module.py:
| def some_func(): |
| return 42 |
To make it installable with pip, we need a setup.py file. This is about as bare bones as you can get:
| from setuptools import setup |
| |
| setup( |
| name='some_module', |
| py_modules=['some_module'] |
| ) |
One directory with one module and a setup.py file is enough to make it installable via pip:
| $ cd /path/to/code/appendices/packaging |
| $ pip install ./some_module_proj |
| Processing ./some_module_proj |
| Installing collected packages: some-module |
| Running setup.py install for some-module ... done |
| Successfully installed some-module-0.0.0 |
And we can now use some_module from Python (or from a test):
| $ python |
| Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04) |
| [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin |
| Type "help", "copyright", "credits" or "license" for more information. |
| >>> from some_module import some_func |
| >>> some_func() |
| 42 |
| >>> exit() |
That’s a minimal setup, but it’s not realistic. If you’re sharing code, odds are you are sharing a package. The next section builds on this to write a setup.py file for a package.