Python: Publishing Packages
Tags: python, beginners, tutorial, pypi
Published: 2023-01-02 15:35:04

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
```
- **Twine** : Install the twine (https://pypi.org/project/twine/), this is a utility for publishing Python packages on PyPI. 
```
pip install twine
```
- **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
```

**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
```

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",
    ]
)
```

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

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

```

**Build the package:**

Now we have to package the complete project using:
```
python setup.py sdist bdist_wheel  
```

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/* 
```

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
```
Import the project in your py file, and call the method:

```
import friendspy.qoutes as qoutes
print(qoutes.random())
```
It will print:

```
How you doin'?
```