问题
I have created a module using python. I want to publish it to pip and PyPi so that others can download and use it easily. How do I do it?
回答1:
The answer can be easily found on the Internet. I just referenced this site to answer you. You can follow the steps below:
create an account on PyPi.
Create a
README.md
file as an instruction for users (Highly recommended).Create a
setup.cfg
file, and write the following content:
[metadata]
description-file = README.md
Create a
LICENSE
file by referencing this website.As @Yang HG mentioned, write a
setup.py
file, followed by runningpython setup.py sdist
.Upload your distribution by using
twine
. First, you need topip install twine
, then runtwine upload dist/*
.
Finally, your distribution can be viewed on https://pypi.org/project/YOURPACKAGENAME/
回答2:
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 uploaddist
packages to PyPi:
python3 -m twine upload dist/*
来源:https://stackoverflow.com/questions/56129825/publishing-modules-to-pip-and-pypi