How to create and upload Python package to PyPI?

You must be familiar with Packages in python and how they are used. In simple words, a Python package is a collection of Python modules that provides an easy way of structuring these modules. Some of the most common and popular Python packages that you must have come across include NumPy, pandas, matplotlib etc.
Note: Python packages contain an additional __init.py__ file to distinguish themselves from directories that just happen to contain a bunch of Python scripts.

Python packages provide very efficient ways of solving different problems. Let us say you wrote a package, wouldn’t you want to share it with the rest of the programmers? This is exactly where the PyPI helps. So, in this tutorial, you will learn how to create and upload the Python package to PyPI.

The Python Package Index(PyPI)

To share your Python package with other people, you need a central repository that programmers across the world can access. The PyPI is the official software repository for Python and contains more than 3,00,000 Python packages. You might find it interesting to know that the pip command that you come across while installing packages uses PyPI as its source. That is, along with installing packages, it allows you to publish your packages as well.
You can visit and check it out at The PyPI.

Creating a Python Package

  • Firstly, choose a name for your package. While doing this, do ensure that the name doesn’t already exist on PyPI.
    Now create a folder and name it with the same name as that of the package. Inside this folder, create another folder with the same name(package) and drop all the .py files of your package here.
  • Now create an __init__.py file and save it in the same folder. Whenever you import a package into a Python program, the __init__.py program must run. That is, the purpose of the __init__.py file is to inform the interpreter that the folder contains the package.
    All the folders and code(classes/methods etc) must be imported in __init__.py.
  • Now, go to the root folder (the first folder you created) or simply one level up in the directory. Here, you need to create three files:
    Setup.py
    README.md
    License.txt
  • The Setup.py file is basically a guide to the package that describes the dependencies involved, authors, versions etc. Whenever a pip command is run, it automatically looks up for the setup file that calls the setup method that receives the above-specified arguments(dependencies/authors etc). An example setup file is as shown:
    from setuptools import setup, find_packages
    setup(
        name='varsha example project',
        version='0.6',
        license='MIT',
        author="Varsha",
        author_email='[email protected]',
        packages=find_packages('src'),
        package_dir={'': 'src'},
        url='https://github.com/varsha example',
        keywords='example project',
    )

     

  • The License.txt file is more like an agreement that specifies to the user, the terms and conditions under which they can use your package. You can simply create your own license file specifying the terms and policies or choose from license makers online (choosealicense.com).
  • The README.md file is more like an ‘about file’, that simply tells the users about your package. This markdown file must be easy to understand and can be created using any editor.

Uploading a Python Package

Once you have successfully created your package, you need to upload it to the PyPI.

  • First of all, go to¬†pypi.org and create yourself an account. Note down your credentials as these will be asked later on.
  • Next, install the twine package into your environment. Twine allows you to publish Python packages into different software repositories including the PyPI.
    pip install twine

    Twine allows you to upload the entire package in the form of a .dist file.
    ‘.dist’ is a simple extension used for configuration files that do not contain any personal data and help you get started with the software/application.

  • To create the .dist file for your package, open the terminal and cd to the folder containing your package. Then, run the following command:
    python setup.py bdist_wheel

    This will create both the source distribution as well as a Python wheel for your package and both of them will be in your current directory.

  • Once this is done, you can finally upload your package in the form of a .dist file by running the below command:
    twine upload dist/*

    Enter the necessary credentials of your PyPI account and press enter. The package gets uploaded to the PyPI and a link for the same is displayed.

  • You can further cross-check this by going to https://pypi.org/ and searching for your package by specifying its name.

Thus, you can now successfully share your packages with programmers over the world.

Also read, Managing Packages with pip in Python!

Leave a Reply

Your email address will not be published.