How to Make a Python Script Pip-Installable

As Python developers, we’ve all used pip to install applications, but speaking to other Python developers, it’s not always clear how to make your own application/script pip-installable.

If you want to automatically publish your Python to PyPi, check out makepip

automation tip

Steps involved

  • Create a python script, or application
  • Add and configure a setup file
  • Build your new Pip Package Locally
  • Upload them both to the Python Package Index (PyPi)

So let’s run through these 4 steps to get you going.

Create a python script, or application

Python scripts can be big or small, simple or complex, but either way, we will end up with a python file. So let’s create one and just print out some text for a super simple demonstration.

print("Hello world")

You can really name this file anything, so we will call our script helloworld.py.

Add and configure a setup file

Within a pip-installable package exists a setup.py file. This is the central piece of creating, distributing and installing a module. These modules will be installed using the Distutils internal package.

So let’s create a simple setup.py file for our helloworldscript.

from setuptools import setup

setup(
  name="my-very-own-helloworld-app",
  version="1.0.0",
  scripts=["helloworld"]
)

A few things to note:

name is the name of your newly created PyPi package that will be installed.

version is how you will version your package each time someone installs or upgrades it.

scripts is the name of your script. Note that this is also the command people will using when calling your module.

How to build your new Pip Package Locally

At this stage we will package our script using the configurations given in our setup.py file. This can be done by running:

python setup.py sdist

This command will create a dist directory in the same parent directory, and will contain all the needed files for the pip distribution.

If you want to install it locally once this directory is created, you can test it by running:

python setup.py install

Not comes the time to make it available to the world!

Upload them both to the PyPi

At this stage, we will be using https://pypi.org/ , so if you don’t already have an account (free), then go register one.

You used to be able to do it from the command-line by typing python setup.py register, but I think those days are over.

In fact, if you do this step now you will get the following error:

Server response (410): Project pre-registration is no longer required or supported, upload your files instead.

So just go and register directly at https://pypi.org/account/register/ .

Once this is ready, you can login. Note that your login username is stored in a local file called ~/.pypirc.

This file will look something like this:

[distutils]
index-servers=pypi

[pypi]
repository = https://upload.pypi.org/legacy/
username = aogl

Remember to change your username to reflect your own one. The one in the example above is my own.

At this stage you can distribute your application with the following command:

python setup.py sdist upload

You can combine the commands above as follows:

python setup.py register sdist upload

Install your new package using Pip

Congratulations, your new package is now on PyPi and available for the world to consume!

Type pip install my-very-own-helloworld-app to install your new application.

Shortcuts

If you don’t want to have to do all this each time, there is a very useful package called <a href="https://pypi.org/project/makepip/" target="_blank" rel="noreferrer noopener nofollow">makepip</a> available for this very need and a writeup on how to automatically push to pip here .