This is well documented in Packaging Python Projects.
Creating README.md
Create a file named README.md
and edit it as you like (in Markdown).
Creating setup.py
setup.py
is the build script for setuptools
. It tells setuptools
about your package (such as the name and version) as well as which code files to include.
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="example-pkg-your-username",
version="0.0.1",
author="YOUR NAME",
author_email="YOUR EMAIL",
description="A small example package",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pypa/sampleproject",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
)
Creating a LICENSE
Create a file named LICENSE
and choose your content from here.
Generating distribution archives
The next step is to generate distribution packages for the package. These are archives that are uploaded to the Package Index and can be installed by pip.
We first need to make sure we have wheel
and setuptools
installed:
python3 -m pip install --user --upgrade setuptools wheel
Now we need to run the following command from the same directory setup.py
is located:
python3 setup.py sdist bdist_wheel
Uploading the distribution archives
It is recommended to upload to TestPyPi before the actual PyPi - although I will not cover this part.
The following steps show how to upload your package to PyPi:
- Install
twine
:
python3 -m pip install --user --upgrade twine
- Register to PyPi.
- Run
twine
to upload dist
packages to PyPi:
python3 -m twine upload dist/*
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…