DEV Community

Cover image for Python: Publishing Packages
Ashutosh Sharma
Ashutosh Sharma

Posted on

Python: Publishing Packages

You've created a Python project and you're eager to share it with others. But first, you'll have to package it! This tutorial walks you through how to package a simple Python project. It will show you how to add the necessary files and structure to create the package, how to build the package, and how to upload it to PyPI.

Environment Setup:

  • pip Upgrade: Update the pip version to the latest:
python3 -m pip install --upgrade pip
Enter fullscreen mode Exit fullscreen mode
pip install twine
Enter fullscreen mode Exit fullscreen mode
  • Wheel: Let's install wheel (https://pypi.org/project/wheel/), which is the reference implementation of the Python wheel packaging standard, as defined in PEP 427.
pip install wheel
Enter fullscreen mode Exit fullscreen mode

Python Project and Structure:

Let's create a small project to upload to PyPI.

I was looking for an idea what to create to explain the packaging, and I was watching Friends so thought to create something around it.

So let's name the project FriendsPy ( it's not Friend SPY :D ).

Create a new folder name it FriendsPy. Under the folder we need below structure:

FriendsPy/
├── friendspy/
│   ├── qoutes/
│      ├── __init__.py
├── setup.py
├── README.md
Enter fullscreen mode Exit fullscreen mode

The setup.py will hold all the required properties to upload the package to PyPI.

Sample file will have below content:

from setuptools import setup

VERSION = '0.0.1'
DESCRIPTION = 'Friends Show Related API'
LONG_DESCRIPTION = 'A package that allows to get details about friends show'

# Setting up
setup(
    name="friendspy",
    version=VERSION,
    author="Ashutosh Sharma",
    author_email="email2ashusharma@gmail.com",
    description=DESCRIPTION,
    long_description_content_type="text/markdown",
    long_description=LONG_DESCRIPTION,
    packages = ['friendspy.qoutes'], # this should have the list of packages we want to expose
    install_requires=[],
    keywords=['python', 'friends', 'entertainment', 'fun'],
    classifiers=[
        "Development Status :: 1 - Planning",
        "Intended Audience :: Developers",
        "Programming Language :: Python :: 3",
        "Operating System :: Unix",
        "Operating System :: MacOS :: MacOS X",
        "Operating System :: Microsoft :: Windows",
    ]
)
Enter fullscreen mode Exit fullscreen mode

Let's create a method under __init__.py file which can be used by other:

def random():
    return "How you doin'?"

Enter fullscreen mode Exit fullscreen mode

Build the package:

Now we have to package the complete project using:

python setup.py sdist bdist_wheel  
Enter fullscreen mode Exit fullscreen mode

It will create a dist folder which will have the packages compatible for PyPI.

Upload to PyPI:
To upload this package we have to run below command:

twine upload dist/* 
Enter fullscreen mode Exit fullscreen mode

It will ask you for the username and password for PyPI website. Once authenticated it will uploaded to PyPI.

GitHub Repository: https://github.com/ashusharmatech/friendspy
PyPI: https://pypi.org/project/friendspy/

Use this library:
Install the latest version of package:

pip install friendspy==0.0.2
Enter fullscreen mode Exit fullscreen mode

Import the project in your py file, and call the method:

import friendspy.qoutes as qoutes
print(qoutes.random())
Enter fullscreen mode Exit fullscreen mode

It will print:

How you doin'?
Enter fullscreen mode Exit fullscreen mode

Top comments (0)