Publishing modules to pip and PyPi

…衆ロ難τιáo~ 提交于 2021-02-07 11:13:33

问题


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:

  1. create an account on PyPi.

  2. Create a README.md file as an instruction for users (Highly recommended).

  3. Create a setup.cfg file, and write the following content:

[metadata]
description-file = README.md
  1. Create a LICENSE file by referencing this website.

  2. As @Yang HG mentioned, write a setup.py file, followed by running python setup.py sdist.

  3. Upload your distribution by using twine. First, you need to pip install twine, then run twine 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:

  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/*


来源:https://stackoverflow.com/questions/56129825/publishing-modules-to-pip-and-pypi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!