setup.py - Package and install pythonic_sqlalchemy_query

To package

Create a source distribution, a built distribution, then upload both to pythonic_sqlalchemy_query at PyPI:

1
2
3
python -m pip install -U pip setuptools wheel twine
python setup.py sdist bdist_wheel
python -m twine upload dist/*

For development:

pip install -e .

Packaging script

Otherwise known as the evils of setup.py.

PyPA copied code

From PyPA’s sample setup.py, read long_description from a file. This code was last updated on 26-May-2015 based on this commit.

Always prefer setuptools over distutils

from setuptools import setup

To use a consistent encoding

from os import path

Imports for version parse code.

import os
import re

here = path.abspath(path.dirname(__file__))
 

Get the long description from the relevant file.

with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
    long_description = f.read()

The inclusion of a raw tag causes PyPI to not render the reST. Ouch. Remove it before uploading.

    long_description = re.sub('\.\. raw.*<\/iframe>', '', long_description, flags=re.DOTALL)
 

This code was copied from version parse code. See version in the call to setup below.

def read(*names, **kwargs):
    with open(
        os.path.join(os.path.dirname(__file__), *names),
        encoding=kwargs.get("encoding", "utf8")
    ) as fp:
        return fp.read()

def find_version(*file_paths):
    version_file = read(*file_paths)
    version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
                              version_file, re.M)
    if version_match:
        return version_match.group(1)
    raise RuntimeError("Unable to find version string.")
 

My code

setup(

This must comply with PEP 0426’s name requirements.

    name='pythonic_sqlalchemy_query',
 

Projects should comply with the version scheme specified in PEP440. I use this so that my Sphinx docs will have the same version number. There are a lot of alternatives in Single-sourcing the Project Version. While I like something simple, such as import pythonic_sqlalchemy_query then version=pythonic_sqlalchemy_query.__version__ here, this means any dependeninces of pythonic_sqlalchemy_query/__init__.py - Provide concise, Pythonic query syntax for SQLAlchemy will be requred to run setup, a bad thing. So, instead I read the file in setup.py and parse the version with a regex (see version parse code).

    version=find_version("pythonic_sqlalchemy_query/__init__.py"),

    description="Provide concise, Pythonic query syntax for SQLAlchemy",
    long_description=long_description,
 

The project’s main homepage.

    url='http://pythonic_sqlalchemy_query.readthedocs.io/en/latest/',

    author="Bryan A. Jones",
    author_email="bjones@ece.msstate.edu",

    license='GPLv3+',
 

These are taken from the full list.

    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
        'Operating System :: OS Independent',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Topic :: Database',
    ],

    keywords='SQLAlchemy, query helper',

    packages=['pythonic_sqlalchemy_query'],
 

List run-time dependencies here. These will be installed by pip when your project is installed. For an analysis of “install_requires” vs pip’s requirements files see: https://packaging.python.org/en/latest/requirements.html

    install_requires=([
        'SQLAlchemy',
    ]),
 

List additional groups of dependencies here (e.g. development dependencies). You can install these using the following syntax, for example:

$ pip install -e .[test]
    extras_require={
        'test': ['pytest', 'Flask-SQLAlchemy'],
    },
)