Python Guide Documentation - Read the Docs

Python Guide Documentation

Release 0.0.1 Kenneth Reitz

Dec 21, 2018

Contents

1 Getting Started with Python

3

1.1 Picking a Python Interpreter (3 vs 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.2 Properly Installing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1.3 Installing Python 3 on Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

1.4 Installing Python 3 on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

1.5 Installing Python 3 on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.6 Installing Python 2 on Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

1.7 Installing Python 2 on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

1.8 Installing Python 2 on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

1.9 Pipenv & Virtual Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

1.10 Lower level: virtualenv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2 Python Development Environments

25

2.1 Your Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

2.2 Further Configuration of pip and Virtualenv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

3 Writing Great Python Code

35

3.1 Structuring Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

3.2 Code Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

3.3 Reading Great Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

3.4 Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

3.5 Testing Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

3.6 Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

3.7 Common Gotchas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

3.8 Choosing a License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

4 Scenario Guide for Python Applications

79

4.1 Network Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

4.2 Web Applications & Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

4.3 HTML Scraping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

4.4 Command-line Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

4.5 GUI Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

4.6 Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

4.7 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

4.8 Systems Administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

4.9 Continuous Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

4.10 Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

i

4.11 Scientific Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 4.12 Image Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 4.13 Data Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 4.14 XML parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 4.15 JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 4.16 Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 4.17 Machine Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 4.18 Interfacing with C/C++ Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

5 Shipping Great Python Code

135

5.1 Packaging Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

5.2 Freezing Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

6 Additional Notes

143

6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

6.2 The Community . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

6.3 Learning Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

6.4 Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

6.5 News . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156

6.6 Contribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

6.7 License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

6.8 The Guide Style Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

ii

Python Guide Documentation, Release 0.0.1

Greetings, Earthling! Welcome to The Hitchhiker's Guide to Python. This is a living, breathing guide. If you'd like to contribute, fork us on GitHub! This handcrafted guide exists to provide both novice and expert Python developers a best practice handbook to the installation, configuration, and usage of Python on a daily basis. This guide is opinionated in a way that is almost, but not quite, entirely unlike Python's official documentation. You won't find a list of every Python web framework available here. Rather, you'll find a nice concise list of highly recommended options.

Note: The use of Python 3 is highly preferred over Python 2. Consider upgrading your applications and infrastructure if you find yourself still using Python 2 in production today. If you are using Python 3, congratulations -- you are indeed a person of excellent taste. --Kenneth Reitz

Let's get started! But first, let's make sure you know where your towel is.

Contents

1

Python Guide Documentation, Release 0.0.1

2

Contents

1 CHAPTER

Getting Started with Python

New to Python? Let's properly setup up your Python environment:

1.1 Picking a Python Interpreter (3 vs 2)

3

Python Guide Documentation, Release 0.0.1

1.1.1 The State of Python (3 & 2)

When choosing a Python interpreter, one looming question is always present: "Should I choose Python 2 or Python 3"? The answer is a bit more subtle than one might think. The basic gist of the state of things is as follows:

1. Most production applications today use Python 2.7. 2. Python 3 is ready for the production deployment of applications today. 3. Python 2.7 will only receive necessary security updates until 20206. 4. The brand name "Python" encapsulates both Python 3 and Python 2.

1.1.2 Recommendations

Note: The use of Python 3 is highly preferred over Python 2. Consider upgrading your applications and infrastructure if you find yourself still using Python 2 in production today. If you are using Python 3, congratulations -- you are indeed a person of excellent taste. --Kenneth Reitz

I'll be blunt: ? Use Python 3 for new Python applications. ? If you're learning Python for the first time, familiarizing yourself with Python 2.7 will be very useful, but not more useful than learning Python 3. ? Learn both. They are both "Python". ? Software that is already built often depends on Python 2.7. ? If you are writing a new open source Python library, it's best to write it for both Python 2 and 3 simultaneously. Only supporting Python 3 for a new library you want to be widely adopted is a political statement and will alienate many of your users. This is not a problem -- slowly, over the next three years, this will become less the case.

1.1.3 So. . . . 3?

If you're choosing a Python interpreter to use, I recommend you use the newest Python 3.x, since every version brings new and improved standard library modules, security and bug fixes. Given such, only use Python 2 if you have a strong reason to, such as a pre-existing code-base, a Python 2 exclusive library, simplicity/familiarity, or, of course, you absolutely love and are inspired by Python 2. No harm in that. Check out Can I Use Python 3? to see if any software you're depending on will block your adoption of Python 3. Further Reading It is possible to write code that works on Python 2.6, 2.7, and Python 3. This ranges from trivial to hard depending upon the kind of software you are writing; if you're a beginner there are far more important things to worry about. Note that Python 2.6 is end-of-life upstream, so you shouldn't try to write 2.6-compatible code unless you're being paid specifically to do that.

6

4

Chapter 1. Getting Started with Python

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download