setuptools
Table of Contents
- Which distribution tool should I use?
- Python Packaging User Guide
setup.py
setup.py
(minimal)install_requires
vsrequirements.txt
scripts
vsconsole_scripts
- Make a python CLI through
entry_points
Which distribution tool should I use? discussion
So in conclusion, out of all these options, I would recommend Setuptools, unless your requirements are very basic and you only need Distutils. Setuptools works very well with Virtualenv and Pip, tools that I highly recommend. Virtualenv and Pip could both be considered official, as they're part of PyPA, and Python 3 now ships ensurepip (which helps you install pip on some systems).
Python Packaging User Guide tutorial
setup.py
reference
from setuptools import setup, find_packages
setup(
name='sample',
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='1.2.0',
description='A sample Python project',
long_description=long_description,
# The project's main homepage.
url='https://github.com/pypa/sampleproject',
# Author details
author='The Python Packaging Authority',
author_email='pypa-dev@googlegroups.com',
# Choose your license
license='MIT',
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers=[
# How mature is this project? Common values are
'Development Status :: 3 - Alpha',
# Indicate who your project is intended for
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
# Pick your license as you wish (should match "license" above)
'License :: OSI Approved :: MIT License',
# Specify the Python versions you support here. In particular, ensure
# that you indicate whether you support Python 2, Python 3 or both.
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
],
# What does your project relate to?
keywords='sample setuptools development',
# You can just specify the packages manually here if your project is
# simple. Or you can use find_packages().
packages=find_packages(exclude=['contrib', 'docs', 'tests']),
# Alternatively, if you want to distribute just a my_module.py, uncomment
# this:
# py_modules=["my_module"],
# List run-time dependencies here.
# These will be installed by pip when your project is installed.
install_requires=['peppercorn'],
# List additional groups of dependencies here (e.g. development
# dependencies). You can install these using the following syntax,
# for example:
# $ pip install -e .[dev,test]
extras_require={
'dev': ['check-manifest'],
'test': ['coverage'],
},
# If there are data files included in your packages that need to be
# installed, specify them here. If using Python 2.6 or less, then these
# have to be included in MANIFEST.in as well.
package_data={
'sample': ['package_data.dat'],
},
# Although 'package_data' is the preferred approach, in some case you may
# need to place data files outside of your packages. See:
data_files=[('my_data', ['data/data_file'])],
entry_points={
'console_scripts': [
'sample=sample.__main__:main',
],
},
)
setup.py
(minimal) reference
from setuptools import setup
setup(
name='yourapplication',
packages=['yourapplication'],
include_package_data=True,
install_requires=['flask'],
)
install_requires
vs requirements.txt
discussion
install_requires
is mostly required when the project is a python library. For just a python application, it seems okay to just have requirements.txt
.
scripts
vs console_scripts
discussion
funniest/
funniest/
__init__.py
...
setup.py
bin/
funniest-joke
funniest/
funniest/
__init__.py
command_line.py
...
setup.py
...
setup(
...
entry_points = {
'console_scripts': ['funniest-joke=funniest.command_line:main'],
}
...
)
Make a python CLI through entry_points
howto
To define a command named foo
which executes a function named cli
placed in package.__main__.py
:
The command created by this specification has additional load time at start because it needs to resolve the python environment. Normally this load is negligible, but if you use this command for a parallel batch scripts or so, it's better off to use python -m package
, and the like.