官术网_书友最值得收藏!

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:

  1. 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.
  2. 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.
  3. 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 venv
  • Bootstrapping pip using ensurepip
  • Installing packages based on distutils (C/C++) with pip
主站蜘蛛池模板: 井陉县| 城固县| 沈丘县| 北票市| 金沙县| 保定市| 临洮县| 穆棱市| 龙门县| 曲靖市| 板桥市| 汉中市| 麻江县| 白水县| 白玉县| 田阳县| 忻城县| 铜陵市| 柳林县| 香港 | 新泰市| 延津县| 忻城县| 沅江市| 左云县| 连平县| 延边| 安阳市| 灵台县| 金沙县| 环江| 樟树市| 浦城县| 志丹县| 新乐市| 宁化县| 西乌珠穆沁旗| 汉阴县| 广丰县| 贵阳市| 定日县|