Slide 1

Slide 1 text

Python Packaging from Init to Deploy

Slide 2

Slide 2 text

Slides & Notes http://daveops.com/pyohio2015

Slide 3

Slide 3 text

Dave Forgac [email protected] @tylerdave

Slide 4

Slide 4 text

A long time ago in a galaxy far, far away...

Slide 5

Slide 5 text

How to package? There should be one — and preferably only one — obvious way to do it.

Slide 6

Slide 6 text

Which is it? distutils setuptools distribute ?

Slide 7

Slide 7 text

Copy, replace

Slide 8

Slide 8 text

Fast forward ⏩

Slide 9

Slide 9 text

PyPA! PyPUG!

Slide 10

Slide 10 text

What did I learn?

Slide 11

Slide 11 text

Definitions

Slide 12

Slide 12 text

Module Saved Python code

Slide 13

Slide 13 text

(Import) Package Namespace (directory)

Slide 14

Slide 14 text

(Distribution) Package Shareable / installable

Slide 15

Slide 15 text

Source Distribution sdist

Slide 16

Slide 16 text

Built Distribution bdist_egg bdist_wheel

Slide 17

Slide 17 text

Wheel Universal Pure Python Platform

Slide 18

Slide 18 text

PyPI ≠

Slide 19

Slide 19 text

Components . ├── data │ └── data_file ├── DESCRIPTION.rst ├── MANIFEST.in ├── README.rst ├── sample │ ├── __init__.py │ └── package_data.dat ├── setup.cfg ├── setup.py └── tests ├── __init__.py └── test_simple.py

Slide 20

Slide 20 text

Code

Slide 21

Slide 21 text

setup.py

Slide 22

Slide 22 text

setup.cfg

Slide 23

Slide 23 text

MANIFEST.in

Slide 24

Slide 24 text

README.rst

Slide 25

Slide 25 text

DESCRIPTION.rst

Slide 26

Slide 26 text

setup.py (1) from setuptools import setup, find_packages setup( name='pyohio2015', version='0.1.0', # PEP440 description='Example package for PyOhio talk.', long_description='Displayed on PyPI project page.', url='https://github.com/tylerdave/PyOhio-2015-Example', author='Dave Forgac', author_email='[email protected]', …

Slide 27

Slide 27 text

setup.py (2) … license='MIT', classifiers=[ 'Development Status :: 3 - Alpha', 'Intended Audience :: Developers', 'License :: OSI Approved :: MIT License', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.3', 'Programming Language :: Python :: 3.4', ], …

Slide 28

Slide 28 text

setup.py (3) … keywords='example project pyohio', packages=find_packages(exclude=['docs', 'tests*'] install_requires=['requests'], package_data={ 'sample': ['package_data.dat'] } data_files=None …

Slide 29

Slide 29 text

setup.py (4) … # scripts= , entry_points={ 'console_scripts': [ 'hello=pyohio2015:say_hello', ], } )

Slide 30

Slide 30 text

More Components

Slide 31

Slide 31 text

LICENSE

Slide 32

Slide 32 text

Tests ./tests tox.ini

Slide 33

Slide 33 text

Docs ./docs

Slide 34

Slide 34 text

CI .travis.yml

Slide 35

Slide 35 text

requirements.txt

Slide 36

Slide 36 text

.gitignore

Slide 37

Slide 37 text

Other files (rst | md | txt) HISTORY or CHANGES or CHANGELOG CONTRIBUTING AUTHORS

Slide 38

Slide 38 text

Let's make a package!

Slide 39

Slide 39 text

Requirements pip install wheel pip install twine pip install tox

Slide 40

Slide 40 text

Repitition & Boilerplate We automate! We automate! We let someone else!

Slide 41

Slide 41 text

pip install cookiecutter

Slide 42

Slide 42 text

Make virtualenv (I use virtualenvwrapper) mkvirtualenv pyohio2015

Slide 43

Slide 43 text

Run cookiecutter dave@xps:$ cookiecutter https://github.com/tylerdave/cookiec python-package.git … full_name (default is "Dave Forgac")? email (default is "[email protected]")? github_username (default is "tylerdave")? project_name (default is "Python Boilerplate")? PyOhio 2015 repo_name (default is "boilerplate")? pyohio2015 project_short_description (default is "Python Boilerplate co all the boilerplate you need to create a Python package.")? package for PyOhio talk. release_date (default is "2015-08-02")? year (default is "2015")? version (default is "0.1.0")?

Slide 44

Slide 44 text

Git init cd pyohio2015 git init git add . git commit -m 'initial commit'

Slide 45

Slide 45 text

Add your code pyohio2015/cli.py: from __future__ import print_function def hello(): """ Returns a Hello, World! """ return("Hello, PyOhio!") def say_hello(): """ Prints Hello, World message """ print(hello())

Slide 46

Slide 46 text

Add tests tests/test_pyohio2015.py: import pyohio2015 class TestPyohio2015(unittest.TestCase): def setUp(self): self.hello_message = "Hello, PyOhio!" def test_prints_hello_pyohio(self): output = pyohio2015.hello() assert(output == self.hello_message)

Slide 47

Slide 47 text

Run tests tox … OK ___________________________ summary ________________________ py26: InterpreterNotFound: python2.6 py27: commands succeeded py33: InterpreterNotFound: python3.3 py34: commands succeeded

Slide 48

Slide 48 text

Commit git add . git commit -m "add hello world functionality"

Slide 49

Slide 49 text

Services

Slide 50

Slide 50 text

Create github repo

Slide 51

Slide 51 text

Add repo to Travis CI https://travis-ci.org/

Slide 52

Slide 52 text

Git push To [email protected]:tylerdave/PyOhio-2015-Example.git * [new branch] master -> master Branch master set up to track remote branch master from origin.

Slide 53

Slide 53 text

See builds succeeding!

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

PyPI

Slide 57

Slide 57 text

Save PyPI Settings $HOME/.pypirc [distutils] index-servers=pypi [pypi] repository = https://pypi.python.org/pypi username = password =

Slide 58

Slide 58 text

Build

Slide 59

Slide 59 text

Create distribution files ./setup.py sdist ./setup.py bdist_wheel

Slide 60

Slide 60 text

Sign distribution files gpg --detach-sign -a

Slide 61

Slide 61 text

Register Package Upload from pyohio2015.egg-info/PKG-INFO

Slide 62

Slide 62 text

Upload twine upload dist/*

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Iterate

Slide 65

Slide 65 text

Develop Mode ./setup.py develop or pip install -e .

Slide 66

Slide 66 text

Make changes

Slide 67

Slide 67 text

Update version in setup.py and [package]/__init__.py

Slide 68

Slide 68 text

Commit, tag, & push git commit -m "awesome new functionality!" git push origin master git tag v0.2.0 git push origin --tags

Slide 69

Slide 69 text

Build, sign, & upload

Slide 70

Slide 70 text

Versioneer

Slide 71

Slide 71 text

Caveats

Slide 72

Slide 72 text

Thank You! Talk & Contact Info: http://daveops.com/pyohio2015 [email protected] @tylerdave