Packaging Your Pythons

A talk by @dirn

So you want to package your code

Where do you begin?

setup.py

from setuptools import setup

setup(
    name='nycpython',
    version='1.0.0',
    description='The official package of NYC Python',
    url='https://github.com/NYCPython/nycpython',
    author='Andy Dirnberger',
    author_email='andy@nycpython.com',
    license='BSD',
    packages=['nycpython'],
    zip_safe=False,
)

An aside about versions

PEP 386 (current)

PEP 440 (future)

Package layout

$ tree
.
|-- nycpython
|   |-- __init__.py
|-- setup.py

nycpython/__init__.py

"""The magic of NYC Python"""

__version__ = '1.0.0'


def make_the_awesome():
    return 'http://nycpython.com'
$ python
from nycpython import make_the_awesome
print(make_the_awesome)

An aside about modules

instead of

nycpython/__init__.py

and

packages=['nycpython']

we could have used

nycpython.py

and

pymodules=['nycpython']

Why didn't we?

Growth

That was easy, right?

But this is open source night

we can do better

README

LICENSE

$ tree
.
|-- LICENSE
|-- nycpython
|   |-- __init__.py
|-- README.rst
|-- setup.py
from setuptools import setup

from nycpython import __version__


def read_file(filename):
    with open(filename) as f:
        return f.read()

setup(
    name='nycpython',
    version=__version__,
    description='The official package of NYC Python',
    long_description=read_file('README.rst'),
    url='https://github.com/NYCPython/nycpython',
    author='Andy Dirnberger',
    author_email='andy@nycpython.com',
    license=read_file('LICENSE'),
    packages=['nycpython'],
    zip_safe=False,
    classifiers=[...],
)

Learn from my mistake

MANIFEST.in

include LICENSE README.rst

~ or ~

include *.rst LICENSE

$ tree
.
|-- LICENSE
|-- MANIFEST.in
|-- nycpython
|   |-- __init__.py
|-- README.rst
|-- setup.py

An aside about reStructured Text

Why not Markdown?

PyPI *

*

more on that later

Sphinx

Read the Docs

Publishing to PyPI

Now then, some cheese please, my good man.

Certainly, sir. What would you like?

Well, eh, how about a little red Leicester.

I'm, afraid we're fresh out of red Leicester, sir.

Register on the Python Package Index

Not so fast!!

Register on the test site

~/.pypirc

[disutils]
index-servers =
    pypi
    test

[pypi]
repository = http://pypi.python.org/pypi
username = dirn
password = <my super secret password goes here>

[test]
repository = https://testpypi.python.org/pypi
username = dirn
password = <another super secret password goes here>

Building and Uploading

$ python setup.py sdist

An aside about testing your build

$ rm -rf build-env
$ virtualenv build-env
$ build-env/bin/pip install --no-index dist/nycpython-1.0.0.tar.gz
...
$ build-env/bin/python
>>> import nycpython
>>> nycpython.__version__
'1.0.0'
$ python setup.py register -r test
$ python setup.py sdist upload -r test
$ python setup.py register -r pypi
$ python setup.py sdist upload -r pypi

Installation

$ pip install nycpython

An aside about the future

Wheel

Because 'newegg' was taken.

setup.cfg

[wheel]
universal = 1
$ pip install wheel
$ python setup.py bdist_wheel
$ rm -rf build-env
$ virtualenv build-env
$ build-env/bin/pip install --use-wheel --no-index \
    --find-links dist nycpython
...
$ build-env/bin/python
>>> import nycpython
>>> nycpython.__version__
'1.0.0'
$ python setup.py bdist_wheel upload -r test

and

$ python setup.py bdist_wheel upload -r pypi

Questions?

SpaceForward
Left, Down, Page DownNext slide
Right, Up, Page UpPrevious slide
POpen presenter console
HToggle this help