Python

Share Your Code of Love PyPI

Okay. This is one is going to be awesome! 😀

In this post I am going to share how to share your package to PyPI.  Mine is up and live, it’s pyhoroscope . ❤

What 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. There are currently 48101 packages here.

 

So? What do I have to do with that?

  • Written something awesome?
  • Want others to pip install it?
  • Want to test your package?
  • Want to share your package, that you made with so much ❤ and some lines of Python.

Even if one bullet is in your mind, let me tell you the way to contribute your package to PyPI.

It’s fun. It’s cool. It’s awesome!

 

What I need?

  1. Your package.
  2. README
  3. .pypirc configuration file.
  4. Accounts on PyPI Live and PyPi Test.
  5. LICENSE.txt. I used this.
  6. setup.py
  7. requirements.txt. If your package needs something to be installed first.

 

Damn! I am confused. What are these?

This is how your work tree is going to look like,


your_package_name -version/ #Come'on you can name it anything!

    setup.py

    README.txt

    README.md #For GitHub may be

    requirements.txt

    CHANGES.txt #It's always good to have it.

    LICENSE.txt

    your_package_name/

        bob.py

        alice.py

        __init.py__

 

Now let me go ahead with what goes in which file

setup.py

This is the metadata about your library. My setup.py here.


from setuptools import setup  #I used setuptools, you can use dist.core

setup(
    name='<your_package_name>',
    version='<version>',
    description='<Some sort of description explaining what you are doing>',
    author='<Ooh! your_name here>',
    author_email='<your_email_id>',
    url='<url>',
    packages=['<your_package_name>'],
    install_requires=[
        "some_package == version", #package to install to make your package work.
    ]
)

README

You know this, right? Refer this for more.

 .pypirc Configuration File

This holds the information to authenticate you with PyPI. I hope you have created the account here and here. This should be in your home directory.


[distutils]  # this tells distutils what package indexes you can push to
index-servers =
    pypi  # the live PyPI
    test  # test PyPI

[test]  # authentication details for test PyPI
repository = https://testpypi.python.org/pypi
username = <your_user_name>
password = <your_password>

[pypi]  # authentication details for live PyPI
repository = https://pypi.python.org/pypi
username = <your_user_name>
password = <your_password>

 

LICENSE.txt

This file will contain whichever license you want your code to have. You can use MIT license. You can choose from here.

 

requirements.txt

This has the packages to be installed to make your package work. My requirements.txt looks like this :


beautifulsoup4==4.3.2

as I needed bs4 to be installed to make my package work. Here it is.

 

Your Package

Obviously!

 

Okay I am done with all these. Tell me how to upload?

Register your package against PyPI’s test server


python setup.py register -r test

#Mine gave me this
running register
running egg_info
creating horoscope.egg-info
writing requirements to horoscope.egg-info/requires.txt
writing horoscope.egg-info/PKG-INFO
writing top-level names to horoscope.egg-info/top_level.txt
writing dependency_links to horoscope.egg-info/dependency_links.txt
writing manifest file 'horoscope.egg-info/SOURCES.txt'
reading manifest file 'horoscope.egg-info/SOURCES.txt'
writing manifest file 'horoscope.egg-info/SOURCES.txt'
Registering horoscope to https://testpypi.python.org/pypi
Server response (200): OK

#Listing the stuff
tapasweni@Tapasweni:~/horoscope-0.1.0$ ls
CHANGES.txt  horoscope.egg-info  README   requirements.txt

horoscope  LICENSE.txt  README.md  setup.py

 

Upload it to testPyPI to test 


python setup.py sdist upload -r test

#Mine gave me this

running sdist
running egg_info
writing requirements to horoscope.egg-info/requires.txt
writing horoscope.egg-info/PKG-INFO
writing top-level names to horoscope.egg-info/top_level.txt
writing dependency_links to horoscope.egg-info/dependency_links.txt
reading manifest file 'horoscope.egg-info/SOURCES.txt'
writing manifest file 'horoscope.egg-info/SOURCES.txt'
creating horoscope-0.1.0
creating horoscope-0.1.0/horoscope
creating horoscope-0.1.0/horoscope.egg-info
making hard links in horoscope-0.1.0...
hard linking README -> horoscope-0.1.0
hard linking setup.py -> horoscope-0.1.0
hard linking horoscope/__init__.py -> horoscope-0.1.0/horoscope
hard linking horoscope/pyhoroscope.py -> horoscope-0.1.0/horoscope
hard linking horoscope.egg-info/PKG-INFO -> horoscope-0.1.0/horoscope.egg-info
hard linking horoscope.egg-info/SOURCES.txt -> horoscope-0.1.0/horoscope.egg-info
hard linking horoscope.egg-info/dependency_links.txt -> horoscope-0.1.0/horoscope.egg-info
hard linking horoscope.egg-info/requires.txt -> horoscope-0.1.0/horoscope.egg-info
hard linking horoscope.egg-info/top_level.txt -> horoscope-0.1.0/horoscope.egg-info
Writing horoscope-0.1.0/setup.cfg
creating dist
tar -cf dist/horoscope-0.1.0.tar horoscope-0.1.0
gzip -f9 dist/horoscope-0.1.0.tar
removing 'horoscope-0.1.0' (and everything under it)
running upload
Submitting dist/horoscope-0.1.0.tar.gz to https://testpypi.python.org/pypi
Server response (200): OK

 

To install package from testPyPI


pip install -i test <your_package_name>

 

If everything goes fine, then let’s put it in the PyPI live

 

Register your package against PyPI’s Live server


python setup.py register -r pypi

 

Upload it to PyPI Live 


python setup.py sdist upload -r pypi

 

Install

Now you can pip install it.


pip install your_package_name

 

You can go ahead and check your package here.

Congratulations! on your first successful publish on PyPI. 🙂

 

To add a new version, you can use the same steps. Read more, here.

 

 

 

Back to ❤ coding.

Tapasweni Pathak

 

 

Advertisements

One thought on “Share Your Code of Love PyPI

  1. Thanks for the lovely guide! If you’re interested in Python package distribution then take a look at Binstar (I’ve got no affiliation with them, just saying 🙂 ) which allows you to host public/private packages on PyPi and the Conda repos 🙂

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s