簡介
簡單說,pipenv就是把pip和virtualenv包裝起來的一個便攜工具。
它不會在你的項目文件夾里生成一大堆東西,只有兩個文本文件:
Pipfile, 簡明地顯示項目環境和依賴包。Pipfile.lock, 詳細記錄環境依賴,並且利用了hash算法保證了它完整對應關系。只在你使用pipenv lock命令后才出現。
安裝
本機環境
我這里已經安裝了Python3.7,Python2.7兩個版本,其中Python3.7為默認版本,已將其路徑添加到了PATH環境變量中。Python2.7 沒有添加任何的環境變量。
D:\Program Files\Python\Python37\
D:\Program Files\Python\Python37\Scripts\
普通安裝
pipenv 可使用 pip 直接安裝。
pip install pipenv
pip默認安裝包路徑:
D:\Program Files\Python\Python37\Lib\site-packages
用戶模式安裝
為防止和系統python庫產生影響,可使用此種方案安裝。
pip install --user pipenv
如果使用用戶模式安裝,安裝包路徑:
C:\Users\qhong\AppData\Roaming\Python\Python37\site-packages
如果不知道用戶模式安裝后路徑的,可以執行命令:
$ python -m site --user-site
C:\Users\qhong\AppData\Roaming\Python\Python37\site-packages
使用用戶模式安裝以后,如果pipenv不可用,需要把路徑添加到環境變量PATH中
pipenv 使用
初始化虛擬環境
執行pipenv install,創建虛擬環境,如下:
D:\Git\vscode
$ pipenv install
Creating a virtualenv for this project…
Pipfile: D:\Git\vscode\Pipfile
Using d:\program files\python\python37\python.exe (3.7.3) to create virtualenv…
[ ] Creating virtual environment...Already using interpreter d:\program files\python\python37\python.exe Using base prefix 'd:\\program files\\python\\python37'
New python executable in C:\Users\qhong\.virtualenvs\vscode-R5kwUx1U\Scripts\python.exe
Installing setuptools, pip, wheel...
done.
Successfully created virtual environment!
Virtualenv location: C:\Users\qhong\.virtualenvs\vscode-R5kwUx1U
Creating a Pipfile for this project…
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (a65489)!
Installing dependencies from Pipfile.lock (a65489)…
================================ 0/0 - 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
從打印信息可見,它在目錄用戶目錄C:\Users\qhong\.virtualenvs\下創建了個和項目同名的虛擬環境(可通過配置環境變量來自定義虛擬環境目錄,export WORKON_HOME=~/.venvs),python使用的是默認的python2.7 。
可通過參數--two 和--three 來泛指python版本,也可通過--python 3.5 來明確知道python版本,但是這些參數的前提是你系統上有此python版本,否則會報如下錯誤:
D:\Git\pyy
$ pipenv --python 3.5 install
Warning: Python 3.5 was not found on your system…
You can specify specific versions of Python with:
$ pipenv --python path\to\python
正確的:
D:\Git\vscode
$ pipenv --python 3.7 install
Creating a virtualenv for this project…
Pipfile: D:\Git\vscode\Pipfile
Using D:/Program Files/Python/Python37/python.exe (3.7.3) to create virtualenv…
[== ] Creating virtual environment...Using base prefix 'D:\\Program Files\\Python\\Python37'
New python executable in D:\Git\vscode\.venv\Scripts\python.exe
Installing setuptools, pip, wheel...
done.
Running virtualenv with interpreter D:/Program Files/Python/Python37/python.exe
Successfully created virtual environment!
Virtualenv location: D:\Git\vscode\.venv
Creating a Pipfile for this project…
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (a65489)!
Installing dependencies from Pipfile.lock (a65489)…
================================ 0/0 - 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
初始化好虛擬環境后,會在項目目錄下生成2個文件Pipfile和Pipfile.lock。為pipenv包的配置文件,代替原來的 requirement.txt。項目提交時,可將Pipfile 文件和Pipfile.lock文件受控提交,待其他開發克隆下載,根據此Pipfile 運行命令pipenv install [--dev]生成自己的虛擬環境。
Pipfile.lock 文件是通過hash算法將包的名稱和版本,及依賴關系生成哈希值,可以保證包的完整性。
使用Python2.7版本創建一下虛擬環境:
D:\Git\py27
$ pipenv --python 2.7 install
Creating a virtualenv for this project…
Pipfile: D:\Git\py27\Pipfile
Using D:/Program Files/Python/Python27/python.exe (2.7.16) to create virtualenv…
[ =] Creating virtual environment...New python executable in D:\Git\py27\.venv\Scripts\python.exe
Installing setuptools, pip, wheel...
done.
Running virtualenv with interpreter D:/Program Files/Python/Python27/python.exe
Successfully created virtual environment!
Virtualenv location: D:\Git\py27\.venv
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.
查看Pipfile
D:\Git\py27
$ pipenv shell
Launching subshell in virtual environment…
D:\Git\py27
(.venv) $ cat Pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
[requires]
python_version = "2.7"
D:\Git\py27
(.venv) $ pip list
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Package Version
---------- -------
pip 19.1.1
setuptools 41.0.1
wheel 0.33.4
D:\Git\py27
(.venv) $ pip -V
pip 19.1.1 from d:\git\py27\.venv\lib\site-packages\pip (python 2.7)
項目虛擬環境同步
只需要安裝Pipenv,然后:
pipenv install
Pipenv會在項目文件夾下自動尋找Pipfile和Pipfile.lock文件,創建一個新的虛擬環境並安裝必要的軟件包。
也就是說pipenv install的時候有三種邏輯:
- 如果目錄下沒有Pipfile和Pipfile.lock文件,表示創建一個新的虛擬環境;
- 如果有,表示使用已有的Pipfile和Pipfile.lock文件中的配置創建一個虛擬環境;
- 如果后面帶諸如django這一類庫名,表示為當前虛擬環境安裝第三方庫。
進入退出虛擬環境
使用pipenv shell命令以后就會進入到虛擬環境
D:\Git\py27
$ pip -V
pip 19.1.1 from d:\program files\python\python37\lib\site-packages\pip (python 3.7)
D:\Git\py27
$ py -V
Python 3.7.3
D:\Git\py27
$ pipenv shell
Launching subshell in virtual environment…
D:\Git\py27
(.venv) $ pip -V
pip 19.1.1 from d:\git\py27\.venv\lib\site-packages\pip (python 2.7)
D:\Git\py27
(.venv) $ py -V
Python 2.7.16
退出pipenv shell
exit //或者 ctrl+d
運行虛擬環境(無需進入特定shell即可按照該環境運行腳本):
D:\Git\py27
$ pipenv run pip -V
pip 19.1.1 from d:\git\py27\.venv\lib\site-packages\pip (python 2.7)
D:\Git\py27
$ pip -V
pip 19.1.1 from d:\program files\python\python37\lib\site-packages\pip (python 3.7)
D:\Git\py27
$ py -V
Python 3.7.3
D:\Git\py27
$ pipenv run py -V
Python 2.7.16
修改虛擬環境位置
默認地,虛擬環境會創建在~/.local/share/virtualenvs目錄里面。
如果我們希望在每個項目的根目錄下保存虛擬環境目錄(.venv),需要在 .bashrc 或 .bash_profile 中配置如下
PIPENV_VENV_IN_PROJECT=1
或者
WORKON_HOME=~/.venvs
重新創建一個項目,初始化虛擬環境
D:\Git\vscode
$ pipenv --where
D:\Git\vscode
D:\Git\vscode
$ pipenv --venv
D:\Git\vscode\.venv
發現虛擬環境位置改變
安裝python模塊
$ pipenv install <包名>
你需要知道的是,進入pipenv虛擬環境后,你還是可以用pip install來安裝包的,也能正常使用,因為virtualenv就是這樣做的。
但是,這樣你就不算使用了pipenv策略了,如果你要在項目文件夾里的Pipfile記錄所有項目需要的依賴環境,就應該放棄使用pip install而使用pipenv install,這樣你的Pipfile就會精確記錄所有需要的依賴。
重新安裝所有packages:
有時候需要沖github上clone項目,下載好后,只需要一句話就可以完成創建環境:
# 根據Pipfile中的描述安裝所有依賴
$ pipenv install
# 或者,根據Pipfile.lock中的描述安裝所有依賴
$ pipenv install --ignore-pipfile
# 或者,只安裝dev組的依賴
$ pipenv install --dev
# 或者,根據曾經在pip上導出requirements.txt安裝依賴
$ pipenv install -r <path-to-requirements.txt>
安裝 requests 模塊:
(vscode-R5kwUx1U) $ pipenv install requests
Installing requests…
Adding requests to Pipfile's [packages]…
Installation Succeeded
Pipfile.lock (444a6d) out of date, updating to (a65489)…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Success!
Updated Pipfile.lock (444a6d)!
Installing dependencies from Pipfile.lock (444a6d)…
================================ 5/5 - 00:00:02
更新python模塊
從項目中更新某個包
pipenv update requests
或更新所有的包
pipenv update
刪除python模塊,刪除環境
# 刪除某個包
pipenv uninstall <包名>
# 刪除整個環境
$ pipenv --rm
鎖定版本
更新 lock 文件鎖定當前環境的依賴版本
pipenv lock
按照樹形結構顯示當前環境的依賴關系:
$ pipenv graph
然后就會顯示出如下效果:
(vscode-R5kwUx1U) $ pipenv graph
requests==2.22.0
- certifi [required: >=2017.4.17, installed: 2019.3.9]
- chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]
- idna [required: >=2.5,<2.9, installed: 2.8]
- urllib3 [required: >=1.21.1,<1.26,!=1.25.1,!=1.25.0, installed: 1.25.3]
更換pipenv源
修改Pipfile
Pipenv本身就是基於Pip,所以也可以更換PyPI源。只需要在Pipfile中更換對應的url即可。比如,下面的Pipfile使用阿里雲提供的鏡像源:
[[source]]
url = "http://mirrors.aliyun.com/pypi/simple"
verify_ssl = true
name = "pypi"
上方這種設置為http的,會出現信任問題,可以使用 :
url = "http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com"
或者直接使用https
url = https://pypi.tuna.tsinghua.edu.cn/simple
執行命令--pypi-mirror指定
在執行安裝命令時通過--pypi-mirror選項指定PyPI源,比如:
$ pipenv install --pypi-mirror <mirror_url>
$ pipenv update --pypi-mirror <mirror_url>
$ pipenv sync --pypi-mirror <mirror_url>
$ pipenv lock --pypi-mirror <mirror_url>
$ pipenv uninstall --pypi-mirror <mirror_url>
例如:
$ pipenv install --pypi-mirror https://pypi.tuna.tsinghua.edu.cn/simple django
Installing django…
Adding django to Pipfile's [packages]…
Installation Succeeded
Pipfile.lock (748999) out of date, updating to (444a6d)…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Success!
Updated Pipfile.lock (748999)!
Installing dependencies from Pipfile.lock (748999)…
================================ 8/8 - 00:00:02
環境變量PIPENV_PYPI_MIRROR
設置環境變量PIPENV_PYPI_MIRROR效果相同。
PIPENV_PYPI_MIRROR = https://pypi.tuna.tsinghua.edu.cn/simple
在VsCode中使用
在項目中的settings.json配置文件夾修改配置:
"python.pythonPath": "${workspaceRoot}\\.venv\\Scripts\\python.exe",
"python.venvPath":"${workspaceRoot}\\.venv",
常見錯誤
pipenv lock 時遇到的SSL Error
錯誤反饋如下:
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
usr/local/Cellar/pipenv/2018.5.18/libexec/lib/python3.6/site-packages/pipenv/vendor/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/Cellar/pipenv/2018.5.18/libexec/lib/python3.6/site-packages/pipenv/vendor/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/usr/local/Cellar/pipenv/2018.5.18/libexec/lib/python3.6/site-packages/pipenv/vendor/requests/adapters.py", line 506, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /pypi/pyobjc-framework-netfs/json (Caused by SSLError(SSLError(1, u'[SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590)'),))
最佳解決方案是:
$ pip install pyopenssl
因為這種SSL Error在其他地方也常見,一般都是沒有在環境里安裝pyopenssl的問題。所以不管你在哪個環境,如果出現這個SSL問題,就先裝pyopenssl解決。
注意:不要用pipenv install pyopenssl,因為你真的不想在每個環境里都重新裝一遍這個,干脆把它撞到本機:$ pip install pyopenssl.
常見錯誤操作
不要在pipenv shell里面運行pipenv install
不要在pipenv shell里面運行deactivate
