Pipenv与虚拟环境

https://farm3.staticflickr.com/2943/33485660921_dfc0494739_k_d.jpg

这篇教程将带你安装与使用Python包。

它阐明了如何安装和使用必要的工具,强力推荐了一些最佳实践。时刻记住Python被用于非常多的场景,取决于你如何发布软件,依赖的具体管理方式也可能千差万别。这里展示的指引不仅对大多数开发部署环境和网络服务 (包括web应用) 都是适用的,而且对于管理开发测试环境,也适用于任何类型的项目。

注解

这篇指引是基于Python 3编写的,但对Python 2.7也是适用的——如果你还在用的话。

☤ 确保你已经安装好了Python和pip

在进行接下来的步骤之前,请确保你已经安装好了Python并且可以从命令行启动。你可以用下面的简单的命令来做检查:

$ python --version

你应该能看到类似于 3.6.2 的输出。如果你系统上没有Python,请从 python.org 安装最新的3.x版本,或者参考 Python银河系漫游指南 中的 Installing Python 一节。

注解

如果你是新手并看到了这样的错误:

>>> python
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'python' is not defined

这是因为这条命令应该在 shell (也称为 终端控制台) 中运行。参考Python入门的 getting started tutorial 学习如何使用操作系统的shell与Python交互。

此外,你还需要确保pip是可用的。你可以运行下面的命令来检查:

$ pip --version
pip 9.0.1

如果你从源代码安装Python,从 python.org 上提供的安装器,从 Homebrew 或是从 Linuxbrew 安装,都应该自带了pip。但如果你是Linux用户并且通过系统的包管理器安装的Python,你可能需要单独安装 pip

如果你打算通过Homebrew或者Linuxbrew安装Python则可以跳过此步骤。Homebrew/Linuxbrew安装器会自动帮你装好pip。

☤ 安装Pipenv

Pipenv is a dependency manager for Python projects. If you’re familiar with Node.js’s npm or Ruby’s bundler, it is similar in spirit to those tools. While pip can install Python packages, Pipenv is recommended as it’s a higher-level tool that simplifies dependency management for common use cases.

☤ Isolated Installation of Pipenv with Pipx

`Pipx`_ is a tool to help you install and run end-user applications written in Python. It installs applications into an isolated and clean environment on their own. To install pipx, just run:

$ pip install --user pipx

Once you have pipx ready on your system, continue to install Pipenv:

$ pipx install pipenv

☤ 用普通方式安装Pipenv

If you have a working installation of pip, and maintain certain “tool-chain” type Python modules as global utilities in your user environment, pip user installs allow for installation into your home directory. Note that due to interaction between dependencies, you should limit tools installed in this way to basic building blocks for a Python workflow like virtualenv, pipenv, tox, and similar software.

安装方法:

$ pip install --user pipenv

注解

这会执行一次 user installation 来避免破坏系统的包。如果安装完成后 pipenv 在命令行中不可用,你需要把 user base 的二进制执行文件目录添加到 PATH 变量中。

在Linux和macOS上你可以使用 python -m site --user-base 的输出末尾加上 /bin 来获取用户的二进制执行文件目录。举例来说,这一般会输出 ~/.local ( ~ 展开成你的用户根目录的绝对路径),所以你需要将 ~/.local/bin 加到你的 PATH 中。你可以通过修改 ~/.profile`_ 文件永久设置 ``PATH

在Windows上,你可以运行 python -m site --user-site,然后将输出中的 site-packages 替换为 Scripts 即可得到用户的二进制执行文件目录。举例来说,如果它输出了 C:\Users\Username\AppData\Roaming\Python36\site-packages ,你就要设置 PATH , 加上 C:\Users\Username\AppData\Roaming\Python36\Scripts 。你可以在 Control Panel 中永久设置 PATH 变量。为了让 PATH 的改变生效,可能需要注销。

更多信息,请参阅 用户安装文档

在任何时候,如果要更新Pipenv:

$ pip install --user --upgrade pipenv

☤ 暴力安装Pipenv

如果你连pip都没有安装,你可以使用这个暴力安装法安装一整套系统:

$ curl https://raw.githubusercontent.com/pypa/pipenv/master/get-pipenv.py | python

☤ Homebrew Installation of Pipenv(Discouraged)

Homebrew 是一款macOS上的开源包管理系统。对于Linux用户,`Linuxbrew`是它的Linux移植版本。

通过Homebrew或Linuxbrew安装Pipenv时会将Pipenv及其依赖都放在一个隔离的虚拟环境中,这样就不会影响你其他的Python安装的包。

你装好了Homebrew或Linuxbrew之后只要运行:

$ brew install pipenv

在任何时候,如果要更新Pipenv:

$ brew upgrade pipenv

注解

Homebrew installation is discouraged because each time the Homebrew Python is upgraded, which Pipenv depends on, users have to re-install Pipenv, and perhaps all virtual environments managed by it.

☤ 在项目中安装依赖包

Pipenv是分项目管理依赖的。要安装依赖包,进入你的项目目录 (或者就一个空目录),运行:

$ cd myproject
$ pipenv install requests

这样,Pipenv就会将 Requests 这个优秀的库安装好,并且在你的项目目录中创建一个 Pipfile 文件。Pipfile 文件是用来跟踪项目需要的依赖,以备以后如果你将项目分享给其他人,重新安装时使用。你应该得到类似于下面的输出 (具体路径会有所不同):

Creating a Pipfile for this project...
Creating a virtualenv for this project...
Using base prefix '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6'
New python executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python3.6
Also creating executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python
Installing setuptools, pip, wheel...done.

Virtualenv location: ~/.local/share/virtualenvs/tmp-agwWamBd
Installing requests...
Collecting requests
  Using cached requests-2.18.4-py2.py3-none-any.whl
Collecting idna<2.7,>=2.5 (from requests)
  Using cached idna-2.6-py2.py3-none-any.whl
Collecting urllib3<1.23,>=1.21.1 (from requests)
  Using cached urllib3-1.22-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Using cached chardet-3.0.4-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests)
  Using cached certifi-2017.7.27.1-py2.py3-none-any.whl
Installing collected packages: idna, urllib3, chardet, certifi, requests
Successfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22

Adding requests to Pipfile's [packages]...
P.S. You have excellent taste! ✨ 🍰 ✨

☤ 使用安装好的包

现在Requests已经安装好了,你可以创建一个简单的 main.py 文件来使用它:

import requests

response = requests.get('https://httpbin.org/ip')

print('Your IP is {0}'.format(response.json()['origin']))

然后你可以使用 pipenv run 来运行此脚本:

$ pipenv run python main.py

你应该会得到类似于下面的输出:

Your IP is 8.8.8.8

使用 $ pipenv run 可以确保你安装好的包在你的脚本中可用。也可以通过 $ pipenv shell 启动一个新的终端环境,在其中所有的命令都可以访问你安装好的包。

☤ 虚拟环境映射说明

  • Pipenv会自动将项目映射到对应的虚拟环境。
  • 虚拟环境会以项目的根目录加上全路径的hash值作为环境名称 (例如 my_project-a3de50) 存储起来。
  • 如果项目路径被更改,就会破坏这个默认的映射,这样pipenv就无法找到和使用项目的虚拟环境。
  • 你可以在你的.bashrc/.zshrc (或者其他终端配置文件中) 加入 export PIPENV_VENV_IN_PROJECT=1 来让虚拟环境创建到你的项目目录中,这样就避免了以后路径更改带来的问题。

☤ 后续步骤

恭喜,你已经学会了如何安装使用Python包!✨ 🍰 ✨