How to distribute Python package via pip (Ft. Twine)
Motivation
This guide provides steps for packaging your Python code and installing it via
pip
.
The tutorial should not take more than 5 minutes to follow.
Folder structure
Begin by creating the following folder structure:
├── src/
│ └── bobtwine/
│ ├── __init__.py (package initialization file)
│ └── example.py (example module within the package)
│── setup.py
├── main.py (main script to run your package functionality, if needed)
└── README.md (README file with project info and documentation)
Key Python files
Below are the contents for each of the Python files generated.
setup.py
:
from setuptools import setup, find_packages
setup(
name='bobtwine',
version='0.1',
package_dir={'': 'src'},
packages=find_packages(where='src'),
install_requires=[], # List your dependencies here
)
example.py
:
def square(number):
return number**2
main.py
:
from bobtwine import example
value = example.square(2)
print(value) # 4
Distribution process
Install twine
. twine
helps upload files to PyPI.
PyPI is an online repository for packages. pip communicates with PyPI to download packages.
# Install twine
pip install twine
# Generate distribution files for your package
python setup.py sdist bdist_wheel
# Upload to PyPI
twine upload dist/*
Twine will prompt you for your API key. Register and generate an API key at https://pypi.org/manage/account/ shown below.
Your Python package is available at https://pypi.org/project/bobtwine/.
Downloading the package
In main.py
, we will try to import bobtwine
.
# Install bobtwine
pip install bobtwine==0.1
# Run main.py
python main.py # This prints 4
Now, if you want to simply make changes your bobtwine
locally and update the function called in main.py
without the need to re-upload and download from PyPI:
# To install your package locally and make it editable
pip install -e .
To uninstall
pip uninstall bobtwine