pipenv安装与使用

pipenv使用 Pipfile 和 Pipfile.lock 来管理和维护项目的依赖包,实现虚拟环境运行,避免了包冲突问题

安装与使用

安装
pip3 install pipenv
创建空目录
mkdir myproject
cd myproject
pipenv install requests

如果运行成功,则生成一个 Pipfile。

安装完Requests,创建一个简单的 main.py

1
2
3
4
import requests

response = requests.get('https://httpbin.org/ip')
print('Your IP is {0}'.format(response.json()['origin']))

测试运行
pipenv run python main.py

1
2
3
4
5
6
7
8
9
10
11
pipenv shell 激活虚拟环境   exit退出虚拟环境
pipenv graph 查看目前安装的库及其依赖
pipenv install django==1.11 安装固定版本模块并加入到Pipfile
pipenv install django 安装最新?默认版本模块并加入到Pipfile
pipenv install pytest --dev 通过添加 –dev 参数 来区分开发环境
pipenv uninstall django 卸载第三方库
pipenv uninstall --all 卸载全部包并从Pipfile中移除
pipenv --venv 获得虚拟环境路
pipenv --py 获取虚拟环境 Python 解释器路径
pipenv --site-packages 加载系统 Python包 (默认新创建的虚拟环境不包含第三方包)
pipenv lock 产生Pipfile.lock文件(默认会自己产生)

官方文档:https://pipenv.readthedocs.io/en/latest/#pipenv-usage

InstallError

TLS/SSL缺失

