Book Image

Mastering Python

By : Rick van Hattem
Book Image

Mastering Python

By: Rick van Hattem

Overview of this book

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.
Table of Contents (22 chapters)
Mastering Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
6
Generators and Coroutines – Infinity, One Step at a Time
Index

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:

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

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