Mastering Python - Rick van Hattem - E-Book

Mastering Python E-Book

Rick van Hattem

0,0
34,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Master the art of writing beautiful and powerful Python by using all of the features that Python 3.5 offers

About This Book

  • Become familiar with the most important and advanced parts of the Python code style
  • Learn the trickier aspects of Python and put it in a structured context for deeper understanding of the language
  • Offers an expert's-eye overview of how these advanced tasks fit together in Python as a whole along with practical examples

Who This Book Is For

Almost anyone can learn to write working script and create high quality code but they might lack a structured understanding of what it means to be 'Pythonic'. If you are a Python programmer who wants to code efficiently by getting the syntax and usage of a few intricate Python techniques exactly right, this book is for you.

What You Will Learn

  • Create a virtualenv and start a new project
  • Understand how and when to use the functional programming paradigm
  • Get familiar with the different ways the decorators can be written in
  • Understand the power of generators and coroutines without digressing into lambda calculus
  • Create metaclasses and how it makes working with Python far easier
  • Generate HTML documentation out of documents and code using Sphinx
  • Learn how to track and optimize application performance, both memory and cpu
  • Use the multiprocessing library, not just locally but also across multiple machines
  • Get a basic understanding of packaging and creating your own libraries/applications

In Detail

Python is a dynamic programming language. It is known for its high readability and hence it is often the first language learned by new programmers. Python being multi-paradigm, it can be used to achieve the same thing in different ways and it is compatible across different platforms. Even if you find writing Python code easy, writing code that is efficient, easy to maintain, and reuse is not so straightforward.

This book is an authoritative guide that will help you learn new advanced methods in a clear and contextualised way. It starts off by creating a project-specific environment using venv, introducing you to different Pythonic syntax and common pitfalls before moving on to cover the functional features in Python. It covers how to create different decorators, generators, and metaclasses. It also introduces you to functools.wraps and coroutines and how they work. Later on you will learn to use asyncio module for asynchronous clients and servers. You will also get familiar with different testing systems such as py.test, doctest, and unittest, and debugging tools such as Python debugger and faulthandler. You will learn to optimize application performance so that it works efficiently across multiple machines and Python versions. Finally, it will teach you how to access C functions with a simple Python call. By the end of the book, you will be able to write more advanced scripts and take on bigger challenges.

Style and Approach

This book is a comprehensive guide that covers advanced features of the Python language, and communicate them with an authoritative understanding of the underlying rationale for how, when, and why to use them.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 532

