pipenv 是Python.org正式推薦的python包管理工具。它完美的解決了python版本及包的管理問題。
pipenv的思路簡單理解便是把pip和virtualenv 2個工具統(tǒng)一起來,使用 pipenv 來代替。
比起virtualenv,它的學(xué)習(xí)成本更低,使用更方便。
它的特點(diǎn):
- pipenv 使用 Pipfile 來代替 requirement.txt 文件記錄python包。
- 增加了Pipfile.lock 文件來鎖定python軟件的包名及版本,以及其依賴關(guān)系的列表。
pipenv 安裝
普通安裝(全局安裝)
pip install pipenv
作者建議在python3下邊安裝,會提高與virtualenv的兼容性。
用戶模式安裝(推薦)
為防止和系統(tǒng)python庫產(chǎn)生影響,可使用此種方案安裝。
pip install pipenv --user
pipenv 使用
初始化虛擬環(huán)境
執(zhí)行pipenv install,創(chuàng)建虛擬環(huán)境
進(jìn)入虛擬環(huán)境
pipenv shell
更多用法
在使用pipenv之前,必須徹底的忘記pip這個東西
新建一個準(zhǔn)備當(dāng)環(huán)境的文件夾pipenvtest,并cd進(jìn)入該文件夾:
pipenv --three 會使用當(dāng)前系統(tǒng)的Python3創(chuàng)建環(huán)境
pipenv --python 3.6 指定某一Python版本創(chuàng)建環(huán)境
可通過參數(shù)--two 和--three 來泛指python版本,也可通過--python 3.5 來明確指定python版本
pipenv shell 激活虛擬環(huán)境
pipenv --where 顯示目錄信息
pipenv --venv 顯示虛擬環(huán)境信息
pipenv --py 顯示Python解釋器信息
pipenv install requests 安裝相關(guān)模塊并加入到Pipfile
pipenv install django==1.11 安裝固定版本模塊并加入到Pipfile
pipenv graph 查看目前安裝的庫及其依賴
pipenv check 檢查安全漏洞
pipenv uninstall --all 卸載全部包并從Pipfile中移除
舉例說明:
環(huán)境 centos7.4 (默認(rèn)已經(jīng)安裝python2.7)
需要的軟件:
python 2, ansible 2.4.2
python 2, ansible 2.6.0
python 3, ansible 2.7.1
也就是說我需要基于不同python 版本的 不同ansible軟件。
實(shí)操過程
Install python 3
默認(rèn)沒有,需要手工安裝。
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);為了防止與系統(tǒng)沖突,裝在個人目錄下,所以用--user 參數(shù)。
初始化環(huán)境
一個新建目錄,執(zhí)行命令,創(chuàng)建虛擬環(huán)境。
(如果不新建目錄,pipenv 會自動在用戶 $HOME 目錄下創(chuàng)建虛擬環(huán)境,這種情況下,你只有一個虛擬環(huán)境。)
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
虛擬環(huán)境已經(jīng)創(chuàng)建,目錄里面多了兩個文件Pipfile 和 Pipfile.lock
激活虛擬環(huán)境
先檢查當(dāng)前的python
[royzeng@roy-pipenv pipenvtest]$ python --version
Python 2.7.5
[royzeng@roy-pipenv pipenvtest]$ which python
/usr/bin/python
還是系統(tǒng)默認(rèn)的python
下面用pipenv shell 激活環(huán)境。
[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]$
在當(dāng)前環(huán)境下安裝ansible 2.7.1
[注] 不要換目錄,如果還要安裝別的軟件,也要在此目錄下安裝,安裝結(jié)束后才能切換到其他目錄。
(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 部分。
看一下依賴關(guān)系
(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]
.....
下面創(chuàng)建一個新的環(huán)境,安裝基于python2的ansible
先要退出原來的環(huán)境
(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.
激活新環(huán)境,并安裝對應(yīng)的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版本,可以再創(chuàng)建一個虛擬環(huán)境來安裝,也可以用系統(tǒng)默認(rèn)環(huán)境安裝。
略
其它
卸載剛才安裝的軟件
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]$
刪除虛擬環(huán)境
pipenv --rm