What is important for us here to know is that src/math.py contains the code which we will want to deploy and tests/test_math.py contains a simple test for our code.
If we look at the test file, we can see the code we want to deploy contains simple math functions:
You can run pipenv run pytest to confirm everything is as it should be.
Editing our setup.py file
We need to edit setup.py to use the setuptools package and contain information about our package:
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="demo_pip_math",
version="0.1.0",
author="Dennis O'Keeffe",
author_email="hello@dennisokeeffe.com",
description="Demo your first Pip package.",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/okeeffed/your-first-pip-package-in-python",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
keywords='pip-demo math',
project_urls={
'Homepage': 'https://github.com/okeeffed/your-first-pip-package-in-python',
},
)
Our setup.py makes use of the setuptools.setup function to prepare our package.
Some of the more important arguments we are passing:
Argument
Does
name
Name of the package
version
Current package version in semantic versioning
author
Your name
description
Your package description
long_description
A description that comes from our README.md file
python_requires
What versions of Python does your package work with
keywords
Words to match the package when searching for it
Add a license
You can choose a license to copy into our LICENSE file. I have gone with the MIT license:
Copyright (c) 2021 Dennis O'Keeffe
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Adding a README
I am adding a simple README for the sake of completing the package deployment. My README.md file reads as follows:
# Your first Pip package in Python
This repository is the companion post to my blog ["Your first Pip package in Python"](https://blog.dennisokeeffe.com)
## Usage
```py
from demo_pip_math.math import add, subtract, multiply
def test_add():
assert add(3, 2) == 5
def test_subtract():
assert subtract(3, 2) == 1
def test_multiply():
assert multiply(3, 2) == 6
```
Include non-Python files in upload
To enure our README.md and LICENSE are included, we can edit our MANIFEST.in file to have the following:
include README.md
include LICENSE
Packaging the files
Let's edit our Pipfile and add the following to [scripts]:
[scripts]
test = "pytest"
build = "python3 setup.py sdist bdist_wheel" # Added line here
deploy = "twine upload dist/*" # Added line here
This will enable us to run pipenv run build to generate our distribution archives and pipenv run deploy to upload our dist/ folder.