简介

简单说,pipenv就是把pipvirtualenv包装起来的一个便携工具。

它不会在你的项目文件夹里生成一大堆东西,只有两个文本文件:

  • 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个文件PipfilePipfile.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)'),))

参考pipenv的issue解答。

最佳解决方案是:

$ pip install pyopenssl

因为这种SSL Error在其他地方也常见,一般都是没有在环境里安装pyopenssl的问题。所以不管你在哪个环境,如果出现这个SSL问题,就先装pyopenssl解决。

注意:不要用pipenv install pyopenssl,因为你真的不想在每个环境里都重新装一遍这个,干脆把它撞到本机:$ pip install pyopenssl.

常见错误操作

不要在pipenv shell里面运行pipenv install

不要在pipenv shell里面运行deactivate

参考

pipenv 更优雅的管理你的python开发环境

快速入门Python 最新最流行的pipenv虚拟环境

Pipenv:人类的Python Dev工作流程

利用pipenv和pyenv管理多个相互独立的Python虚拟开发环境

Pipenv – 超好用的 Python 包管理工具

Pipenv 的使用

pipenv管理python开发环境的更多相关文章

  1. 在Ubuntu下配置舒服的Python开发环境

    Ubuntu 提供了一个良好的 Python 开发环境,但如果想使我们的开发效率最大化,还需要进行很多定制化的安装和配置.下面的是我们团队开发人员推荐的一个安装和配置步骤,基于 Ubuntu 12.0 ...

  2. 转[开发环境配置]在Ubuntu下配置舒服的Python开发环境

    在Ubuntu下配置舒服的Python开发环境 Ubuntu 提供了一个良好的 Python 开发环境,但如果想使我们的开发效率最大化,还需要进行很多定制化的安装和配置.下面的是我们团队开发人员推荐的 ...

  3. [转]Aptana Studio 3配置Python开发环境图文教程

    转载URL:http://www.cr173.com/html/49260_1.html 一.安装Aptana Studio 3 安装完运行时建议将相关默认工作目录设定在英文的某个目录下.避免可能出现 ...

  4. 翻译:打造基于Sublime Text 3的全能python开发环境

    原文地址:https://realpython.com/blog/python/setting-up-sublime-text-3-for-full-stack-python-development/ ...

  5. Notepad++配置Python开发环境

    1. 安装Python 1 下载 我选择了32位的2.7版本.https://www.python.org/ftp/python/2.7.8/python-2.7.8.msi 2. 安装 安装的时候可 ...

  6. [原]打造Python开发环境之初篇

    古语有云: 工欲善其事,必先利其器 拥有自己的一套得心应手的Python开发环境,开发起来,简直如丝般顺滑.以我工作中使用到的Python开发环境(主要是Web应用),先做个总体介绍 Python环境 ...

  7. 配置Sublime Text 3的Python开发环境

    最近的项目是用Python开发自动化测试脚本的,所以使用Python比较多.我用的编辑器是Sublime Text3. Sublime Text 3是一个轻量级的跨平台文字编辑器,一经面世便被认为是一 ...

  8. Python开发环境的搭建(win7)

    一个.安装和配置Python 事实上,在开发python最好ubuntu环境.简单和易于扩展每个package. 在谈到如何win7建筑物Python开发环境. 因为python十字-platform ...

  9. windows 10下sublime text3环境的搭建以及配置python开发环境

    1 - 安装Sublime Text 3 到官网下载对应的版本,如下: OS X (10.7 or later is required) Windows - also available as a p ...

随机推荐

  1. springboot + shiro + mysql + mybatis 工程快速搭建

    1. 新建 springboot 工程 2. 随便起个名字 3. 初始化工程 4. 导入 shiro 和 thymeleaf 依赖 <!-- thymeleaf依赖 --> <dep ...

  2. Jmeter通过正则表达式获取登录后token

    1.引用名称:即参数化,便于后面调用登录后获取的token 2.正则表达式:"token":"([0-9 a-z -]+?)" 3.模板:设置为 "$ ...

  3. XGBoost、LightGBM参数讲解及实战

    本文链接:https://blog.csdn.net/linxid/article/details/80785131XGBoost一.API详解xgboost.XGBClassifier1.1 参数1 ...

  4. prometheus operator的深度好文

    转: https://www.servicemesher.com/blog/prometheus-operator-manual/

  5. 2019面向对象程序设计(java)课程学习进度条

    2019面向对象程序设计(java)课程学习进度条 周次 (阅读/编写)代码行数 发布博客量/评论他人博客数量 课余学习时间(小时) 学习收获最大的程序阅读或编程任务 1 20/10 1/0 5 九九 ...

  6. oracle 循环插入数据

    参考链接:oracle 行转列 pivot函数基本用法 --建表 --drop table SalesList; create table SalesList( keHu varchar2(20), ...

  7. [C3W2] Structuring Machine Learning Projects - ML Strategy 2

    第二周:机器学习策略(2)(ML Strategy(2)) 误差分析(Carrying out error analysis) 你好,欢迎回来,如果你希望让学习算法能够胜任人类能做的任务,但你的学习算 ...

  8. 【声明式事务】Spring事务特性(二)

    spring所有的事务管理策略类都继承自org.springframework.transaction.PlatformTransactionManager接口. 其中TransactionDefin ...

  9. ICCV

    ICCV 简介 ICCV 的全称是 IEEE International Conference on Computer Vision,即国际计算机视觉大会,由IEEE主办,与计算机视觉模式识别会议(C ...

  10. 【2019.8.8 慈溪模拟赛 T1】开箱(chest)(暴力DP水过)

    转化题意 这题目乍一看十分玄学,完全不可做. 但实际上,假设我们在原序列从小到大排序之后,选择开的宝箱编号是\(p_{1\sim Z}\),则最终答案就是: \[\sum_{i=1}^Za_{p_i} ...