问题描述
[pipenv.exceptions.InstallError]: [‘pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
python2版本,import ssl无错误
pyton3.6.1, import ssl 显示no moudle named _ssl

解决方法
python pip 出现locations that require TLS/SSL异常处理方法: https://blog.csdn.net/zhengcaihua0/article/details/79681991

1
2
3
4
5
6
7
8
9
10
查看openssl安装包,看是否缺少openssl-devel包 
# rpm -aq|grep openssl
yum安装openssl-devel
# yum install openssl-devel -y

对python3.6进行编译安装
cd Python-3.6.1
./configure --with-ssl
make
sudo make install

允许安装的python3使用ssl功能模块,进入python3中,执行import ssl无错误

Locking Failed

问题描述:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Creating a Pipfile for this project…
Installing requests…
✔ Installation Succeeded
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✘ Locking Failed!
[pipenv.exceptions.ResolutionFailure]: File "/usr/local/lib/python3.6/site-packages/pipenv/resolver.py", line 69, in resolve
[pipenv.exceptions.ResolutionFailure]: req_dir=requirements_dir
[pipenv.exceptions.ResolutionFailure]: File "/usr/local/lib/python3.6/site-packages/pipenv/utils.py", line 695, in resolve_deps
[pipenv.exceptions.ResolutionFailure]: req_dir=req_dir,
[pipenv.exceptions.ResolutionFailure]: File "/usr/local/lib/python3.6/site-packages/pipenv/utils.py", line 469, in actually_resolve_deps
[pipenv.exceptions.ResolutionFailure]: resolved_tree = resolver.resolve()
[pipenv.exceptions.ResolutionFailure]: File "/usr/local/lib/python3.6/site-packages/pipenv/utils.py", line 408, in resolve
[pipenv.exceptions.ResolutionFailure]: raise ResolutionFailure(message=str(e))
[pipenv.exceptions.ResolutionFailure]: pipenv.exceptions.ResolutionFailure: ERROR: ERROR: Could not find a version that matches requests
[pipenv.exceptions.ResolutionFailure]: No versions found
[pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
Hint: try $ pipenv lock --pre if it is a pre-release dependency.
ERROR: ERROR: Could not find a version that matches requests
No versions found
Was https://pypi.org/simple reachable?
[pipenv.exceptions.ResolutionFailure]: req_dir=requirements_dir
[pipenv.exceptions.ResolutionFailure]: File "/usr/local/lib/python3.6/site-packages/pipenv/utils.py", line 695, in resolve_deps
[pipenv.exceptions.ResolutionFailure]: req_dir=req_dir,
[pipenv.exceptions.ResolutionFailure]: File "/usr/local/lib/python3.6/site-packages/pipenv/utils.py", line 469, in actually_resolve_deps
[pipenv.exceptions.ResolutionFailure]: resolved_tree = resolver.resolve()
[pipenv.exceptions.ResolutionFailure]: File "/usr/local/lib/python3.6/site-packages/pipenv/utils.py", line 408, in resolve
[pipenv.exceptions.ResolutionFailure]: raise ResolutionFailure(message=str(e))
[pipenv.exceptions.ResolutionFailure]: pipenv.exceptions.ResolutionFailure: ERROR: ERROR: Could not find a version that matches requests
[pipenv.exceptions.ResolutionFailure]: No versions found
[pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
Hint: try $ pipenv lock --pre if it is a pre-release dependency.
ERROR: ERROR: Could not find a version that matches requests
No versions found
Was https://pypi.org/simple reachable?

  1. 检查https://pypi.org/simple是否能打开。
    1
    2
    pipenv.patched.notpip._vendor.urllib3.exceptions.ReadTimeoutError: 
    HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

更换国内源

修改 Pipfile 文件默认源

1
2
3
4
5
查看 Pipfile 的内容: 
cat Pipfile
编辑Pipfile:
vim Pifile
将url = "https://pypi.org/simple"改为url = "https://pypi.tuna.tsinghua.edu.cn/simple/"

  1. 跳过lock
    pipenv install —skip-lock
    失效,出现TLS/SSL缺失,解决后TLS/SSL缺失问题后,该问题解决。

pipenv命令

在pipenv虚拟环境目录下,输入 pipenv 命令可查看命令的完整用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Usage: pipenv [OPTIONS] COMMAND [ARGS]...

Options:
--update Update Pipenv & pip to latest.
--where Output project home information.
--venv Output virtualenv information.
--py Output Python interpreter information.
--envs Output Environment Variable options.
--rm Remove the virtualenv.
--bare Minimal output.
--completion Output completion (to be eval'd).
--man Display manpage.
--three / --two Use Python 3/2 when creating virtualenv.
--python TEXT Specify which version of Python virtualenv should use.
--site-packages Enable site-packages for the virtualenv.
--jumbotron An easter egg, effectively.
--version Show the version and exit.
-h, --help Show this message and exit.


Usage Examples:
Create a new project using Python 3.6, specifically:
$ pipenv --python 3.6

Install all dependencies for a project (including dev):
$ pipenv install --dev

Create a lockfile containing pre-releases:
$ pipenv lock --pre

Show a graph of your installed dependencies:
$ pipenv graph

Check your installed dependencies for security vulnerabilities:
$ pipenv check

Install a local setup.py into your virtual environment/Pipfile:
$ pipenv install -e .

Commands:
check Checks for security vulnerabilities and against PEP 508 markers
provided in Pipfile.
graph Displays currently–installed dependency graph information.
install Installs provided packages and adds them to Pipfile, or (if none
is given), installs all packages.
lock Generates Pipfile.lock.
open View a given module in your editor.
run Spawns a command installed into the virtualenv.
shell Spawns a shell within the virtualenv.
uninstall Un-installs a provided package and removes it from Pipfile.
update Uninstalls all packages, and re-installs package(s) in [packages]
to latest compatible versions.


Commands:
check 检查安全漏洞
graph 显示当前依赖关系图信息
install 安装虚拟环境或者第三方库
lock 锁定并生成Pipfile.lock文件
open 在编辑器中查看一个库
run 在虚拟环境中运行命令
shell 进入虚拟环境
uninstall 卸载一个库
update 卸载当前所有的包,并安装它们的最新版本