python虚拟环境之pipenv
官方介绍
具体查看官方库
“Windows is a first–class citizen, in our world.”, Windows用户有没有感动到
首次使用install等命令时,如虚拟环境不存在,则会自动创建
1 | To activate this project's virtualenv, run the following: |
查看帮助文档
1 | >>>pipenv |
特点
- pip和虚拟环境于一体
- 丢弃requirments使用Pipfile和Pipfile.lock
- 清晰的看到库依赖图
- 自动生成Pipfile
- pipenv install或uninstall的库自动从Pipfile中添加/删除
安装
1 | pip install pipenv |
常用命令
1 | pipenv [OPTIONS] COMMAND [ARGS] |
Command
1 | check Checks for security vulnerabilities and against PEP 508 markers provided in Pipfile. |
update是先从pipfile中读取,更新lock文件,然后更新库的安装。
sync是直接安装lock文件中的库。
注意,lock文件在大部分平台相同的情况下用,否则可以不提交lock,每次重新生成(不同平台,如Windows,Linux)。
初始化
1 | pipenv install |
指定python版本
初始化一个python3版本的虚拟环境 :
1 | > pipenv --python 3.6 |
初始化一个python2版本的虚拟环境:
1 | pipenv --python 2.7 |
不指定python版本时,默认安装环境变量中的python版本的虚拟环境。
如果环境变量中有两个指定的python版本,则以第一个为依赖版本:
批量安装pipfile中库
1 | pipenv install |
install后面不带参数,表示安装pipfile中所有库
安装单个库
1 | pipenv install xxx |
单个安装xxx库,并添加到pipfile中
更新安装所需库
1 | pipenv sync or pipenv update |
更换项目的虚拟环境
如果当前依据Python2创建的虚拟环境,想要使用Python3创建虚拟环境,直接重新创建就可以
1 | > pipenv --python 3.6 |
pipfile中的Python版本一致
pipenv创建的的虚拟环境对Python依赖的版本要与pipfile中的版本号一致,否则创建会报错:
1 | Warning: Your Pipfile requires python_version 2.7, but you are using 3.6.5 (/home/admin/.local/share/v/q/bin/python). |
不过不会影响。
pycharm指定虚拟环境
刚创建好的虚拟环境,在pycharm中指定。
选择设置->解释器->添加本地环境
选择刚创建的虚拟环境,windows下的存在用户文件下