How to Publish a Python Package to PyPI using Poetry | by Edward Krueger | Towards Data Science

By: Edward Krueger and Dylan Rossi
Have you ever wanted to create a python package for the world to use but thought it was too difficult to get published? Poetry can alleviate your worries with how simple they make the process.
Once you have your package created, Poetry will take the wheel (yes, that is a pun) and do the publishing work for you.
Note: This article will assume you have already built a python package with Poetry that is ready for publishing. If you do not have a package already created, please download our example and follow along.
To publish a python package to PyPI, you must first have a PyPI account.
If you do not have one created, head over to PyPI and click on the Register link.
They will ask for the usual information (email, username, password) and register your account.
You should also receive an email from them asking you to confirm your account registration.
Once you are fully registered, sign in and head over to your account page.
You now need to create an API token to connect to Poetry. This token will allow Poetry to know where to publish your package and also give it access/permission to publish to your account.
From your account page, click on the “Account Settings” link on the left-hand side.
Scroll down until you see the “API Tokens” section and click the “Add API Token” button.
You will then give your token a name and scope.
For this example, we are naming the token “poetrypackagetemplate.”
You will also set the scope to “Entire Account (all projects),” but in practice, it is better to select one token per project instead (which is why it will notify you with a cautionary message).
After this is filled in, create the API token by clicking the “Add Token” button.
PyPI will generate your API token and display it on your screen, but only for now, so be sure to copy it or leave this page open until we connect it to Poetry.
Now that you have an API token connected to your PyPI account, we can give this token to Poetry, so it knows where to publish your package. We can do this by creating environment variables that will save your credentials which we can then pass to Poetry’s publish command.
Go ahead and open up Terminal (git-bash on Windows) and make sure you are in the directory of either your python package or our example one.
Then run these commands on macOS or Linux:
export PYPI_USERNAME=__token__export PYPI_PASSWORD=<Your API Token>
Run these commands (on Windows):
set PYPI_USERNAME=__token__set PYPI_PASSWORD=<Your API Token>
That is it! Poetry is now ready to publish your python package to PyPI.
To finalize your publication, you will need to run one more command.
poetry publish --build --username $PYPI_USERNAME --password $PYPI_PASSWORD
Poetry will then begin the process to build and publish your python package to PyPI.
Once it finishes, your package will be on your PyPI account and available to install via pip!
As you can see from this brief article, publishing your python package to PyPI is a breeze with Poetry.
If you want, you can test out installing your newly published package by simply running
pip install <Your Package Name>
Congratulations on publishing your python package!
Check out this article by Christopher Sidebottom if you want some more information on Poetry: