pipenv 是Python.org正式推荐的python包管理工具。它完美的解决了python版本及包的管理问题。
pipenv的思路简单理解便是把pip和virtualenv 2个工具统一起来,使用 pipenv 来代替。
比起virtualenv,它的学习成本更低,使用更方便。
它的特点:
- pipenv 使用 Pipfile 来代替 requirement.txt 文件记录python包。
- 增加了Pipfile.lock 文件来锁定python软件的包名及版本,以及其依赖关系的列表。
pipenv 安装
普通安装(全局安装)
pip install pipenv
作者建议在python3下边安装,会提高与virtualenv的兼容性。
用户模式安装(推荐)
为防止和系统python库产生影响,可使用此种方案安装。
pip install pipenv --user
pipenv 使用
初始化虚拟环境
执行pipenv install,创建虚拟环境
进入虚拟环境
pipenv shell
更多用法
在使用pipenv之前,必须彻底的忘记pip这个东西
新建一个准备当环境的文件夹pipenvtest,并cd进入该文件夹:
pipenv --three
会使用当前系统的Python3创建环境
pipenv --python 3.6
指定某一Python版本创建环境
可通过参数--two 和--three 来泛指python版本,也可通过--python 3.5 来明确指定python版本
pipenv shell
激活虚拟环境
pipenv --where
显示目录信息
pipenv --venv
显示虚拟环境信息
pipenv --py
显示Python解释器信息
pipenv install requests
安装相关模块并加入到Pipfile
pipenv install django==1.11
安装固定版本模块并加入到Pipfile
pipenv graph
查看目前安装的库及其依赖
pipenv check
检查安全漏洞
pipenv uninstall --all
卸载全部包并从Pipfile中移除
举例说明:
环境 centos7.4 (默认已经安装python2.7)
需要的软件:
python 2, ansible 2.4.2
python 2, ansible 2.6.0
python 3, ansible 2.7.1
也就是说我需要基于不同python 版本的 不同ansible软件。
实操过程
Install python 3
默认没有,需要手工安装。
sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm
sudo yum -y install python36u
Verify:
$ which python3.6
/usr/bin/python3.6
Install pip3
sudo python3.6 -m ensurepip
Verify:
$ which pip3
/usr/bin/pip3
Install pipenv
pip3 install pipenv --user
为了兼容用python3 安装,所以是pip3
(not pip);为了防止与系统冲突,装在个人目录下,所以用--user
参数。
初始化环境
一个新建目录,执行命令,创建虚拟环境。
(如果不新建目录,pipenv 会自动在用户 $HOME 目录下创建虚拟环境,这种情况下,你只有一个虚拟环境。)
pipenv install
e.g.
[royzeng@roy-pipenv ~]$ mkdir pipenvtest
[royzeng@roy-pipenv ~]$ cd pipenvtest/
[royzeng@roy-pipenv pipenvtest]$ pipenv install
Creating a virtualenv for this project…
Pipfile: /home/royzeng/pipenvtest/Pipfile
Using /bin/python3.6 (3.6.5) to create virtualenv…
⠏Already using interpreter /bin/python3.6
Using base prefix '/usr'
New python executable in /home/royzeng/.local/share/virtualenvs/pipenvtest-oy7aR4Tb/bin/python3.6
Also creating executable in /home/royzeng/.local/share/virtualenvs/pipenvtest-oy7aR4Tb/bin/python
Installing setuptools, pip, wheel...done.
Virtualenv location: /home/royzeng/.local/share/virtualenvs/pipenvtest-oy7aR4Tb
Creating a Pipfile for this project…
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (ca72e7)!
Installing dependencies from Pipfile.lock (ca72e7)…
🎃 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
[royzeng@roy-pipenv pipenvtest]$ ls
Pipfile Pipfile.lock
虚拟环境已经创建,目录里面多了两个文件Pipfile 和 Pipfile.lock
激活虚拟环境
先检查当前的python
[royzeng@roy-pipenv pipenvtest]$ python --version
Python 2.7.5
[royzeng@roy-pipenv pipenvtest]$ which python
/usr/bin/python
还是系统默认的python
下面用pipenv shell 激活环境。
[royzeng@roy-pipenv pipenvtest]$ pipenv shell
Launching subshell in virtual environment…
. /home/royzeng/.local/share/virtualenvs/pipenvtest-oy7aR4Tb/bin/activate
[royzeng@roy-pipenv pipenvtest]$ . /home/royzeng/.local/share/virtualenvs/pipenvtest-oy7aR4Tb/bin/activate
(pipenvtest) [royzeng@roy-pipenv pipenvtest]$ which python
~/.local/share/virtualenvs/pipenvtest-oy7aR4Tb/bin/python
(pipenvtest) [royzeng@roy-pipenv pipenvtest]$ python --version
Python 3.6.5
(pipenvtest) [royzeng@roy-pipenv pipenvtest]$
在当前环境下安装ansible 2.7.1
[注] 不要换目录,如果还要安装别的软件,也要在此目录下安装,安装结束后才能切换到其他目录。
(pipenvtest) [royzeng@roy-pipenv pipenvtest]$ pipenv install ansible==2.7.1
Installing ansible==2.7.1…
Collecting ansible==2.7.1
Downloading https://files.pythonhosted.org/packages/ec/ee/1494474b59c6e9cccdfde32da1364b94cdb280ff96b1493deaf4f3ae55f8/ansible-2.7.1.tar.gz (11.7MB)
Collecting jinja2 (from ansible==2.7.1)
....
Successfully built ansible PyYAML MarkupSafe pycparser
Installing collected packages: MarkupSafe, jinja2, PyYAML, pycparser, cffi, six, bcrypt, pyasn1, pynacl, idna, asn1crypto, cryptography, paramiko, ansible
Successfully installed MarkupSafe-1.0 PyYAML-3.13 ansible-2.7.1 asn1crypto-0.24.0 bcrypt-3.1.4 cffi-1.11.5 cryptography-2.3.1 idna-2.7 jinja2-2.10 paramiko-2.4.2 pyasn1-0.4.4 pycparser-2.19 pynacl-1.3.0 six-1.11.0
Adding ansible to Pipfile's [packages]…
Pipfile.lock (93cf17) out of date, updating to (ca72e7)…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (93cf17)!
Installing dependencies from Pipfile.lock (93cf17)…
🎃 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 14/14 — 00:00:11
(pipenvtest) [royzeng@roy-pipenv pipenvtest]$ which ansible
~/.local/share/virtualenvs/pipenvtest-oy7aR4Tb/bin/ansible
(pipenvtest) [royzeng@roy-pipenv pipenvtest]$ ansible --version
ansible 2.7.1
config file = None
configured module search path = ['/home/royzeng/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/royzeng/.local/share/virtualenvs/pipenvtest-oy7aR4Tb/lib/python3.6/site-packages/ansible
executable location = /home/royzeng/.local/share/virtualenvs/pipenvtest-oy7aR4Tb/bin/ansible
python version = 3.6.5 (default, Apr 10 2018, 17:08:37) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
从Pipfile看,之前安装了什么
(pipenvtest) [royzeng@roy-pipenv pipenvtest]$ cat Pipfile
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
ansible = "==2.7.1"
[dev-packages]
[requires]
python_version = "3.6"
比起之前,多了ansible 部分。
看一下依赖关系
(pipenvtest) [royzeng@roy-pipenv pipenvtest]$ pipenv graph
ansible==2.7.1
- cryptography [required: Any, installed: 2.3.1]
- asn1crypto [required: >=0.21.0, installed: 0.24.0]
- cffi [required: >=1.7,!=1.11.3, installed: 1.11.5]
- pycparser [required: Any, installed: 2.19]
- idna [required: >=2.1, installed: 2.7]
- six [required: >=1.4.1, installed: 1.11.0]
- jinja2 [required: Any, installed: 2.10]
- MarkupSafe [required: >=0.23, installed: 1.0]
- paramiko [required: Any, installed: 2.4.2]
- bcrypt [required: >=3.1.3, installed: 3.1.4]
- cffi [required: >=1.1, installed: 1.11.5]
- pycparser [required: Any, installed: 2.19]
.....
下面创建一个新的环境,安装基于python2的ansible
先要退出原来的环境
(pipenvtest) [royzeng@roy-pipenv pipenvtest]$ exit
exit
[royzeng@roy-pipenv pipenvtest]$ cd ..
[royzeng@roy-pipenv ~]$
New dir for new environment
[royzeng@roy-pipenv ~]$ mkdir p2
[royzeng@roy-pipenv ~]$ cd p2
[royzeng@roy-pipenv p2]$ pipenv install --two
Creating a virtualenv for this project…
Pipfile: /home/royzeng/p2/Pipfile
Using /usr/bin/python2.7 (2.7.5) to create virtualenv…
⠋Running virtualenv with interpreter /usr/bin/python2.7
New python executable in /home/royzeng/.local/share/virtualenvs/p2-GP1OBmJO/bin/python2.7
Also creating executable in /home/royzeng/.local/share/virtualenvs/p2-GP1OBmJO/bin/python
Installing setuptools, pip, wheel...done.
Virtualenv location: /home/royzeng/.local/share/virtualenvs/p2-GP1OBmJO
Creating a Pipfile for this project…
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (dfae9f)!
Installing dependencies from Pipfile.lock (dfae9f)…
🎃 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
激活新环境,并安装对应的ansible
[royzeng@roy-pipenv p2]$ pipenv shell
Launching subshell in virtual environment…
. /home/royzeng/.local/share/virtualenvs/p2-GP1OBmJO/bin/activate
[royzeng@roy-pipenv p2]$ . /home/royzeng/.local/share/virtualenvs/p2-GP1OBmJO/bin/activate
(p2) [royzeng@roy-pipenv p2]$ pipenv install ansible==2.6.0
Installing ansible==2.6.0…
Collecting ansible==2.6.0
Collecting cryptography (from ansible==2.6.0)
....
....
Adding ansible to Pipfile's [packages]…
Pipfile.lock (040b7c) out of date, updating to (dfae9f)…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (040b7c)!
Installing dependencies from Pipfile.lock (040b7c)…
🎃 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 16/16 —
(p2) [royzeng@roy-pipenv p2]$
(p2) [royzeng@roy-pipenv p2]$ ansible --version
ansible 2.6.0
config file = None
configured module search path = [u'/home/royzeng/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /home/royzeng/.local/share/virtualenvs/p2-GP1OBmJO/lib/python2.7/site-packages/ansible
executable location = /home/royzeng/.local/share/virtualenvs/p2-GP1OBmJO/bin/ansible
python version = 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
第三个ansible版本,可以再创建一个虚拟环境来安装,也可以用系统默认环境安装。
略
其它
卸载刚才安装的软件
pipenv uninstall requests
e.g.
(p2) [royzeng@roy-pipenv p2]$ pipenv uninstall ansible
Un-installing ansible…
Uninstalling ansible-2.6.0:
Successfully uninstalled ansible-2.6.0
Removing ansible from Pipfile…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (dfae9f)!
(p2) [royzeng@roy-pipenv p2]$
删除虚拟环境
pipenv --rm