Veröffentlichungsjahr: 2016

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Mastering Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
eBooks, discount offers, and more
Why subscribe?
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Getting Started – One Environment per Project
Creating a virtual Python environment using venv
Creating your first venv
venv arguments
Differences between virtualenv and venv
Bootstrapping pip using ensurepip
ensurepip usage
Manual pip install
Installing C/C++ packages
Debian and Ubuntu
Red Hat, CentOS, and Fedora
OS X
Windows
Summary
2. Pythonic Syntax, Common Pitfalls, and Style Guide
Code style – or what is Pythonic code?
Formatting strings – printf-style or str.format?
PEP20, the Zen of Python
Beautiful is better than ugly
Explicit is better than implicit
Simple is better than complex
Flat is better than nested
Sparse is better than dense
Readability counts
Practicality beats purity
Errors should never pass silently
In the face of ambiguity, refuse the temptation to guess
One obvious way to do it
Now is better than never
Hard to explain, easy to explain
Namespaces are one honking great idea
Conclusion
Explaining PEP8
Duck typing
Differences between value and identity comparisons
Loops
Maximum line length
Verifying code quality, pep8, pyflakes, and more
flake8
Pep8
pyflakes
McCabe
flake8
Pylint
Common pitfalls
Scope matters!
Function arguments
Class properties
Modifying variables in the global scope
Overwriting and/or creating extra built-ins
Modifying while iterating
Catching exceptions – differences between Python 2 and 3
Late binding – be careful with closures
Circular imports
Import collisions
Summary
3. Containers and Collections – Storing Data the Right Way
Time complexity – the big O notation
Core collections
list – a mutable list of items
dict – unsorted but a fast map of items
set – like a dict without values
tuple – the immutable list
Advanced collections
ChainMap – the list of dictionaries
counter – keeping track of the most occurring elements
deque – the double ended queue
defaultdict – dictionary with a default value
namedtuple – tuples with field names
enum – a group of constants
OrderedDict – a dictionary where the insertion order matters
heapq – the ordered list
bisect – the sorted list
Summary
4. Functional Programming – Readability Versus Brevity
Functional programming
list comprehensions
dict comprehensions
set comprehensions
lambda functions
The Y combinator
functools
partial – no need to repeat all arguments every time
reduce – combining pairs into a single result
Implementing a factorial function
Processing trees
itertools
accumulate – reduce with intermediate results
chain – combining multiple results
combinations – combinatorics in Python
permutations – combinations where the order matters
compress – selecting items using a list of Booleans
dropwhile/takewhile – selecting items using a function
count – infinite range with decimal steps
groupby – grouping your sorted iterable
islice – slicing any iterable
Summary
5. Decorators – Enabling Code Reuse by Decorating
Decorating functions
Why functools.wraps is important
How are decorators useful?
Memoization using decorators
Decorators with (optional) arguments
Creating decorators using classes
Decorating class functions
Skipping the instance – classmethod and staticmethod
Properties – smart descriptor usage
Decorating classes
Singletons – classes with a single instance
Total ordering – sortable classes the easy way
Useful decorators
Single dispatch – polymorphism in Python
Contextmanager, with statements made easy
Validation, type checks, and conversions
Useless warnings – how to ignore them
Summary
6. Generators and Coroutines – Infinity, One Step at a Time
What are generators?
Advantages and disadvantages of generators
Pipelines – an effective use of generators
tee – using an output multiple times
Generating from generators
Context managers
Coroutines
A basic example
Priming
Closing and throwing exceptions
Bidirectional pipelines
Using the state
Summary
7. Async IO – Multithreading without Threads
Introducing the asyncio library
The async and await statements
Python 3.4
Python 3.5
Choosing between the 3.4 and 3.5 syntax
A simple example of single-threaded parallel processing
Concepts of asyncio
Futures and tasks
Event loops
Event loop implementations
Event loop policies
Event loop usage
Processes
Asynchronous servers and clients
Basic echo server
Summary
8. Metaclasses – Making Classes (Not Instances) Smarter
Dynamically creating classes
A basic metaclass
Arguments to metaclasses
Accessing metaclass attributes through classes
Abstract classes using collections.abc
Internal workings of the abstract classes
Custom type checks
Using abc.ABC before Python 3.4
Automatically registering a plugin system
Importing plugins on-demand
Importing plugins through configuration
Importing plugins through the file system
Order of operations when instantiating classes
Finding the metaclass
Preparing the namespace
Executing the class body
Creating the class object (not instance)
Executing the class decorators
Creating the class instance
Example
Storing class attributes in definition order
The classic solution without metaclasses
Using metaclasses to get a sorted namespace
Summary
9. Documentation – How to Use Sphinx and reStructuredText
The reStructuredText syntax
Getting started with reStructuredText
Inline markup
Headers
Lists
Enumerated list
Bulleted list
Option list
Definition list
Nested lists
Links, references, and labels
Images
Substitutions
Blocks, code, math, comments, and quotes
Conclusion
The Sphinx documentation generator
Getting started with Sphinx
Using sphinx-quickstart
Using sphinx-apidoc
Sphinx directives
The table of contents tree directive (toctree)
Autodoc, documenting Python modules, classes, and functions
Sphinx roles
Documenting code
Documenting a class with the Sphinx style
Documenting a class with the Google style
Documenting a class with the NumPy style
Which style to choose
Summary
10. Testing and Logging – Preparing for Bugs
Using examples as tests with doctest
A simple doctest example
Writing doctests
Testing with pure documentation
The doctest flags
True and False versus 1 and 0
Normalizing whitespace
Ellipsis
Doctest quirks
Testing dictionaries
Testing floating-point numbers
Times and durations
Testing with py.test
The difference between the unittest and py.test output
The difference between unittest and py.test tests
Simplifying assertions
Parameterizing tests
Automatic arguments using fixtures
Cache
Custom fixtures
Print statements and logging
Plugins
pytest-cov
pytest-pep8 and pytest-flakes
Configuring plugins
Mock objects
Using unittest.mock
Using py.test monkeypatch
Logging
Configuration
Basic logging configuration
Dictionary configuration
JSON configuration
Ini file configuration
The network configuration
Logger
Usage
Summary
11. Debugging – Solving the Bugs
Non-interactive debugging
Inspecting your script using trace
Debugging using logging
Showing call stack without exceptions
Debugging asyncio
Handling crashes using faulthandler
Interactive debugging
Console on demand
Debugging using pdb
Breakpoints
Catching exceptions
Commands
Debugging using ipdb
Other debuggers
Debugging services
Summary
12. Performance – Tracking and Reducing Your Memory and CPU Usage
What is performance?
Timeit – comparing code snippet performance
cProfile – finding the slowest components
First profiling run
Calibrating your profiler
Selective profiling using decorators
Using profile statistics
Line profiler
Improving performance
Using the right algorithm
Global interpreter lock
Try versus if
Lists versus generators
String concatenation
Addition versus generators
Map versus generators and list comprehensions
Caching
Lazy imports
Using optimized libraries
Just-in-time compiling
Converting parts of your code to C
Memory usage
Tracemalloc
Memory profiler
Memory leaks
Reducing memory usage
Generators versus lists
Recreating collections versus removing items
Using slots
Performance monitoring
Summary
13. Multiprocessing – When a Single CPU Core Is Not Enough
Multithreading versus multiprocessing
Hyper-threading versus physical CPU cores
Creating a pool of workers
Sharing data between processes
Remote processes
Distributed processing using multiprocessing
Distributed processing using IPyparallel
ipython_config.py
ipython_kernel_config.py
ipcontroller_config.py
ipengine_config.py
ipcluster_config.py
Summary
14. Extensions in C/C++, System Calls, and C/C++ Libraries
Introduction
Do you need C/C++ modules?
Windows
OS X
Linux/Unix
Calling C/C++ with ctypes
Platform-specific libraries
Windows
Linux/Unix
OS X
Making it easy
Calling functions and native types
Complex data structures
Arrays
Gotchas with memory management
CFFI
Complex data structures
Arrays
ABI or API?
CFFI or ctypes?
Native C/C++ extensions
A basic example
C is not Python – size matters
The example explained
static
PyObject*
Parsing arguments
C is not Python – errors are silent or lethal
Calling Python from C – handling complex types
Summary
15. Packaging – Creating Your Own Libraries or Applications
Installing packages
Setup parameters
Packages
Entry points
Creating global commands
Custom setup.py commands
Package data
Testing packages
Unittest
py.test
Nosetests
C/C++ extensions
Regular extensions
Cython extensions
Wheels – the new eggs
Distributing to the Python Package Index
Summary
Index

