Creating and uploading a Python package to the PyPi: Part 3 Creating The package and uploading to pypi.org

winstonmhango23

6 min read

Views: 226

In this part of the series, we will walk you through the final steps of creating a Python package and uploading it to PyPi. We will use the example package bank_creator that we discussed in Part 2. The full code for this package can be found on GitHub.

Table of Contents

  1. Setting Up Your Package Structure
  2. Writing the setup.py File
  3. Creating the README File
  4. Building the Package
  5. Uploading the Package to PyPi
  6. Testing Your Package

Setting Up Your Package Structure

First, ensure that your project structure is correctly set up. Here's a typical structure for a Python package:

bank_creator/
├── bank_creator/
│   ├── __init__.py
│   ├── account.py
│   ├── bank.py
│   ├── decorators.py
│   ├── enhanced_bank.py
│   ├── loan.py
│   ├── reward.py
│   ├── utilities.py
├── tests/
│   ├── __init__.py
│   ├── test_account.py
│   ├── test_bank.py
│   ├── test_decorators.py
│   ├── test_enhanced_bank.py
│   ├── test_loan.py
│   ├── test_reward.py
│   ├── test_utilities.py
├── .gitignore
├── LICENSE
├── README.md
├── setup.py
└── USAGE.md

Writing the setup.py File

The setup.py file is essential for building and distributing your package. Here's the setup.py file for the bank_creator package with detailed comments:

# Import the setup and find_packages functions from setuptools
from setuptools import setup, find_packages

# Call the setup function, which is the primary function for setting up the package
setup(
    # The name of the package
    name="bank_creator",
    # The version of the package
    version="0.1.0",
    # The author's name
    author="winston mhango",
    # The author's email address
    author_email="winstonmhango23@gmail.com",
    # A short description of the package
    description="A comprehensive banking system package with accounts, loans, rewards, and utilities.",
    # A long description of the package, read from the README.md file
    long_description=open("README.md").read(),
    # The format of the long description
    long_description_content_type="text/markdown",
    # The URL for the project's homepage
    url="https://github.com/winstonmhango23/bank_creator",
    # Automatically find all packages within the current directory and subdirectories
    packages=find_packages(),
    # Classifiers help users find your project by categorizing it
    classifiers=[
        # The programming language used
        "Programming Language :: Python :: 3",
        # The license under which the package is distributed
        "License :: OSI Approved :: MIT License",
        # The operating systems on which the package can run
        "Operating System :: OS Independent",
    ],
    # Specify the minimum Python version required
    python_requires=">=3.6",
    # A list of dependencies required to use the package
    install_requires=[
        # List any dependencies here, e.g., 'requests', 'numpy'
        # For now, this is left empty
    ],
)

Creating the README File

The README file (README.md) is where you describe your package. This file will be displayed on your package's PyPi page. Here’s an example:

# bank_creator

`bank_creator` is a comprehensive banking system package with features for managing accounts, loans, rewards, and utilities.

## Features

- Create and manage savings, checking, and loan accounts.
- Perform deposits, withdrawals, and loan repayments.
- Earn and redeem rewards.
- Transfer funds between accounts.
- Track account operations with logging.
- Manage database records of accounts and transactions.

## Installation

You can install the package via pip:

```bash
pip install bank_creator

Usage

Detailed usage instructions can be found in the USAGE.md file. This is where you may want to add more information about the package usage. Mostly you would provide a link to some kind of documentation.

Build the package

Now, build your package using `setuptools`. Run the following commands in your terminal:

1. Navigate to your package directory:

    ```bash
    cd path/to/your/bank_creator
    ```

2. Build the source distribution and wheel distribution:

    ```bash
    python setup.py sdist bdist_wheel
    ```

If everything is setup properly, you may see the screen similar to the one below

This will create a `dist/` directory with `.tar.gz` and `.whl` files.

Uploading the Package to PyPi

To upload your package to PyPi, use `twine`. If you don’t have `twine` installed, you can install it via pip:

pip install twine

Create your account at pypi

Before you can upload your python package to the pypi directory, you need to create your account atpypi.org

There is a number of well guided instructions on how to complete your developer profile on pypi.org that you need to follow and complete properly.

Once you are done setting that up, Upload your package to PyPi using the following command:

Test your package before uploading

It is a good practice to run test on your package. Try running the installation command before you can publish your package. See how to do that in the screenshot below

Well, now we know it has been set up properly. But lets verify if the installation was really successful by running the pip list command. Here we can see that the package is indeed listed among the available packages :

twine upload dist/*

You will be prompted to enter your PyPi credentials. Use your username and the API token you generated from PyPi.

Testing Your Package

Once your package is uploaded, you should test it to ensure it works correctly when installed from PyPi. Create a virtual environment and install your package:

python -m venv env
source env/bin/activate  # On Windows use `env\Scripts\activate`
pip install bank_creator

Now, you can use your package in a Python script or an interactive Python session to verify that everything works as expected:

from bank_creator.account import SavingsAccount, CheckingAccount
from bank_creator.bank import Bank, BankBranch
from bank_creator.loan import LoanAccount
from bank_creator.reward import RewardAccount, RewardSavingsAccount, RewardCheckingAccount
from bank_creator.utilities import transfer

# Create bank and branches
bank = Bank("MyBank", initial_investment=100000)
branch1 = BankBranch("Branch1")
bank.add_branch(branch1)

# Create accounts
savings_account = SavingsAccount("12345", "John Doe", balance=500)
checking_account = CheckingAccount("67890", "Jane Doe", balance=1000)

# Add accounts to branch
branch1.add_account(savings_account)
branch1.add_account(checking_account)

# Perform operations
savings_account.deposit(200)
savings_account.withdraw(100)
checking_account.deposit(300)
checking_account.withdraw(50)

# Transfer funds
transfer(savings_account, checking_account, 50)

If everything works as expected, congratulations! You have successfully created and uploaded your Python package to PyPi. Mine is published on pypi.org here, and the code is available on github in the link below

Recent Related Posts

Related Posts

Creating and uploading a Python package to the PyPi: Part 2 Creating Git repository and uploading to github

Read More

Creating and uploading a Python package to the PyPi: Part 1 Basics

Read More

UNDERSTANDING THE if __name__ == "__main__" MAGIC METHOD IN PYTHON WITH DETAILED EXAMPLES

Python is known for its simplicity and readability, and it provides powerful constructs that enable developers to write clean and modular code. One such construct is the if __name__ == "__main__" statement, which is often seen in Python scripts

Read More

© 2024 .