Creating My First PyPI Package
In my efforts to gulp the cool-aid on all things Python, this weekend I uploaded my first package to PyPI. What is PyPI you ask? When you use pip, the default repository used is PyPI. From the official website:
PyPI — the Python Package Index
The Python Package Index is a repository of software for the Python programming language.
The package I wrote is a simple and silly program that outputs a single random Taylor Swift lyric to the console. I called it tay_say. The technical part is easy, I usually find the hardest part of most projects is coming up with an idea.
The GitHub repository can be found here: https://github.com/DEV3L/python-tay-say.
In this blog post, I intend to cover the steps I took to make this simple package.
Follow Peter Downs:
How to submit a package to PyPI
I Google searched on how to upload a package to PyPI and found an awesome tutorial by Peter Downs: http://peterdowns.com/posts/first-time-with-pypi.html
This blog post takes a user from zero to one, in regards to PyPI! It is really thorough and a great resource. In a nutshell the post covers the following steps:
- Create PyPI Test and Production accounts
- Proper project layout
- setup.py construction
- Upload to PyPI
These steps get you an empty package up on PyPI.
Add Console Entry Point to setup.py
So missing from the above article is how to create an entry script for your package.
If you look at my example setup.py:
entry_points={
'console_scripts': [
'tay_say = tay_say:print_lyric'
]},
The format is pretty simple. It goes '<script name> = <path to module>:<function>'
.
My initial test file simply outputted the word 'test' to the console. The commit that contains these initial changes to setup.py and test function can be found here.
Final Steps
I incremented the package version in setup.py and uploaded the updated package to PyPI.
Now, as a result, anyone with Python installed can type 'pip install tay_say'. Although this is an oversimplified example, it opens the door to using pip to install internal or externally hosted package files.
Example install and usage from the command line:
~ pip install tay_say
~ tay_say
Get In Touch
We'd love to hear from you! Whether you have a question about our services, need a consultation, or just want to connect, our team is here to help. Reach out to us through the form, or contact us directly via social media.