How to upload a package in PyPI using twine ?

While I was working on my project on software licensing, I noticed that there are several packages in PyPI which do not have a requirements.txt file (the file mentioning the dependencies). License files, though not a rare species but surely an endangered one in the Python World. These made my life so difficult you lazy programmers, grrr).

“One has to solve her problem.” So whenever I used to meet any developer I used to tell them the best practices for developers. Some of them used to give me not always a good vibe. (oh you irritating lady :(). Most of them were saddened by the long list of best practices. (me (grrr, grrr, grr))

I wanted to upload my new project, gitcen in PyPI. I shared my this thought with my eternal rival. He gave me quite smile.
He thinking : "Lady now you will understand". I smiled back.
Me thinking : "ohh man I hate you".

The code was all set and working

I used both PyCharm and jupyter notebook to write code.

The main job is done, now I will only have to upload my project at PyPI, the Pyhton Package Index.

requirements.txt

I created a requirements.txt file. One has to add the external dependencies. It helps to create the the similar development environment. Instead of manually typing the dependencies one can use pip freeze which will show the all modules installed in that particular virtual environment.

$ pip freeze
$ pip freeze > requirements.txt

This is easy, (Lazy coders).

setup.py

Now is the time for setup.py. It is a Python file which helps us to get the package. In the file, we call the setup function. The parameter inside the function are the different metadata of the project. The setup.py file of gitcen looks like this:

from setuptools import setup

setup(  
    name="gitcen",
    version='0.1.0',
    description="A project to find git information.",
    long_description="A project to find git information about authors' commits,most active days and time.",
    author="Anwesha Das.",
    author_email="anwesha@das.community",
    url="https://github.com/anweshadas/gitcen",
    license="GPLv3+",
    py_modules=['gitcen'],
    install_requires=[
        'Click',
        'pygit2==0.24'
    ],
    entry_points='''
        [console_scripts]
        gitcen=gitcen:main
    ''',
    classifiers=(
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
        'Programming Language :: Python :: 3.6'
    )
)

A little bite of history, previously there was package a named distutils which provided the setup function. But now we use the package setuptools.

It includes different information such as name, license, entry point etc. There is a parameter classifiers . Read more about it here.

This was lenghty but ok. May these coders are not that lazy (while coding, otherwise is. As per my experience with my man goes.)

Creating Source Distribution

The next was to create a compressed tarball for source distribution.

$python3 setup.py sdist

Output of the command is a tar.gz file.

Creating PyPI account

PyPI, the Python Package Index, the third party official software repository for Python ecosystem. One has to create an account in it.

test.pypi.org

If one wants to test the upload, test.pypi.org is the address to go to. Know more about the steps here.

When will this thing end?

Use twine

twine helps to upload packages in PyPI. It assembles the utilities to interact with PyPI. The first step was to install twine. I installed it from dnf package manager.

$ sudo dnf install python3-twine

I had to register my project. Using the following command

$ twine register dist/gitcen-0.1.0.tar.gz

Finally the moment had come! I uploaded gitcen in PyPI with the following command

 $ twine upload dist/gitcen-0.1.0.tar.gz

I had already put my authentication information in the ~/.pypirc

Now anyone can install gitcen from PyPI

$ pip install gitcen

Therefore going through this now I understand that it is very frustrating. You have a working code and you are not been able to share this with the community (easily, fast). If we give it a thought then we can realize that these are steps one has to memorize. As I was a first timer there were many new things to which I came across, so it made my life difficult. And yes,

Sorry coders, for not understanding your pain!