Publish your own python project on PyPi.org

  1. create a simple project:
    Directory of C:\Hans\packaging_tutorial
C:\Hans\packaging_tutorial>tree /F
Folder PATH listing for volume Windows7_OS
Volume serial number is 006F0074 54AA:C963
C:.
│ LICENSE
│ README.md
│ setup.py
│
└─list_difference
__init__.py

C:\Hans\packaging_tutorial>type setup.py

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="list_difference_zhuby1973", # Replace with your own username
    version="0.0.2",
    author="Hans Zhu",
    author_email="zhuby1973@gmail.com",
    description="A package to find difference between two list",
    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",
    ],
    python_requires='>=3.6',
)

C:\Hans\packaging_tutorial>type README.md

Example Package
This is a simple example package.
  1. Generating distribution archives
    Make sure you have the latest versions of setuptools and wheel installed:
    python -m pip install –user –upgrade setuptools wheel
    Now run this command from the same directory where setup.py is located:
    python setup.py sdist bdist_wheel
    This command should output a lot of text and once completed should generate two files in the dist directory:
    Directory of C:\Hans\packaging_tutorial\dist

05/28/2020 06:31 PM .
05/28/2020 06:31 PM ..
05/28/2020 06:31 PM 2,626 list_difference_zhuby1973-0.0.1-py3-none-
any.whl
05/28/2020 06:31 PM 1,304 list_difference_zhuby1973-0.0.1.tar.gz
2 File(s) 3,930 bytes

  1. Uploading the distribution archives
    you need register an account on pypi.org first!
    You’ll need to install Twine:
    python -m pip install –user –upgrade twine
    Once installed, run Twine to upload all of the archives under dist:
    python -m twine upload –repository pypi dist/*
    You will be prompted for a username and password.
  2. Installing your newly uploaded package
    You can use pip to install your package and verify that it works.
    python -m pip install –index-url https://pypi.org/simple/ –no-deps list_difference_zhuby1973
    Make sure to specify your username in the package name!
    pip should install the package from Test PyPI and the output should look something like this:
    Collecting list_difference_zhuby1973
    Downloading list_difference_zhuby1973-0.0.1-py3-none-any.whl (2.6 kB)
    Installing collected packages: list-difference-zhuby1973
    Successfully installed list-difference-zhuby1973-0.0.1
    You can test that it was installed correctly by importing the package. Run the Python interpreter (make sure you’re still in your virtualenv):

C:> python
and from the interpreter shell import the package: import list_difference

Congratulations, you’ve packaged and distributed a Python project!

Leave a Reply

Your email address will not be published. Required fields are marked *