Elia Perantoni
Elia Perantoni

Reputation: 589

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

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?

Upvotes: 3

Views: 4663

Answers (4)

7yl4r
7yl4r

Reputation: 5348

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='[email protected]',
    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.

Upvotes: 0

yorodm
yorodm

Reputation: 4461

In the not so old days I used this guide to learn how to package and distribute my python code, then some good people created flit which allows me to do the whole process in three steps.

$pip install flit

Create my metadata file:

[metadata]
author=Some guy
[email protected]
home-page=https://github.com/someuser/somepackage
requires=requests
requires-python= >=3
description-file=README.rst
classifiers=Intended Audience :: Developers
    License :: OSI Approved :: BSD License
    Programming Language :: Python :: 3
    Topic :: Software Development :: Libraries :: Python Modules

Publish my package:

$pip flit publish

And done!!!

Upvotes: 7

Amey Kumar Samala
Amey Kumar Samala

Reputation: 944

here is the link to the setup file of pandas, where you can see how they perform checks for their dependencies, they might be platform specific or any third party package specific

Upvotes: 2

developer_hatch
developer_hatch

Reputation: 16224

According to the documentation here

setup.py

There is another type of dependency specification for Python libraries known as setup.py. Setup.py is a standard for distributing and installing Python libraries. If you're building a Python library, such as requests or underwear you must include setup.py so a dependency manager can correctly install both the library as well as additional dependencies for the library. There's still quite a bit of confusion in the Python community over the difference between requirements.txt and setup.py, so read this well written post for further clarification.

Also check this:

What is setup.py?

You can see this example to have an idea of how to make yours:

https://github.com/pypa/sampleproject/blob/master/setup.py

Also, there is a guide here:

https://pythonhosted.org/an_example_pypi_project/setuptools.html

Upvotes: 2

Related Questions