Your first Python package in 1 min
1 min readJul 9, 2020
Create the directory for your package
Here is the very minimal directory you need to create a package.
my_package
├── my_package
│ └── __init__.py
└── setup.py
Let’s see what’s in each of the files.
__init__.py
It is the main file, where you’ll put all your functions, class, objects… Here is an example :
def my_function():
print("Hello world!")
And that’s it !
setup.py
This file is used for installation. Here is an minimal example:
from setuptools import setup, find_packages
setup(
name="my_package",
version="0.1",
packages=find_packages(),
)
find_packages() is used so that, during installation, all the packages necessary for the proper functioning of your package are also installed.
Let’s test it
Open a terminal, and go to the folder containing your package:
$ cd folder_that_contains_your_package
$ pip install my_package/
Processing ./my_package
Installing collected packages: my-package
Running setup.py install for my-package ... done
Successfully installed my-package-0.1
Your package is now installed. To use it with Python:
import my_package
my_package.my_function()