← Back to list

How to Publish Your Python Package to PyPI Using Poetry

Introduction: Share your Useful Python Utilities in 10 Minutes

Deepak Patidar · 2024-04-25 14:57 · 2 claps · 4.4 min read
#poetry #pypi #python-programming #library-design #deepak
Open on Medium ↗
Wiki topics: LIT · Literature & Writing 💻 · Programming ✍️ · Writing & Creative 📚 · Books & Reading

How to Publish Your Python Package to PyPI Using Poetry

Introduction: Share your Useful Python Utilities in 10 Minutes

So, you’ve got those handy Python functions or classes that you keep using across projects, huh? Ever wished you could just import them seamlessly into any project? Well, PyPI’s the place to make that happen. You’ve probably stumbled upon PyPI while installing Python packages yourself. But did you know you can share your own creations there too? And guess what? It’s easier than you think!

Motivation: Empower Your Python Creations

Ever found yourself copying and pasting the same piece of code over and over again? It’s time to break that cycle! Let’s make your useful functions accessible to you and the whole Python community. If they’re handy for you, they’ll likely be handy for others too!

Getting Started: I Published a Package on PyPI and you can as well!

I’ve been there too, using tools like Rich but finding them slightly lacking for my needs. So, I decided to craft my own solution and share it with the world. Now, instead of wrestling with 220 lines of code, I get to prettify my DataFrame with just one line. Cool, right?

Dive into Poetry: Your Ticket to Python Package Management

First things first, let’s talk about Poetry. It’s like the conductor of a Python package orchestra, making dependency management and packaging a breeze. Installing it is a snap, and creating a project with it is even easier.

What is Poetry?

Poetry is a Python library designed to simplify dependency management and packaging for Python projects. Gone are the days of wrestling with requirements.txt files and setup.py scripts. With Poetry, you can manage dependencies with ease and package your project effortlessly.

Create a Project: Let’s Get Crafting!

So, you’ve got your brilliant Python utility in mind. Now it’s time to package it up and share it with the world. Let’s walk through the process step by step.

What is Poetry?

Poetry is a Python library that makes it easy and efficient to manage dependencies and package your Python project. Find instructions on how to install Poetry here.

Step 1: Create a Project

First, fire up your terminal and create a new Poetry project. If you’re starting from scratch, simply run:

$ poetry new pretty_text

If you already have a project, navigate to its directory and initialize Poetry:

$ cd pretty_text
$ poetry init

Poetry will guide you through a series of questions to set up your project.

Step 2: Activate Virtual Environment and Install Dependencies

But before we get too far ahead of ourselves, let’s talk about virtual environments. These nifty tools keep your project dependencies neatly isolated from one another. With Poetry, activating a virtual environment is as easy as pie:

$ poetry shell

With your virtual environment up and running, it’s time to install your dependencies. Let’s start with pyfiglet, shall we?

$ poetry add pyfiglet

Poetry will handle the installation for you, and it’ll even update your pyproject.toml and poetry.lock files to keep track of everything.

Step 3: Add Content of the Package

Now it’s time to add the meat of your package. Create a new file under the pretty_text/pretty_text directory:

$ touch pretty_text/pretty.py

This is where your Python magic will happen. Write your code, and when you’re ready, run it using Poetry:

$ poetry run python pretty_text/pretty.py

And just like that, your function is alive and kicking!

Shorten the Import Statement: Because Who Likes Typing Anyway?

Sure, your package works like a charm, but that import statement. Kinda long, right? Let’s fix that.

Streamline Your Imports

In your pretty_text/__init__.py file, add the following line:

from pretty import pretty_text

Now you can import your function directly from pretty_text!

Publish Your Package: Share Your Genius with the World

You’ve toiled away, crafting the perfect Python package. Now it’s time to share it with the masses. But before you hit that publish button, let’s dot our i’s and cross our t’s.

Build and Publish

First things first, make sure you have an account on PyPI. Once you’re all set up, simply run:

$ poetry build

This will create the necessary distribution files. Then, to publish your package, just type:

$ poetry publish

And just like that, your package is out in the wild, ready for the world to enjoy!

Making It Shine: Make Your Package Look Professional

So, you’ve got your package out there, but it’s looking a little… dull. Let’s spruce it up a bit, shall we?

Create a Link to your Source Code: Share the Love

If you want others to contribute to your masterpiece, you’ll need to give them access to your source code. Luckily, GitHub makes this easy.

Start with a Repository

Head over to GitHub and create a new repository for your package. Give it a catchy name like pretty-text.

Add Some Descriptions and Images

Nobody likes a boring README. Spice yours up with a captivating description and maybe a few images to catch the eye.

Update Your Version: Keep It Fresh

As your package evolves, so too should its version number. Make sure to update it in your pyproject.toml file.

Update and Publish

Once everything looks just right, it’s time to push those changes to GitHub and share your masterpiece with the world.

Conclusion: Your Journey to Python Package Stardom

Congratulations, you’ve just leveled up your Python game! With Poetry by your side, you’ve learned how to create, package, and share your Python creations with the world. So go forth, craft your masterpieces, and share them with pride!

FAQs (Frequently Asked Questions)

  1. What is PyPI, and why should I publish my package there? PyPI, or the Python Package Index, is a repository of software packages for the Python programming language. By publishing your package on PyPI, you make it accessible to the global Python community, allowing others to easily discover and use your work.
  2. Do I need to be an expert programmer to publish a package on PyPI? Not at all! While some technical knowledge is certainly helpful, platforms like Poetry make the process of packaging and publishing Python projects much more accessible to developers of all skill levels.
  3. Is Poetry the only way to manage dependencies and package Python projects? No, there are other tools available for managing Python dependencies and packaging projects, such as pip and setuptools. However, Poetry has gained popularity for its simplicity and user-friendly interface.
  4. Can I update my package on PyPI after publishing it? Absolutely! You can update your package on PyPI whenever you make changes or improvements to it. Simply follow the same process you used to publish it initially, and your updates will be made available to users who install your package

메타데이터
post_id
8b1d79291474
slug
how-to-publish-your-python-package-to-pypi-using-poetry-8b1d79291474
url
https://medium.com/@deepak-patidar/how-to-publish-your-python-package-to-pypi-using-poetry-8b1d79291474
canonical_url
https://medium.com/@deepak-patidar/how-to-publish-your-python-package-to-pypi-using-poetry-8b1d79291474
author_url
https://medium.com/@deepak-patidar
status
ok
fetched_at
2026-07-23 23:50:41