The H itchhiker’s G uide to P ython BEST PRACTICES FOR DEVELOPMENT Kenneth Reitz & Tanya Schlusser The Hitchhiker’s Guide to Python Best Practices for Development Kenneth Reitz and Tanya Schlusser BBeeiijjiinngg BBoossttoonn FFaarrnnhhaamm SSeebbaassttooppooll TTookkyyoo The Hitchhiker’s Guide to Python by Kenneth Reitz and Tanya Schlusser Copyright © 2016 Kenneth Reitz, Tanya Schlusser. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected]. Editor: Dawn Schanafelt Indexer: WordCo Indexing Services, Inc. Production Editor: Nicole Shelby, Nicholas Adams Interior Designer: David Futato Copyeditor: Jasmine Kwityn Cover Designer: Randy Comer Proofreader: Amanda Kersey Illustrator: Rebecca Demarest September 2016: First Edition Revision History for the First Edition 2016-08-26: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491933176 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. The Hitchhiker’s Guide to Python, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-93317-6 [LSI] Dedicated to you Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Part I. Getting Started 1. Picking an Interpreter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The State of Python 2 Versus Python 3 3 Recommendations 4 So…3? 4 Implementations 5 CPython 5 Stackless 5 PyPy 6 Jython 6 IronPython 6 PythonNet 6 Skulpt 7 MicroPython 7 2. Properly Installing Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Installing Python on Mac OS X 9 Setuptools and pip 11 virtualenv 11 Installing Python on Linux 12 Setuptools and pip 12 Development Tools 13 virtualenv 14 Installing Python on Windows 15 v Setuptools and pip 17 virtualenv 18 Commercial Python Redistributions 18 3. Your Development Environment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Text Editors 22 Sublime Text 23 Vim 23 Emacs 25 TextMate 26 Atom 26 Code 27 IDEs 27 PyCharm/IntelliJ IDEA 29 Aptana Studio 3/Eclipse + LiClipse + PyDev 29 WingIDE 30 Spyder 30 NINJA-IDE 30 Komodo IDE 31 Eric (the Eric Python IDE) 31 Visual Studio 32 Enhanced Interactive Tools 32 IDLE 32 IPython 33 bpython 33 Isolation Tools 33 Virtual Environments 34 pyenv 36 Autoenv 36 virtualenvwrapper 37 Buildout 38 Conda 38 Docker 39 Part II. Getting Down to Business 4. Writing Great Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Code Style 43 PEP 8 44 PEP 20 (a.k.a. The Zen of Python) 45 General Advice 46 vi | Table of Contents Conventions 52 Idioms 54 Common Gotchas 58 Structuring Your Project 61 Modules 61 Packages 65 Object-Oriented Programming 66 Decorators 67 Dynamic Typing 68 Mutable and Immutable Types 69 Vendorizing Dependencies 71 Testing Your Code 72 Testing Basics 74 Examples 76 Other Popular Tools 80 Documentation 82 Project Documentation 82 Project Publication 83 Docstring Versus Block Comments 84 Logging 84 Logging in a Library 85 Logging in an Application 86 Choosing a License 88 Upstream Licenses 88 Options 88 Licensing Resources 90 5. Reading Great Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Common Features 92 HowDoI 93 Reading a Single-File Script 93 Structure Examples from HowDoI 96 Style Examples from HowDoI 97 Diamond 99 Reading a Larger Application 100 Structure Examples from Diamond 105 Style Examples from Diamond 109 Tablib 112 Reading a Small Library 112 Structure Examples from Tablib 116 Style Examples from Tablib 124 Requests 126 Table of Contents | vii Reading a Larger Library 126 Structure Examples from Requests 130 Style Examples from Requests 135 Werkzeug 140 Reading Code in a Toolkit 141 Style Examples from Werkzeug 148 Structure Examples from Werkzeug 149 Flask 155 Reading Code in a Framework 156 Style Examples from Flask 162 Structure Examples from Flask 163 6. Shipping Great Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Useful Vocabulary and Concepts 168 Packaging Your Code 169 Conda 169 PyPI 170 Freezing Your Code 172 PyInstaller 174 cx_Freeze 176 py2app 177 py2exe 178 bbFreeze 178 Packaging for Linux-Built Distributions 179 Executable ZIP Files 180 Part III. Scenario Guide 7. User Interaction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Jupyter Notebooks 185 Command-Line Applications 186 GUI Applications 194 Widget Libraries 194 Game Development 200 Web Applications 200 Web Frameworks/Microframeworks 201 Web Template Engines 204 Web Deployment 209 8. Code Management and Improvement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Continuous Integration 213 viii | Table of Contents