Deployment

17_deployment

Module Installation & Deployment

PyPi is also known as the cheese shop. This is the Python Package Index, a public repository for Python.

You will need three files:

  • setup.py Installation Script
  • awesome.py Your Module
  • README.md Module Documentation
# File: setup.py
import setuptools

with open("README.md", "r") as f:
    long_description = f.read()

setuptools.setup(
    name="awesome",  # this is the project name on PyPi
    py_modules=["awesome"],  # this is the 'import' name
    author="One Awesome Developer",
    author_email="you@yourdomain.com",
    version="0.0.1",
    description="Awesome module with an awesome function!",
    long_description=long_description,
    long_description_content_type="text/markdown",
    classifiers=["Development Status :: 1 - Planning"],
    python_requires='>=3.6',
)
# File: awesome.py

def awesome_function(x):
    return x
# File: README.md
AWESOME MODULE

Text about this awesome project...
- elevator pitch
- development history
- other goodies

Installation with pip:

First navigate to the directory containing your module. Then open a terminal and enter the following command.

$ pip install .
...

This alone does not upload anything anywhere, it installs your module on your system. If you’re using a virtual environment, make sure you initialize that environment before attepting to install into it. You may need to specify pip3 not pip if you have pip installed to a Python2 instance somewhere on your system. This is common on Mac.

Once your module is installed – Python should be able to ‘see’ your module from anywhere on your system.

$ python3
>>> from awesome import awesome_function
>>> awesome_function(42)
42

After you install and test your module locally, you can build it for the masses. To build and upload to PyPi with setup & twine, enter the following commands. The first line below will build your module as a wheel. This the modern way to create a cross platform distribution. The second line below will instruct twine to upload your module to PyPi, you will need to have created an account for this next part to work. Register at PyPi.org. Twine will ask for your credentials.

$ python3 setup.py sdist bdist_wheel
...
$ twine upload dist/*
...

Now that our awesome module is built and hosted on PyPi we can install it pretty much anywhere, even on our friend’s computer – assuming they have Python3.

$ pip install awesome
...
$ python3
>>> from awesome import awesome_function
>>> awesome_function(42)
42