Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PyOhio 2015 - Python Packaging from Init to Deploy

PyOhio 2015 - Python Packaging from Init to Deploy

Python packaging really isn't that bad (anymore.) In this talk you'll learn how you can take your beautiful new Python code and share it with the world in a way that everyone benefits. I will cover tools and techniques you can use to get the boring stuff out of the way so you can focus on your code and deploy quickly, frequently, and consistently.

Dave Forgac

August 02, 2015
Tweet

More Decks by Dave Forgac

Other Decks in Technology

Transcript

  1. Python
    Packaging
    from Init to Deploy

    View Slide

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

    View Slide

  3. Dave Forgac
    [email protected]
    @tylerdave

    View Slide

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

    View Slide

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

    View Slide

  6. Which is it?
    distutils
    setuptools
    distribute
    ?

    View Slide

  7. Copy, replace

    View Slide

  8. Fast forward

    View Slide

  9. PyPA!
    PyPUG!

    View Slide

  10. What did I
    learn?

    View Slide

  11. Definitions

    View Slide

  12. Module
    Saved Python code

    View Slide

  13. (Import) Package
    Namespace (directory)

    View Slide

  14. (Distribution) Package
    Shareable / installable

    View Slide

  15. Source Distribution
    sdist

    View Slide

  16. Built Distribution
    bdist_egg
    bdist_wheel

    View Slide

  17. Wheel
    Universal
    Pure Python
    Platform

    View Slide

  18. PyPI

    View Slide

  19. 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

    View Slide

  20. Code

    View Slide

  21. setup.py

    View Slide

  22. setup.cfg

    View Slide

  23. MANIFEST.in

    View Slide

  24. README.rst

    View Slide

  25. DESCRIPTION.rst

    View Slide

  26. 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]',

    View Slide

  27. 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',
    ],

    View Slide

  28. 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

    View Slide

  29. setup.py (4)

    # scripts= ,
    entry_points={
    'console_scripts': [
    'hello=pyohio2015:say_hello',
    ],
    }
    )

    View Slide

  30. More
    Components

    View Slide

  31. LICENSE

    View Slide

  32. Tests
    ./tests
    tox.ini

    View Slide

  33. Docs
    ./docs

    View Slide

  34. CI
    .travis.yml

    View Slide

  35. requirements.txt

    View Slide

  36. .gitignore

    View Slide

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

    View Slide

  38. Let's make a
    package!

    View Slide

  39. Requirements
    pip install wheel
    pip install twine
    pip install tox

    View Slide

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

    View Slide

  41. pip install cookiecutter

    View Slide

  42. Make virtualenv
    (I use virtualenvwrapper)
    mkvirtualenv pyohio2015

    View Slide

  43. Run cookiecutter
    [email protected]:$ 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")?

    View Slide

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

    View Slide

  45. 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())

    View Slide

  46. 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)

    View Slide

  47. Run tests
    tox

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

    View Slide

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

    View Slide

  49. Services

    View Slide

  50. Create github repo

    View Slide

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

    View Slide

  52. 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.

    View Slide

  53. See builds succeeding!

    View Slide

  54. View Slide

  55. View Slide

  56. PyPI

    View Slide

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

    View Slide

  58. Build

    View Slide

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

    View Slide

  60. Sign distribution files
    gpg --detach-sign -a

    View Slide

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

    View Slide

  62. Upload
    twine upload dist/*

    View Slide

  63. View Slide

  64. Iterate

    View Slide

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

    View Slide

  66. Make changes

    View Slide

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

    View Slide

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

    View Slide

  69. Build, sign, & upload

    View Slide

  70. Versioneer

    View Slide

  71. Caveats

    View Slide

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

    View Slide