Mastering Python

Mastering Python

Copyright © 2016 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: April 2016

Production reference: 1270416

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78528-972-9

www.packtpub.com

Credits

Author

Rick van Hattem

Reviewers

Randall Degges

Dave de Fijter

I. de Hoogt

Commissioning Editor

Sarah Crofton

Acquisition Editor

Reshma Raman

Content Development Editor

Arun Nadar

Technical Editors

Ryan Kochery

Tanmayee Patil

Copy Editor

Vikrant Phadke

Project Coordinator

Suzanne Coutinho

Proofreader

Safis Editing

Indexer

Mariammal Chettiyar

Production Coordinator

Nilesh Mohite

Cover Work

Nilesh Mohite

About the Author

Rick van Hattem is an experienced programmer, entrepreneur, and software/database architect with over 20 years of programming experience, including 15 with Python. Additionally, he has a lot of experience with high-performance architectures featuring large amounts of concurrent users and/or data.

Rick has founded several start-ups and has done consulting for many companies, including a few Y Combinator start-ups and several large companies. One of the startups he founded, Fashiolista.com, is one of the largest social networks for fashion in the world, featuring millions of users and the performance challenges to accompany those.

Rick was one of the reviewers on the book PostgreSQL Server Programming, Packt Publishing.

Thanks to my family, in particular Marloes, who supported me every step of the way; and my mother and sister, who have always been there for me.

About the Reviewers

Randall Degges is a happy programmer, speaker, author, and amateur bodybuilder living in California.

Growing up in Los Angeles, he was intensely interested in building command-line programs and writing quality software. His love of programming eventually propelled him into a successful career in software development.

Randall has been a life-long open source developer and has contributed to hundreds of popular projects in Python, Node.js, and Go. He's also the author of several popular libraries, which you can find on his public GitHub account at https://github.com/rdegges.

