Pipenv是Python官方推荐的包管理工具。它结合了Pip和Virtualenv的功能,以及Bundler和NPM等其他语言的包装工具的最佳功能。这样可以简化安装包和管理虚拟环境的工作流程。
在安装Pipenv之前,您需要在系统上安装Python。
如果您使用的是Ubuntu 17.10或更高版本,则可以直接从Pypa ppa
安装Pipenv:
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:
sudo apt install python-pip
pip install pipenv
mkdir python-example && cd python-example
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版本创建环境。
pipenv --python 3.6
ls
检查目录的内容; 你会看到 Pipfile
已经自动创建。在文本编辑器中查看此文件~/python-example/Pipfile
: [[source]]
url = "https://pypi.python.org/simple"
verify\_ssl = true
name = "pypi"
dev-packages
packages
requires
python\_version = "3.6"
[packages]
Pipfile中的部分。此外,Pipenv创建了一个名为的文件Pipfile.lock
,其中包含使用的确切版本的哈希值。这可以确保当其他开发人员为此项目安装依赖项时,它们最终都将使用完全相同的版本。pipenv install numpy[[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
pipenv shell
这是类似于运行source env/bin/activate
带virtualenv
。python
>>> import pytest >>> import numpy as np
virtualenv
):
exit
pipenv graph
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等教程,请前往腾讯云+社区学习更多知识。