前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Pipenv管理Python包和虚拟环境

使用Pipenv管理Python包和虚拟环境

作者头像
双愚
发布2018-08-30 10:33:25
1.4K0
发布2018-08-30 10:33:25
举报

什么是Pipenv?

Pipenv是Python官方推荐的包管理工具。它结合了Pip和Virtualenv的功能,以及Bundler和NPM等其他语言的包装工具的最佳功能。这样可以简化安装包和管理虚拟环境的工作流程。

安装Pipenv

在安装Pipenv之前,您需要在系统上安装Python。

如果您使用的是Ubuntu 17.10或更高版本,则可以直接从Pypa ppa安装Pipenv:

代码语言:txt
复制
sudo apt install software-properties-common python-software-properties
sudo add-apt-repository ppa:pypa/ppa
sudo apt update
sudo apt install pipenv

其他发行版应首先安装Pip并使用它来安装Pipenv:

代码语言:txt
复制
sudo apt install python-pip
pip install pipenv

使用Pipenv

  1. 为示例Python项目创建目录: mkdir python-example && cd python-example
代码语言:txt
复制
Creating a virtualenv for this project…
Using /home/username/miniconda3/bin/python3.6m (3.6.4) to create virtualenv…
⠋Running virtualenv with interpreter /home/username/miniconda3/bin/python3.6m
Using base prefix '/home/username/miniconda3'
New python executable in /home/username/.local/share/virtualenvs/python-example-YJNpmGYi/bin/python3.6m
Also creating executable in /home/username/.local/share/virtualenvs/python-example-YJNpmGYi/bin/python
Installing setuptools, pip, wheel...done.

Virtualenv location: /home/username/.local/share/virtualenvs/python-example-YJNpmGYi

Creating a Pipfile for this project…如果省略该--python选项,将使用系统的默认Python版本创建环境。
  1. 在目录中创建虚拟环境: pipenv --python 3.6
  2. ls检查目录的内容; 你会看到 Pipfile已经自动创建。在文本编辑器中查看此文件~/python-example/Pipfile
代码语言:txt
复制
 [[source]]
   url = "https://pypi.python.org/simple"
   verify\_ssl = true
   name = "pypi"

dev-packages

packages

requires

python\_version = "3.6"
  1. 安装Numpy。Pipenv会自动将依赖项添加到[packages]Pipfile中的部分。此外,Pipenv创建了一个名为的文件Pipfile.lock,其中包含使用的确切版本的哈希值。这可以确保当其他开发人员为此项目安装依赖项时,它们最终都将使用完全相同的版本。pipenv install numpy
  2. 安装特定版本的Pytest作为开发依赖项:pipenv install --dev 'pytest>=3.*'
  3. 查看这些安装对Pipfile所做的更改:
代码语言:txt
复制
[[source]]

url = "https://pypi.python.org/simple"

verify_ssl = true

name = "pypi"

[dev-packages]

pytest = ">=3.*"

[packages]

numpy = "*"

[requires]

python_version = "3.6"

由于在安装Numpy期间未指定任何版本,因此Pipfile指定任何version("*")都可以接受。安装的特定版本记录在Pipfile.lock

注意

如果在没有Pipfile的目录中安装软件包,Pipenv将使用系统的默认Python版本自动在该目录中创建新环境。这意味着本节中的命令可以压缩为两个步骤: pipenv install numpy pipenv install --dev pytest

使用虚拟环境

  1. 从包含Pipfile的目录中,在新环境中启动shell: pipenv shell 这是类似于运行source env/bin/activatevirtualenv
  2. 从这个shell里面启动Python解释器: python
  3. 您应该能够导入任何已安装的软件包: >>> import pytest >>> import numpy as np
  4. 退出shell(类似于停用环境virtualenv): exit
  5. 以图表形式查看项目的依赖项:
代码语言:txt
复制
pipenv graph
代码语言:txt
复制
numpy==1.14.2
pytest==3.5.0
- attrs [required: >=17.4.0, installed: 17.4.0]
- more-itertools [required: >=4.0.0, installed: 4.1.0]
- six [required: >=1.0.0,<2.0.0, installed: 1.11.0]
- pluggy [required: >=0.5,<0.7, installed: 0.6.0]
- py [required: >=1.5.0, installed: 1.5.3]
- setuptools [required: Any, installed: 39.0.1]
- six [required: >=1.10.0, installed: 1.11.0]

该图包括您安装的软件包及其依赖项。

6. 找到虚拟环境的二进制文件: pipenv --venv /home/user/.local/share/virtualenvs/python-example-YJNpmGYi

下一步

有关命令和选项的完整列表,请参阅Pipenv GitHub存储库官方文档

想要了解更多关于python等教程,请前往腾讯云+社区学习更多知识。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 什么是Pipenv?
    • 安装Pipenv
    • 使用Pipenv
      • 使用虚拟环境
      • 下一步
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档