Uploading Python Project On PyPi (2018)

PyPi, a.k.a Python Package Index is a repository of software for the Python programming language. It’s a great place to look for amazing Python libraries or tools that you can integrate in your next big project. Since you can search, you can also upload your own Python libraries on PyPi, and that too for free. Uploading projects on PyPi is easy-peasy.

If you have ever needed to install some third party Python library, you have definitely come around this command line :

pip install  <library_name>

What if, your project could also be available like that? It’ll be “cool”, right? Well, let’s see how it is  done. There are various blogs/tutorials out there, but recently PyPi has changed their API Endpoints and those blogs/tutorials are a little old now.

What We Will Be Doing :

1.) Signing Up for 2 accounts.

2.) Making  The .pypirc file.

3.) Making the setup.py

4.)  Publishing the project

First Things  First :

1.) Make an account here : PyPi Live Site

2.) Make an account  here as well  : PyPi Test Site

3.) Your Python Project (DUH!)

Getting Things Ready  :

Now, it is the time to make the “.pypirc” file. It’s a  file which holds  your PyPi account’s login information, so you won’t have to re-enter that info every time. Don’t worry, we won’t be sharing this file anywhere. So, you can just type in your login information in text format.

Making the .pypirc File

Now, this “.pypirc” file should reside in your  “HOME” directory.  Windows Users  can open command prompt and type in

echo %HOMEPATH%

or  open PowerShell and type

echo $HOME

and it’ll show you where your “Home” directory is and that is where we need this “.pypirc” file.

After finding out your $HOME directory, move to that directory and make this “.pypirc” file.

NOTE : Windows users can just make this file by opening a new Notepad and saving the file. Make sure you have the “encoding” set as “ANSI”, and “save as type” should be “all files”. (Check the screenshot below).

Now, paste this text inside that file  :

[distutils]
index-servers =
pypi
pypitest

[pypi]
repository: https://upload.pypi.org/legacy/
username: <usernamefor pypi live site>
password: <password for pypi live site>

[pypitest]
repository: https://test.pypi.org/legacy/
username: <usernamefor pypi test site>
password: <password for pypi test site>

Windows Users : Creating the file.

Do replace the <username> and <passwords> accordingly.

Making the Setup.py File

Go to your project’s directory and make sure your project’s structure is somewhat like this :

source_dir/
|-- my_python_library
|   |-- __init__.py
|   `-- FILES ....
|-- README.md
|-- setup.cfg
|-- setup.py

Contents of setup.py file could be like this :

from distutils.core import setup

setup(
name = 'my_python_library',
packages = ['my_python_library'],
version = 'version number',
description = 'description',
author = '',
author_email = '',
keywords = ['tag1', 'tag2'],
classifiers = [],
)

Replace the skeleton text with the actual  correct information of your library/package.

In this directory, you  need to make a “README.md” file as well. This file will contain information about your project. It’s like a “Read Me” file, that’ll describe what your project does and how it does. You know, like a basic introduction about your amazing library.

Uploading projects on PyPi :

So, we’re ready to push our project now. Before the update, PyPi needed you to register your project. Now, you don’t need to pre-register the application. You can just upload your project directly. So, let’s get your project on PyPi.

Open a command prompt/Terminal, navigate to the project’s directory and then type this command and press Enter :

python setup.py sdist upload -r pypitest

You should receive a message saying “Server Response  (200) :  OK”. If there’s any error, you’ll be able to see it and they’re pretty easy to understand.

Now, we will push the library to PyPi’s live site with this command (Make sure you’re in the project’s directory) :

python setup.py sdist upload -r pypi

That’s it! You’re done. If everything went good (which should), then you should now see your  project on PyPi website. Setting this up first time could be lengthy. But, you don’t have to make the .pyirc file again for a new project or updating the current project. Below are some screenshots  that you can refer to. I’ve uploaded one of my projects on PyPi, those screenshots refer to that project.

uploading projects on PyPi

If you wish,  you could check out the setup.py, setup.cfg files for my published project on Github.

Reference(s) :

1.) Official Documentation

2.) How to Host your Python Package on PyPI with GitHub

5 Replies to “Uploading Python Project On PyPi (2018)”

  1. Nice blog here! Also your site loads up fast! What web host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as quickly as yours lol

Leave a Reply

Your email address will not be published. Required fields are marked *