How to pack and distribute python program (.py source code) so that other developers can easily install all required dependencies?

Viewed 3643

I'm developing a Python application built with some packages I've installed with pip such as Flask, requests, PIL.

So how can I distribute my program so that other people can easily install every required dependency/package and simply make it work on every computer? Is setup.py what I'm looking for or not at all? If so, could you please explain what it does and provide an example setup.py that does what I'm trying to do?

PS: I've also got this little question: do I need to provide a __init__.py in the top level folder of my program or just in subdirectories?

4 Answers

Unfortunately, python packaging has a complicated history, and new tools are still emerging. My understanding is that the current "gold standard" is to use setup_tools to define a setup.py file. This file will allow others to install your project's source code using pip. If you want to be able to install with pip without explicitly downloading the source first, you will need to publish your project to pypi using python setup.py upload.

Below is the setup.py boilerplate I use as a starting point:

#!/usr/bin/env python
""" boilerplate for new project setup.py """

from setuptools import setup
import io

import projectname

def read(*filenames, **kwargs):
    encoding = kwargs.get('encoding', 'utf-8')
    sep = kwargs.get('sep', '\n')
    buf = []
    for filename in filenames:
        with io.open(filename, encoding=encoding) as f:
            buf.append(f.read())
    return sep.join(buf)

long_description = read('README.md') #, 'CHANGES.txt')

setup(name='projectname',
    version=projectname.__version__,
    description='short desc of projectname',
    long_description=long_description,
    author='Tylar Murray',
    author_email='code+projectname@tylar.info',
    url='https://github.com/7yl4r/projectname',

    tests_require=['nose'],
    install_requires=[
        'networkx'  # or whatever
    ],
    #cmdclass={'test': PyTest},

    packages=['projectname', 'OtherProjectProvidedPackage2']
)

NOTE: this template requires you to have a README.md and something like __version__="0.1.23" in your top-level __init__.py

Regarding your P.S. question: Technically you should open a new question for this but in short the answer is you should include both as covered here and in this answer particularly.

Related