Code Culture GitBook Demo
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 function 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 venv
Bootstrapping pip using ensurepip
Installing packages based on distutils (C/C++) with pip