DEV Community

Bidut Sharkar Shemanto
Bidut Sharkar Shemanto

Posted on

How to Build and Publish a Python Package on PyPI: My Experience with `my_eda_helper`

Publishing your first Python package on PyPI is an exciting milestone for any developer. It’s a great way to share your work with the world, contribute to the open-source community, and showcase your skills. In this blog, I’ll walk you through the step-by-step process I followed to publish my package, my_eda_helper, which simplifies Exploratory Data Analysis (EDA). I’ll also share tips on handling errors, publishing updates, and best practices. Let’s dive in! 🚀


What is my_eda_helper?

my_eda_helper is a Python package designed to streamline the EDA process. It provides helper functions for tasks like:

✅ Missing data analysis

✅ Correlation heatmaps

✅ Hypothesis testing

✅ Interactive visualizations

The goal is to reduce repetitive tasks and allow users to focus on deriving insights rather than writing boilerplate code.


Step-by-Step Guide to Publishing a Python Package

1. Plan Your Package

Before writing any code, plan what your package will do. For my_eda_helper, I wanted to automate common EDA tasks and make them reusable.


2. Organize Your Project Structure

A well-structured project is crucial for maintainability. Your project should have the following structure:

my_eda_helper/                  <-- Root folder
├── my_eda_helper/              <-- Package folder
│   ├── __init__.py             <-- Makes it a package
│   └── core.py                 <-- Main code
├── setup.py                    <-- Package metadata
├── README.md                   <-- Documentation
├── LICENSE                     <-- License file
└── tests/                      <-- Optional: Tests
Enter fullscreen mode Exit fullscreen mode

📌 Explanation:

  • __init__.py: Marks the folder as a Python package. Add from .core import * to expose functions.
  • core.py: Contains the main code (e.g., functions for EDA).
  • setup.py: Defines package metadata and dependencies.
  • README.md: Provides installation and usage instructions.
  • LICENSE: Defines the package license.
  • tests/: (Optional) Contains unit tests for your package.

3. Write setup.py

The setup.py file is crucial for packaging your project. Here’s what mine looks like:

from setuptools import setup, find_packages

setup(
    name="my_eda_helper",
    version="0.1.0",  # Start with 0.1.0 for the first release
    author="Your Name",
    author_email="your.email@example.com",
    description="A helper package for Exploratory Data Analysis (EDA)",
    long_description=open("README.md").read(),
    long_description_content_type="text/markdown",
    url="https://github.com/shemanto27/eda-helper-py",
    packages=find_packages(),
    install_requires=[
        "numpy",
        "pandas",
        "seaborn",
        "matplotlib",
        "scipy",
        "statsmodels",
        "scikit-learn",
        "wordcloud",
        "IPython",
    ],
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires=">=3.6",
)
Enter fullscreen mode Exit fullscreen mode

📌 Why?

This file tells PyPI how to install and use your package.


4. Write a README.md

Your README.md should include:

  • A short description
  • Installation instructions
  • Usage examples

Here’s a snippet from mine:

# My EDA Helper

A Python package to simplify Exploratory Data Analysis (EDA).

## Installation
Enter fullscreen mode Exit fullscreen mode


bash
pip install my_eda_helper


## Usage
Enter fullscreen mode Exit fullscreen mode


python
import my_eda_helper as eda

Missing data analysis

missing_data = eda.missing_info(df)
print(missing_data)

Enter fullscreen mode Exit fullscreen mode

5. Build Your Package

Before uploading to PyPI, build your package:

pip install build
python -m build
Enter fullscreen mode Exit fullscreen mode

This creates a dist/ folder containing .tar.gz and .whl files.


6. Upload to PyPI

Step 1: Create a PyPI Account

Sign up at pypi.org.

Step 2: Generate an API Token

  • Go to Account Settings > API Tokens
  • Create a new token

Step 3: Upload Your Package

pip install twine
python -m twine upload dist/*
Enter fullscreen mode Exit fullscreen mode
  • Use __token__ as the username.
  • Paste your API token as the password.

Your package is now live on PyPI! 🎉


7. Test Your Package

To confirm the installation works, install and import it:

pip install my_eda_helper
python -c "import my_eda_helper; print(dir(my_eda_helper))"
Enter fullscreen mode Exit fullscreen mode

Handling Errors and Publishing Updates

Common Errors

Package Not Found:

  • Ensure the package name is unique on PyPI.
  • Double-check the name field in setup.py.

Missing Files:

  • Ensure all necessary files (__init__.py, core.py, README.md) are included.

Version Conflict:

  • Increment the version number in setup.py before uploading a new version.

Publishing a New Version

If you want to update your package:

1️⃣ Update the version number in setup.py (e.g., 0.1.1).

2️⃣ Rebuild the package:

python -m build
Enter fullscreen mode Exit fullscreen mode

3️⃣ Upload the new version:

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

✅ Done! Your new version is live.


Final Thoughts

Publishing a Python package is a rewarding experience. Whether it’s a small utility or a full-fledged library, your work can help others and grow your skills.

If you found this guide helpful, follow me for more tips and projects:

🔗 LinkedIn: https://www.linkedin.com/in/shemanto/

🐍 GitHub: http://github.com/shemanto27/

Happy coding! 🚀

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay