Publishing Your Package to PyPI

 

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.

The packages pushed to PyPI can be easily installed using pip.

To push a package to PyPI following are required:

  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.

This is how work tree should look like,


your_package_name -version/ 
    setup.py
    README.txt
    README.md
    requirements.txt
    CHANGES.txt
    LICENSE.txt
    your_package_name/
        bob.py
        alice.py
        __init.py__

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.
    ]
)

.pypirc configuration file

This holds the information to authenticate you with PyPI placed inside 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>

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.

Register your package to PyPI 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

then upload to the server:

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

It can be tested by installing the package from PyPI test server

pip install -i test <your_package_name>

If you testing works with package on PyPI test, the next steps are to register and upload it to PyPI live.


python setup.py register -r pypi
python setup.py sdist upload -r pypi
#Now you can pip install it.
pip install your_package_name

All packages can be found here.

 

One Reply to “Publishing Your Package to 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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s