Have you ever wanted to create a python package that the world could use, but you think it’s too hard to get published? Poetry can alleviate your worries about how simple they make the process.
Once you’ve created your package, Poetry takes the wheel (yes, it’s a dictionary) and does the publishing work for you.
Note: This article assumes that you have already built a poetry-related python package that is ready for release. If you do not already have a package created, please download our example and follow along.
TO to publish a python package in PyPI, you must first have a PyPI account.
If you have not created one, go to PyPI and click the Register link.
They will ask for standard 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, log in and go to your account page.
You will now need to create an API ID to connect to poetry. This ID allows Poetry to know where the package will be published and also gives it permission to publish to your account.
Click the “Account Preferences” link on the left side of the account page.
Scroll down until you see the “API Tokens” section, and click the “Add API Token” button.
You then give your ID a name and scope.
In this example, we name the ID “poetpackagetemplate”.
You also set the scope to Full Account (all projects), but in practice it’s better to choose one tag for each project (which is why it notifies you with a warning message).
Once this is complete, click the Add ID button to create an API ID.
PyPI will create your API and display it on your screen, but only for now, so be sure to copy it or leave this page open until we associate it with poetry.
Now that you have an API ID associated with your PyPI account, we can give this ID to Poetry so that it knows where the package will be released. We can do this by creating environment variables that store login information, which we can then pass to the Poetry publish command.
Go ahead and open Terminal (git-bash in Windows) and make sure you are in either your python package or our example directory.
Then run these commands on macOS or Linux:
export PYPI_USERNAME=__token__export PYPI_PASSWORD=<Your API Token>
Run these commands (Windows):
set PYPI_USERNAME=__token__set PYPI_PASSWORD=<Your API Token>
That’s it! Poetry is now ready to publish your python package in PyPI.
You must run one more command to complete the publication.
poetry publish --build --username $PYPI_USERNAME --password $PYPI_PASSWORD
Poetry then begins building and publishing your python package in PyPI.
When it’s done, your package will be in your PyPI account and can be installed via pip!
As you can see from this short article, publishing a Python package in PyPI is easy with Poetry.
If you want, you can test the installation of a newly released package by simply performing
pip install <Your Package Name>
Congratulations on releasing your python package!
See this article Christopher Sidebottom if you want more information about the poem: