Publishing modules to pip and PyPi

Question:

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?

Asked By: Roshan

||

Answers:

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/*
Answered By: AdamGold

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/

Answered By: Sean_Syue

You can publish your package using this tool i made, find source code: https://gist.github.com/lamoboos223/e1b8e3638e3b5eddd01ae0bf2d1c8d73

In your project’s root execute the following command:

bash <(curl -s https://gist.githubusercontent.com/lamoboos223/e1b8e3638e3b5eddd01ae0bf2d1c8d73/raw/ff4f1ac762a45c4a45f5ac72faf3b9240626478f/publish_to_pypi.sh)

It’s an interactive tool that will generate a setup.py file. Then it will ask you which env of pypi you would like to publish your package to.

Answered By: lamoboos223
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.