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

winstonmhango23

5 min read

Views: 243

Table of Contents

  1. Introduction
  2. Why Create Python Packages?
  3. Python Package Structure
  4. Setting Up the Project
  5. Creating the Package Files
  6. Writing the Setup Script
  7. Conclusion

Introduction

In the world of software development, code reusability is crucial. One of the best ways to achieve this in Python is by creating packages. Python packages are collections of modules that can be reused across different projects. In this multi-part series, we will guide you through the process of creating and uploading a Python package to the Python Package Index (PyPi). This first part will cover the fundamentals, and in subsequent parts, we will build and upload a package named bank_creator to PyPi.

Why Create Python Packages?

Creating Python packages has several benefits:

  • Reusability: Write code once and reuse it in multiple projects.
  • Distribution: Share your code with the community or distribute it within your organization.
  • Maintainability: Manage and update code in a structured manner.

Python Package Structure

A typical Python package structure looks like this:

my_package/
├── my_package/
│   ├── __init__.py
│   ├── module1.py
│   ├── module2.py
├── tests/
│   ├── __init__.py
│   ├── test_module1.py
│   ├── test_module2.py
├── LICENSE
├── README.md
├── setup.py
├── requirements.txt

And since we intend to create a banck_creator, it will follow that structure, and here is how our code will look like:

bank_creator/
├── bank_creator/
│   ├── __init__.py
│   ├── bank.py
│   ├── branch.py
│   ├── account.py
│   ├── loan.py
│   ├── utilities.py
│   ├── decorators.py
│   ├── database.py
├── tests/
│   ├── __init__.py
│   ├── test_bank.py
│   ├── test_branch.py
│   ├── test_account.py
│   ├── test_loan.py
│   ├── test_utilities.py
├── README.md
├── LICENSE
├── setup.py

  • my_package/: This is the root directory of your package.
  • my_package/: Contains your package modules.
  • tests/: Contains your test cases.
  • LICENSE: License for your package.
  • README.md: Description and usage instructions for your package.
  • setup.py: Script for setting up the package.
  • requirements.txt: List of dependencies.

Writing the Setup Script

The setup.py file is the script used to build and distribute the package. Here's a basic example:

# setup.py
from setuptools import setup, find_packages

setup(
    name='bank_creator',
    version='0.1.0',
    packages=find_packages(),
    install_requires=[],
    entry_points={
        'console_scripts': [
            'bank_creator=bank_creator.cli:main',
        ],
    },
    author='Your Name',
    author_email='your.email@example.com',
    description='A package for creating and managing bank accounts',
    long_description=open('README.md').read(),
    long_description_content_type='text/markdown',
    url='https://github.com/yourusername/bank_creator',
    classifiers=[
        'Programming Language :: Python :: 3',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
    ],
    python_requires='>=3.6',
)

Key Components of setup.py

  • name: The name of your package.
  • version: The version of your package.
  • packages: The packages to include. find_packages() automatically finds all packages and sub-packages.
  • install_requires: A list of dependencies.
  • entry_points: Console scripts entry points.
  • author and author_email: Your name and email.
  • description: A short description of your package.
  • long_description: A detailed description (often the content of README.md).
  • url: The URL of the package's homepage.
  • classifiers: A list of classifiers that define the package.
  • python_requires: The Python versions supported.

Conclusion

In this first part, we have covered the basics of creating a Python package, including setting up the project structure, and creating and writing the setup.py script.

In the next part, we will dive deeper into adding functionality to our bank_creator package, writing tests, and preparing the package for distribution.

Recent Related Posts

Related Posts

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

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.

Read More

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

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 .