At 23, he cofounded an extremely popular API service in the telephony industry: OpenCNAM (https://www.opencnam.com). At 25, he joined Stormpath (https://stormpath.com) as the head of developer evangelism, whereby he writes open source security libraries full time and travels the world giving technical talks about building secure software.

In his free time, Randall writes and edits technical books, runs a security podcast called Stormcast (https://www.stormca.st), posts blogs on his personal website (https://www.rdegges.com), and tries to spend time with his high-school sweetheart, Samantha.

Dave de Fijter is a Python developer from the Netherlands. He always knew he would end up "doing something with computers." At a young age, he went to the library to read books about them even though he had no computer at that time. This obsession never really ended. In 2001, aged 14, he started his first part-time job, creating dynamic websites in PHP for a local web development company, and there he found his calling.

In 2007, he finished his bachelor's degree in ICT while already working full time as a PHP developer for over a year. In 2008, he switched from PHP to Python and Django for web development and loved this new technology stack so much that he never looked back.

After working as a Python developer for various start-ups and established companies, Dave used this experience to start his own business called Indentity (https://indentity.nl) in 2010, focusing on Python/Django development and advice. Up until now, he runs this company and mainly spends his time helping out start-ups with designing and building technologically advanced web applications from the ground up as an interim CTO/technical cofounder.

I. de Hoogt, with some basic experience wrought from university assignments in the field of modeling of multi-phase flows, got himself started in software development. His main experience in programming in Python stems from an internship at a company dealing in 3D printing software, where a package resulting in optimized object orientation and guaranteed mathematical mesh validity was created.

Other projects that he's been involved with have dealt with control systems such as self-parking cars, multi-legged robots, and quadcopters, but his current job is in the field of data analysis.

www.PacktPub.com

eBooks, discount offers, and more

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

Why subscribe?

Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

Preface

Python is a language that is easy to learn and both powerful and convenient from the start. Mastering Python, however, is a completely different question.

Every programming problem you will encounter has at least several possible solutions and/or paradigms to apply within the vast possibilities of Python. This book will not only illustrate a range of different and new techniques but also explain where and when a method should be applied.

This book is not a beginner's guide to Python 3. It is a book that can teach you about the more advanced techniques possible within Python. Specifically targeting Python 3.5 and up, it also demonstrates several Python 3.5-only features such as async def and await statements.

As a Python programmer with many years of experience, I will attempt to rationalize the choices made in this book with relevant background information. These rationalizations are in no way strict guidelines, however. Several of these cases boil down to personal style in the end. Just know that they stem from experience and are, in many cases, the solutions recommended by the Python community.

Some of the references in this book might not be obvious to you if you are not a fan of Monty Python. This book extensively uses spam and eggs instead of foo and bar in code samples. To provide some background information, I recommend watching the "Spam" sketch by Monty Python. It is positively silly!

What this book covers

Chapter 1, Getting Started – One Environment per Project, introduces virtual Python environments using virtualenv or venv to isolate the packages in your Python projects.

Chapter 2, Pythonic Syntax, Common Pitfalls, and Style Guide, explains what Pythonic code is and how to write code that is Pythonic and adheres to the Python philosophy.

Chapter 3, Containers and Collections – Storing Data the Right Way, is where we use the many containers and collections bundled with Python to create code that is fast and readable.

Chapter 4, Functional Programming – Readability Versus Brevity, covers functional programming techniques such as list/dict/set comprehensions and lambda statements that are available in Python. Additionally, it illustrates their similarities with the mathematical principles involved.

Chapter 5, Decorators – Enabling Code Reuse by Decorating, explains not only how to create your own function/class decorators, but also how internal decorators such as property, staticmethod, and classmethod work.

Chapter 6, Generators and Coroutines – Infinity, One Step at a Time, shows how generators and coroutines can be used to lazily evaluate structures of infinite size.

Chapter 7, Async IO – Multithreading without Threads, demonstrates the usage of asynchronous functions using async def and await so that external resources no longer stall your Python processes.

Chapter 8, Metaclasses – Making Classes (Not Instances) Smarter, goes deeper into the creation of classes and how class behavior can be completely modified.

Chapter 9, Documentation – How to Use Sphinx and reStructuredText, shows how you can make Sphinx automatically document your code with very little effort. Additionally, it shows how the Napoleon syntax can be used to document function arguments in a way that is legible both in the code and the documentation.

Chapter 10, Testing and Logging – Preparing for Bugs, explains how code can be tested and how logging can be added to enable easy debugging in case bugs occur at a later time.

Chapter 11, Debugging – Solving the Bugs, demonstrates several methods of hunting down bugs with the use of tracing, logging, and interactive debugging.

Chapter 12, Performance – Tracking and Reducing Your Memory and CPU Usage, shows several methods of measuring and improving CPU and memory usage.

Chapter 13, Multiprocessing – When a Single CPU Core Is Not Enough, illustrates that the multiprocessing library can be used to execute your code, not just on multiple processors but even on multiple machines.

Chapter 14, Extensions in C/C++, System Calls, and C/C++ Libraries, covers the calling of C/C++ functions for both interoperability and performance using Ctypes, CFFI, and native C/C++.

Chapter 15, Packaging – Creating Your Own Libraries or Applications, demonstrates the usage of setuptools and setup.py to build and deploy packages on the Python Package Index (PyPI).

What you need for this book

The only hard requirement for this book is a Python interpreter. A Python 3.5 or newer interpreter is recommended, but many of the code examples will function in older Python versions, such as 2.7, with a simple from __future__ import print_statement added at the top of the file.

Additionally, Chapter 14, Extensions in C/C++, System Calls, and C/C++ Libraries requires a C/C++ compiler, such as GCC, Visual Studio, or XCode. A Linux machine is by far the easiest to execute the C/C++ examples, but these should function on Windows and OS X machines without too much effort as well.

Who this book is for

If you are beyond the absolute Python beginner level, then this book is for you. Even if you are already an expert Python programmer, I guarantee that you will find some useful techniques and insights in this book.

At the very least, it will allow Python 2 programmers to learn a lot more about the new features introduced in Python 3, and specifically Python 3.5.

Basic proficiency in Python is required as the installation of Python interpreters and the basic Python syntax are not covered.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

Log in or register to our website using your e-mail address and password.Hover the mouse pointer on the SUPPORT tab at the top.Click on Code Downloads & Errata.Enter the name of the book in the Search box.Select the book for which you're looking to download the code files.Choose from the drop-down menu where you purchased this book from.Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Also, the code for the book is hosted on GitHub at https://github.com/mastering-python/code

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR / 7-Zip for WindowsZipeg / iZip / UnRarX for Mac7-Zip / PeaZip for Linux

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.

Chapter 1. Getting Started – One Environment per Project

There is one aspect of the Python philosophy that always has been, and always will be, the most important in the entire language—readability, or Pythonic code. This book will help you master writing Python the way it was meant to be: readable, beautiful, explicit, and as simple as possible. In short, it will be Pythonic code. That is not to say that complicated subjects will not be covered. Naturally, they will, but whenever the philosophy of Python is at stake, you will be warned when and where the technique is justified.

Most of the code within this book will function on both Python 2 and Python 3, but the main target is Python 3. There are three reasons for doing this:

Python 3 was released in 2008, which is a very long time in the rapidly changing software world. It's not a new thing anymore, it's stable, it's usable, and, most importantly, it's the future.Development for Python 2 effectively stopped in 2009. Certain features have been backported from Python 3 to Python 2, but any new development will be for Python 3 first.Python 3 has become mature. While I have to admit that Python 3.2 and older versions still had a few small issues that made it hard to write code that functions on both Python 2 and 3, Python 3.3 did improve greatly in that aspect, and I consider it mature. This is evidenced by the marginally modified syntax in Python 3.4 and 3.5 and a lot of very useful features, which are covered in this book.

To summarize, Python 3 is an improvement over Python 2. I have been a skeptic for a very long time myself, but I do not see any reason not to use Python 3 for new projects, and even porting existing projects to Python 3 is generally possible with only minor changes. With cool new features such as async with in Python 3.5, you will want to upgrade just to try it.

This first chapter will show you how to properly set up an environment, create a new isolated environment, and make sure you get similar results when running the same code on different machines. Most Python programmers are already using virtualenv to create virtual Python environments, but the venv command, introduced in Python 3.3, is a very nice alternative. It is essentially a clone of the virtualenv package but is slightly simpler and bundled with Python. While its usage is mostly analogous to virtualenv, there are a few changes that are interesting to know.

Secondly, we will discuss the pip command. The pip command is automatically installed when using venv through the ensurepip package, a package introduced in Python 3.4. This package automatically bootstraps pip into an existing Python library while maintaining independent versions of Python and pip. Before Python 3.4, venv came without pip and had to be installed manually.

Finally, we will discuss how packages created with distutils can be installed. While pure Python packages are generally easy to install, it can get challenging when C modules are involved.

In this chapter, the following topics are covered:

Creating a virtual Python environment using venvBootstrapping pip using ensurepipInstalling packages based on distutils (C/C++) with pip

Creating a virtual Python environment using venv

Most Python programmers are already be familiar with venv or virtualenv, but even if you're not, it's never too late to start using it. The venv module is designed to isolate your Python environments so that you can install packages specific to your current project without polluting your global namespace. For example, having a filename such as sys.py in your current directory can seriously break your code if you expect to have the standard Python sys library—your local sys libraries will be imported before the global one, effectively hiding the system library. In addition, because the packages are installed locally, you don't need system (root/administrator) access to install them.

The result is that you can make sure you have exactly the same version of a package on both your local development machine and production machines without interfering with other packages. For example, there are many Django packages around that require specific versions of the Django project. Using venv, you can easily install Django 1.4 for project A and Django 1.8 for project B without them ever knowing that there are different versions installed in other environments. By default, the environments are even configured in such a way that the global packages are not visible. The benefit of this is that to get an exact list of all installed packages within the environment, simply a pip freeze will suffice. The downside is that some of the heavier packages (for example, numpy) will have to be installed in every separate environment. Needless to say, which choice is the best for your project depends on the project. For most projects, I would keep the default setting of not having the global packages, but when messing around with projects that have lots of C/C++ extensions, it would be convenient to simply enable the global site packages. The reason is simple; if you do not have a compiler available, installing the package locally can be difficult, while the global install has an executable for Windows or an installable package for Linux/Unix available.

Note

The venv module (https://docs.python.org/3/library/venv.html) can be seen as a slightly simplified version of the virtualenv tool (https://virtualenv.pypa.io/), which has been bundled with Python since version 3.3 (refer to PEP 0405 -- Python Virtual Environments: https://www.python.org/dev/peps/pep-0405/).

The virtualenv package can generally be used as a drop-in replacement for venv, which is especially relevant for older Python versions (below 3.3) that do not come bundled with venv.

Creating your first venv

Creating an environment is quite easy. The basic command comes down to pyvenv PATH_TO_THE_NEW_VIRTUAL_ENVIRONMENT, so let's give it a try. Note that this command works on Linux, Unix, and Mac; the Windows command will follow shortly:

# pyvenv test_venv# . ./test_venv/bin/activate(test_venv) #

Note

Some Ubuntu releases (notably 14.04 LTS) maim the Python installation by not including the full pyvenv package with ensurepip. The standard workaround is to call pyvenv --without-pip test_env, which requires a manual pip installation through the get_pip.py file available on the pip home page.

This creates an environment called test_venv, and the second line activates the environment.

On Windows, everything is slightly different but similar overall. By default, the pyvenv command won't be in your PATH, so running the command is slightly different. The three options are as follows:

Add the Python\Tools\Scripts\ directory to your PATHRun the module:
python -m venv test_venv
Run the script directly:
python Python\Tools\Scripts\pyvenv.py test_venv

For convenience, I would recommend that you add the Scripts directory to your PATH anyhow, since many other applications/scripts (such as pip) will be installed there as well.

Here is the full example for Windows:

C:\envs>python -m venv test_venvC:\envs>test_venv\Scripts\activate.bat(test_venv) C:\envs>

Tip

When using Windows PowerShell, the environment can be activated by using test_venv\Scripts\Activate.ps1 instead. Note that you really do need backslashes here.

venv arguments

So far, we have just created a plain and regular venv, but there are a few, really useful flags for customizing your venv specifically to your needs.

First, let's look at the venv help:

Parameter

Description

--system-site-packages

It gives the virtual environment access to the system-site-packages directory

--symlinks

Try to use symlinks rather than copies when symlinks are not the default for the platform

--copies

Try to use copies rather than symlinks even when symlinks are the default for the platform

--clear

Delete the contents of the environment directory, if it exists, before environment creation

--upgrade

Upgrade the environment directory to use this version of Python, assuming that Python has been upgraded in-place

--without-pip

This skips installing or upgrading pip in the virtual environment (pip is bootstrapped by default)

The most important argument to note is --system-site-packages, which enables the global site packages within the environment. This means that if you have a package installed in your global Python version, it will be available within your environment as well. However, if you try to update it to a different version, it will be installed locally. Whenever possible, I would recommend disabling the --system-site-packages flag because it gives you a simple environment without too many variables. A simple update of the system packages could break your virtual environment otherwise, but worse, there is no way to know which packages are needed locally and which ones are just installed for other purposes.

To enable this for an existing environment, you can simply run the environment creation command again, but this time adding the --system-site-packages flag to enable the global site packages.

To disable it again, you can simply run the environment creation command without the flag. This will keep the locally (within the environment) installed packages available but will remove the global packages from your Python scope.

Tip

When using virtualenvwrapper, this can also be done with the toggleglobalsitepackages command from within the activated environment.

The --symlinks and --copies arguments can generally be ignored, but it is important to know the difference. These arguments decide whether the files will be copied from the base python directory or whether they will be symlinked.

Note

Symlinks are a Linux/Unix/Mac thing; instead of copying a file it creates a symbolic link that tells the system where to find the actual file.

By default, venv will try to symlink the files, and if that fails, it will fall back to copying. Since Windows Vista and Python 3.2, this is also supported on Windows, so unless you're using a very old system, you will most likely be using symlinks in your environment. The benefit of symlinks is that it saves disk space and stays in sync with your Python installation. The downside is that if your system's Python version undergoes an upgrade, it can break the packages installed within your environment, but that can easily be fixed by reinstalling the packages using pip.

Finally, the --upgrade argument is useful if your system Python version has been upgraded in-place. The most common use case for this argument is for repairing broken environments after upgrading the system Python with a copied (as opposed to symlinked) environment.

Differences between virtualenv and venv

Since the venv module is essentially a simpler version of virtualenv, they are mostly the same, but some things are different. Also, since virtualenv is a package that is distributed separately from Python, it does have some advantages.

The following are the advantages of venv over virtualenv:

venv is distributed with Python 3.3 and above, so no separate install is neededvenv is simple and straightforward with no features besides the bare necessities

Advantages of virtualenv over venv:

virtualenv is distributed outside of Python, so it can be updated separately.virtualenv works on old Python versions, but Python 2.6 or a higher version is recommended. However, Python 2.5 support is possible with older versions (1.9.x or lower).It supports convenient wrappers, such as virtualenvwrapper (http://virtualenvwrapper.readthedocs.org/)

In short, if venv is enough for you, use it. If you are using an old Python version or want some extra convenience, such as virtualenvwrapper, use virtualenv instead. Both projects essentially do the same thing, and efforts have been made to easily switch between them. The biggest and most significant difference between the two is the wide variety of Python versions that virtualenv supports.

Bootstrapping pip using ensurepip

Slowly, the pip package manager has been replacing easy_install since its introduction in 2008. Since Python 3.4, it has even become the default and is bundled with Python. Since Python 3.4 onward, it is installed by default within both the regular Python environment and that of pyvenv; before that, a manual install is required. To automatically install pip in Python 3.4 and above, the ensurepip library is used. This is a library that handles automatic installation and/or upgrades of pip, so it is at least as recent as the one bundled with ensurepip.

ensurepip usage

The usage of ensurepip is fairly straightforward. Just run python -m ensurepip to guarantee a pip version or python -m ensurepip --upgrade to make sure that pip will be at least the version that is bundled with ensurepip.

In addition to installing the regular pip shortcut, this will also install the pipX and pipX.Y links, which allow you to select a specific Python version. When using Python 2 and Python 3 simultaneously, this allows you to install packages within Python 2 and Python 3 with pip2 and pip3, respectively. This means that if you use python -m ensurepip on Python 3.5 you will get pip, pip3, and pip3.5 commands installed in your environment.

Manual pip install

The ensurepip package is great if you are using Python 3.4 or above. Below that, however, you need to install pip manually. Actually, this is surprisingly easy. It involves just two steps:

Download the get-pip.py file: https://bootstrap.pypa.io/get-pip.py.Execute the get-pip.py file: python get-pip.py.

Tip

If the ensurepip command fails due to permission errors, it can be useful to supply the --user argument. This allows you to install pip inside the user specific site packages directory, so root/admin access is not required.

Installing C/C++ packages

Most Python packages are purely Python and blissfully easy to install, just as a simple pip install packagename does the trick. However, there are cases where compilation is involved and installation goes from a simple pip install to searching for hours to see which dependencies are needed to install a certain package.

The specific error message will differ as per the project and environment, but there is a common pattern in these errors, and understanding what you are looking at can help a lot when searching for a solution.

For example, when installing pillow on a standard Ubuntu machine, you'll get a few pages full of errors, warnings, and other messages that end like this:

x86_64-linux-gnu-gcc: error: build/temp.linux-x86_64-3.4/libImaging/Jpeg2KDecode.o: No such file or directory x86_64-linux-gnu-gcc: error: build/temp.linux-x86_64-3.4/libImaging/Jpeg2KEncode.o: No such file or directory x86_64-linux-gnu-gcc: error: build/temp.linux-x86_64-3.4/libImaging/BoxBlur.o: No such file or directory error: command 'x86_64-linux-gnu-gcc' failed with exit status 1 ----------------------------------------Command "python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-_f0ryusw/pillow/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-kmmobum2-record/install-record.txt --single-version-externally-managed --compile --install-headers include/site/python3.4/pillow" failed with error code 1 in /tmp/pip-build-_f0ryusw/pillow

Upon seeing messages like these, you might be tempted to search for one of the lines such as x86_64-linux-gnu-gcc: error: build/temp.linux-x86_64-3.4/libImaging/Jpeg2KDecode.o: No such file or directory. While this might give you some relevant results, most likely it will not. The trick with installations like these is to scroll up until you see messages about missing headers. Here is an example:

In file included from libImaging/Imaging.h:14:0, from libImaging/Resample.c:16: libImaging/ImPlatform.h:10:20: fatal error: Python.h: No such file or directory #include "Python.h" ^ compilation terminated.

The key message here is that Python.h is missing. These are part of the Python headers and are needed for the compilation of most C/C++ packages within Python. Depending on the operating system, the solutions will vary—unfortunately. So, I recommend that you skip all parts of this paragraph that are not relevant for your case.

Debian and Ubuntu

In Debian and Ubuntu, the package to be installed is python3-dev or python2-dev if you're still using Python 2. The command to execute is as follows:

# sudo apt-get install python3-dev

However, this installs the development headers only. If you want the compiler and other headers bundled with the install, then the build-dep command is also very useful. Here is an example:

# sudo apt-get build-dep python3

Red Hat, CentOS, and Fedora

Red Hat, CentOS, and Fedora are rpm-based distros that use the yum package manager to install the requirements. Most development headers are available through <package-name>-devel and are easily installable as such. To install the Python 3 development headers, use this line:

# sudo apt-get install python3-devel

To make sure you have all the requirements such as development headers and compilers to build packages such as Python, the yum-builddep command is available:

# yum-builddep python3

OS X

The install procedure on OS X consists of three steps before the actual package can be installed.

First, you have to install Xcode. This can be done through the OS X App Store at https://itunes.apple.com/en/app/xcode/id497799835?mt=12.

Then you have to install the Xcode command-line tools:

# xcode-select --install

Finally, you need to install the Homebrew package manager. The steps are available at http://brew.sh/, but the install command is as follows:

# /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Note

Other package managers, such as Macports, are also possible, but Homebrew is currently the OS X package manager with the most active development and community.

Once all of these steps have been completed, you should have a working Homebrew installation. The working of Homebrew can be verified using the brew doctor command. If there are no major errors in the output, then you should be ready to install your first packages through brew. Now we simply need to install Python and we're done:

# brew install python3

Windows

On Windows, manual compilation of C Python packages is generally a non-trivial task to say the least. Most packages have been written with Linux/Unix systems in mind (OS X falls under the Unix category), and Windows is a nice-to-have for developers. The result is that packages are difficult to compile on Windows because there are few people testing them and many of the libraries require manual installation, making it a very tedious task. So, unless you really have to, try and stay away from manually compiling Python packages on Windows. Most packages are available as installable binary downloads with a bit of searching, and there are alternatives such as Anaconda that include binary packages for most important C Python packages.

If you still feel inclined to manually compile C Python packages, then there is another option, and it is generally an easier alternative. The Cygwin project (http://cygwin.com/) attempts to make Linux applications run natively on Windows. This is generally an easier solution than making packages work with Visual Studio.

If you do wish to take the Visual Studio path, I would like to point you towards Chapter 14, Extensions in C/C++, System Calls, and C/C++ Libraries, which covers manual writing of C/C++ extensions and some information on which Visual Studio versions you need for your Python version.

Summary

With the inclusion of packages such as pip and venv, I feel that Python 3 has become a complete package that should suit most people. Beyond legacy applications, there is no real reason not to choose Python 3 anymore. The initial Python 3 release in 2008 was definitely a bit raw compared to the well-rounded Python 2.6 version released the same year, but a lot has changed in that aspect. The last major Python 2 release was Python 2.7, which was released in 2010; within the software world, that is a very, very long time. While Python 2.7 still receives maintenance, it will not receive any of the amazing new features that Python 3 is getting—features such as Unicode strings by default, dict generators (Chapter 6, Generators and Coroutines – Infinity, One Step at a Time), and async methods (Chapter 7, Async IO – Multithreading without Threads).

After finishing this chapter, you should be able to create a clean and recreatable virtual environment and know where to look if an installation of C/C++ packages fails.

Here are the most important notes for this chapter:

For a clean and simple environment, use venv. If compatibility with Python 2 is needed, use virtualenv.If C/C++ packages fail to install, look for the error about missing includes.

The next chapter covers the Python style guide, which rules are important, and why they matter. Readability is one of the most important aspects of the Python philosophy, and you will learn methods and styles for writing cleaner and more readable Python code.