DEV Community

Cover image for Make your first Python Package and publish it to PyPi!
Sachit71
Sachit71

Posted on

Make your first Python Package and publish it to PyPi!

Hello there! This blog post is on how you can publish your first Python package.

PS: It is really easy!

What are Python packages?

A bunch of reusable code scripts can be called a package, it's something like this.

You've probably written some awesome code, which can be reused, and you want to share it with the world!

This tutorial is just for that! While GitHub is a great place to distribute code, Python Package index let's you directly import code without having to copy it into your directory!

Prerequisites

  1. Basic Python knowledge.
  2. Linux or Macintosh OS, unfortunately publishing a package from Windows is quite hectic.
  3. A PYPI account
  4. A GitHub account

And we should be good to go!

Here I will be showing you how I published pysherlock:

Step 1: Make a new folder named after your package.

In my case it was pysherlock, In your folder make another folder with the same name as the parent folder.

This is where the code will go.

There will be two files in here:

  1. _init_.py
  2. main.py

_init_.py is to import all your functions from the main.py file
main.py is where all your code will go.

Step 2: Write some code

For this tutorial let's write some basic code.

Assume that your package's name is 'pyword'(very creative I know)

# main.py
def wordrep(word:str):
     print(word)
Enter fullscreen mode Exit fullscreen mode

So what this basically does is, when a string is passed as an argument it simply repeats the word entered. Also you might face an indentation error, which can easily be fixed.

Now to the _init_.py file, remember to add the underscores before and after init.

#____init.py____
from pyword.main import wordrep
Enter fullscreen mode Exit fullscreen mode

And boom your package is ready for publication, now for the formalities.

Step 3: Push to GitHub

Publish your code to GitHub by making a repo

Step 4: Other files config

This how your directory must look:

-pyword
-pyword
-_init_.py
-main.py
-setup.py
-README.md
-LICENSE.txt
-setup.cfg

It might look complex but it's actually quite easy.

The setup.py file is a python file whiih basically tells the PYPI repo what your package is.

Let's get started with it

import setuptools

# Reads the content of your README.md into a variable to be used in the setup below
with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()

setuptools.setup(
    name='pyword',                           # should match the package folder
    packages=['pyword'],                     # should match the package folder
    version='0.1',                                # important for updates
    license='MIT',                                  # should match your chosen license
    description='Baically repeats what you say.',
    long_description=long_description,              # loads your README.md
    long_description_content_type="text/markdown",  # README.md is of type 'markdown'
    author='your name',
    author_email='your email address',
    url='your github repo name', 
    project_urls = {                                # Optional
        "Headless Chrome": "your github repos issues page"
    },
    install_requires=['requests','qrcode','wikipedia','beautifulsoup4'], #these are filler packages, you need to fill them with the packages your python package will require to function                 
    keywords=["screenshot", "pyautogui", "scrot", "raspberry pi"], #descriptive meta-data
    classifiers=[                                   # https://pypi.org/classifiers
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',

        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
        'Programming Language :: Python :: 3.9',
    ]

    )
Enter fullscreen mode Exit fullscreen mode

Now to the README.md file, this file is to be written by you, it's basically short documentation for your package.

Here's a really simple website I use for writing my readme files.
Make a read me

License.txt is again a file you must choose for your package, depending on what your package does you can choose a License and use it.

Use this link to know what License fits your package.
Open source initiative

Now to the setup.cfg file

[metadata]
description-file = README.md
Enter fullscreen mode Exit fullscreen mode

That is all!

Now that all your files are ready let's publish it!

First in your Home directory create a file names .pypirc

It should contain the following info:

[distutils]
index-servers = 
       pypi
[pypi]
repository: https://upload.pypi.org/legacy/
username: your_username_from_pypi
password: your_password_from_pypi
Enter fullscreen mode Exit fullscreen mode

And save it, the "pypi" in line 3 should be placed after a single 'TAB' key press.

This basically enters the data required to push your package into the pypi repository.

Step 5: Final step, uploading to pypi

Now towards the final steps

pip install twine

After this is done, cd into the directory and type the following into your terminal.

python3 setup.py sdist
Enter fullscreen mode Exit fullscreen mode

This builds the distribution file required.
Do not tamper with this File.

After this is done type the following:

python3 -m twine upload dist/*
Enter fullscreen mode Exit fullscreen mode

And that's it!

There should be a link below saying where your package has been published.

Click on it and it will lead to your packages pypi page, install it and test it out!

Happy Coding and if any doubts don't forget to to comment and ask your doubts.

Also please do leave a like and follow me for more such tutorials and articles related to programming.

Discussion (2)

Collapse
vadimkolobanov profile image
Vadim Kolobanov

Thank you, an interesting topic, you are well done

Collapse
cyber profile image
Sachit71 Author

Thanks means a lot!