简单说,pipenv
就是把pip
和virtualenv
包装起来的一个便携工具。html
它不会在你的项目文件夹里生成一大堆东西,只有两个文本文件:python
Pipfile
, 简明地显示项目环境和依赖包。Pipfile.lock
, 详细记录环境依赖,而且利用了hash算法保证了它完整对应关系。只在你使用pipenv lock
命令后才出现。我这里已经安装了Python3.7
,Python2.7
两个版本,其中Python3.7
为默认版本,已将其路径添加到了PATH
环境变量中。Python2.7
没有添加任何的环境变量。git
D:\Program Files\Python\Python37\ D:\Program Files\Python\Python37\Scripts\
pipenv
可以使用 pip 直接安装。github
pip install pipenv
pip默认安装包路径:算法
D:\Program Files\Python\Python37\Lib\site-packages
为防止和系统python库产生影响,可以使用此种方案安装。shell
pip install --user pipenv
若是使用用户模式安装,安装包路径:django
C:\Users\qhong\AppData\Roaming\Python\Python37\site-packages
若是不知道用户模式安装后路径的,能够执行命令:json
$ python -m site --user-site C:\Users\qhong\AppData\Roaming\Python\Python37\site-packages
使用用户模式安装之后,若是pipenv不可用,须要把路径添加到环境变量PATH中segmentfault
执行pipenv install
,建立虚拟环境,以下:bash
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的时候有三种逻辑:
使用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
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
发现虚拟环境位置改变
$ 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
从项目中更新某个包
pipenv update requests
或更新全部的包
pipenv update
# 删除某个包 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自己就是基于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源,好比:
$ 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 = https://pypi.tuna.tsinghua.edu.cn/simple
在项目中的settings.json配置文件夹修改配置:
"python.pythonPath": "${workspaceRoot}\\.venv\\Scripts\\python.exe", "python.venvPath":"${workspaceRoot}\\.venv",
错误反馈以下:
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