Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

python - Publishing modules to pip and PyPi

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  1. Install twine:
python3 -m pip install --user --upgrade twine
  1. Register to PyPi.
  2. Run twine to upload dist packages to PyPi:
python3 -m twine upload dist/*

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...