() translation by (you can also view the original English article)
Resumen
Python es uno de los lenguajes más amigables y poderosos que existen. Es fácil de aprender para los principiantes, pero tiene un gran impacto y se usa ampliamente en diversos dominios, como programación científica, programación de aplicaciones web y DevOps. Pero uno de los puntos más débiles de Python ha sido su soporte para empaquetar aplicaciones complejas y sus dependencias.
Con los años, se han realizado muchos esfuerzos para mejorar la situación. En agosto de 2017, escribí un tutorial sobre el estado del arte en el empaquetado de Python: Cómo escribir, empaquetar y distribuir una biblioteca en Python.
Han pasado solo cuatro meses, y hay un nuevo jugador en la ciudad. Pipenv es ahora la herramienta oficial recomendada para el empaquetado por PyPA (Python Packaging Authority). En este tutorial aprenderá por qué Pipenv mejora significativamente el estado del empaque y el flujo de trabajo de desarrollo general para los desarrolladores de Python y cómo usarlo de manera efectiva.
Flujo de trabajo de Python Dev para humanos
El objetivo de Pipenv es mejorar el flujo de trabajo de desarrollo de los desarrolladores de Python cuando se trata de administrar dependencias y entornos virtuales. Es otra excelente biblioteca del industrioso Kenneth Reitz, conocido principalmente por el paquete de solicitudes (HTTP para humanos), pero escribió algunos otros excelentes paquetes.
¿Necesitamos otra herramienta de embalaje?
¡Sí! Pipenv toma una página de las prácticas modernas de administración de paquetes y las importa al mundo de Python.
Instalando Pipenv
Puede instalar Pipenv con pip install pipenv
. Obtendrás un buen resultado con emojis:
1 |
$ pip install pipenv |
2 |
✨🍰✨ |
https://github.com/pypa/pipenv/blob/master/get-pipenv.py
| pitón
>
1 |
~/git > mkdir testpipenv |
2 |
~/git > cd testpipenv |
3 |
~/git/testpipenv > pipenv --three |
4 |
|
5 |
Output: |
6 |
|
7 |
Creating a virtualenv for this project…
|
8 |
Using /usr/local/bin/python3 to create virtualenv… |
9 |
⠋Running virtualenv with interpreter /usr/local/bin/python3 |
10 |
Using base prefix '/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6'
|
11 |
New python executable in /Users/gigi.sayfan/.local/share/virtualenvs/testpipenv-0GShD6dy/bin/python3.6
|
12 |
Also creating executable in /Users/gigi.sayfan/.local/share/virtualenvs/testpipenv-0GShD6dy/bin/python
|
13 |
Installing setuptools, pip, wheel...done. |
14 |
|
15 |
Virtualenv location: /Users/gigi.sayfan/.local/share/virtualenvs/testpipenv-0GShD6dy |
16 |
Creating a Pipfile for this project…
|
1 |
~/git/testpipenv > pipenv install requests |
2 |
|
3 |
Output: |
4 |
|
5 |
|
6 |
Installing requests… |
7 |
Collecting requests |
8 |
Using cached requests-2.18.4-py2.py3-none-any.whl |
9 |
Collecting idna<2.7,>=2.5 (from requests) |
10 |
Using cached idna-2.6-py2.py3-none-any.whl |
11 |
Collecting chardet<3.1.0,>=3.0.2 (from requests) |
12 |
Using cached chardet-3.0.4-py2.py3-none-any.whl |
13 |
Collecting certifi>=2017.4.17 (from requests) |
14 |
Using cached certifi-2017.11.5-py2.py3-none-any.whl |
15 |
Collecting urllib3<1.23,>=1.21.1 (from requests) |
16 |
Using cached urllib3-1.22-py2.py3-none-any.whl |
17 |
Installing collected packages: idna, chardet, certifi, urllib3, requests |
18 |
Successfully installed certifi-2017.11.5 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22 |
19 |
|
20 |
Adding requests to Pipfile's [packages]…
|
21 |
PS: You have excellent taste! ✨ 🍰 ✨
|
22 |
Locking [dev-packages] dependencies…
|
23 |
Locking [packages] dependencies…
|
24 |
Updated Pipfile.lock (7b8df8)!
|
1 |
[[source]] |
2 |
|
3 |
url = "https://pypi.python.org/simple" |
4 |
verify_ssl = true |
5 |
name = "pypi" |
6 |
|
7 |
|
8 |
[dev-packages] |
9 |
|
10 |
|
11 |
|
12 |
[packages] |
13 |
|
14 |
requests = "*" |
15 |
|
16 |
|
17 |
[requires] |
18 |
|
19 |
python_version = "3.6" |
1 |
{
|
2 |
"_meta": { |
3 |
"hash": { |
4 |
"sha256": "33a0ec7c8e3bae6f62dd618f847de92ece20e2bd4efb496927e2524b9c7b8df8" |
5 |
},
|
6 |
"host-environment-markers": { |
7 |
"implementation_name": "cpython", |
8 |
"implementation_version": "3.6.3", |
9 |
"os_name": "posix", |
10 |
"platform_machine": "x86_64", |
11 |
"platform_python_implementation": "CPython", |
12 |
"platform_release": "16.7.0", |
13 |
"platform_system": "Darwin", |
14 |
"platform_version": "Darwin Kernel Version 16.7.0: Wed Oct 4 00:17:00 PDT 2017; root:xnu-3789.71.6~1/RELEASE_X86_64", |
15 |
"python_full_version": "3.6.3", |
16 |
"python_version": "3.6", |
17 |
"sys_platform": "darwin" |
18 |
},
|
19 |
"pipfile-spec": 6, |
20 |
"requires": { |
21 |
"python_version": "3.6" |
22 |
},
|
23 |
"sources": [ |
24 |
{
|
25 |
"name": "pypi", |
26 |
"url": "https://pypi.python.org/simple", |
27 |
"verify_ssl": true |
28 |
}
|
29 |
]
|
30 |
},
|
31 |
"default": { |
32 |
"certifi": { |
33 |
"hashes": [ |
34 |
"sha256:244be0d93b71e93fc0a0a479862051414d0e00e16435707e5bf5000f92e04694", |
35 |
"sha256:5ec74291ca1136b40f0379e1128ff80e866597e4e2c1e755739a913bbc3613c0" |
36 |
],
|
37 |
"version": "==2017.11.5" |
38 |
},
|
39 |
"chardet": { |
40 |
"hashes": [ |
41 |
"sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691", |
42 |
"sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae" |
43 |
],
|
44 |
"version": "==3.0.4" |
45 |
},
|
46 |
"idna": { |
47 |
"hashes": [ |
48 |
"sha256:8c7309c718f94b3a625cb648ace320157ad16ff131ae0af362c9f21b80ef6ec4", |
49 |
"sha256:2c6a5de3089009e3da7c5dde64a141dbc8551d5b7f6cf4ed7c2568d0cc520a8f" |
50 |
],
|
51 |
"version": "==2.6" |
52 |
},
|
53 |
"requests": { |
54 |
"hashes": [ |
55 |
"sha256:6a1b267aa90cac58ac3a765d067950e7dbbf75b1da07e895d1f594193a40a38b", |
56 |
"sha256:9c443e7324ba5b85070c4a818ade28bfabedf16ea10206da1132edaa6dda237e" |
57 |
],
|
58 |
"version": "==2.18.4" |
59 |
},
|
60 |
"urllib3": { |
61 |
"hashes": [ |
62 |
"sha256:06330f386d6e4b195fbfc736b297f58c5a892e4440e54d294d7004e3a9bbea1b", |
63 |
"sha256:cc44da8e1145637334317feebd728bd869a35285b93cbb4cca2577da7e62db4f" |
64 |
],
|
65 |
"version": "==1.22" |
66 |
}
|
67 |
},
|
68 |
"develop": {} |
1 |
~/git/testpipenv > pipenv graph
|
2 |
requests==2.18.4 |
3 |
- certifi [required: >=2017.4.17, installed: 2017.11.5] |
4 |
- chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4] |
5 |
- idna [required: <2.7,>=2.5, installed: 2.6] |
6 |
- urllib3 [required: >=1.21.1,<1.23, installed: 1.22] |
shell
. .
solicitudes
están disponibles.e.
1 |
~/git/testpipenv > pipenv shell |
2 |
Spawning environment shell (/bin/bash). Use 'exit' to leave. |
3 |
source /Users/gigi.sayfan/.local/share/virtualenvs/testpipenv-0GShD6dy/bin/activate |
4 |
~/git/testpipenv > source /Users/gigi.sayfan/.local/share/virtualenvs/testpipenv-0GShD6dy/bin/activate |
5 |
(testpipenv-0GShD6dy) ~/git/testpipenv > python |
6 |
Python 3.6.3 (default, Nov 19 2017, 16:39:12) |
7 |
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.38)] on darwin |
8 |
Type "help", "copyright", "credits" or "license" for more information. |
9 |
>>> import requests |
10 |
>>> r = requests.get('https://quotes.rest/qod') |
11 |
>>> r.ok |
12 |
True
|
13 |
>>> r.json() |
14 |
{'success': {'total': 1}, 'contents': {'quotes': [{'quote': 'Hang Out with People Who are Better than You.', 'author': 'Warren Buffett', 'length': None, 'tags': ['getting-better', 'inspire', 'people'], 'category': 'inspire', 'title': 'Inspiring Quote of the day', 'date': '2018-01-03', 'id': None}], 'copyright': '2017-19 theysaidso.com'}} |
15 |
>>> quote = r.json()['contents']['quotes'][0]['quote'] |
16 |
>>> author = r.json()['contents']['quotes'][0]['author'] |
17 |
>>> print(f'{quote} ~~ {author}') |
18 |
Hang Out with People Who are Better than You. ~~ Warren Buffett |
19 |
>>>
|
1 |
~/git/testpipenv > cat requirements.txt |
2 |
requests==2.18.4 |
3 |
~/git/testpipenv > pipenv --three |
4 |
Creating a virtualenv for this project…
|
5 |
Using /usr/local/bin/python3 to create virtualenv… |
6 |
⠋Running virtualenv with interpreter /usr/local/bin/python3 |
7 |
Using base prefix '/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6'
|
8 |
New python executable in /Users/gigi.sayfan/.local/share/virtualenvs/testpipenv-0GShD6dy/bin/python3.6
|
9 |
Also creating executable in /Users/gigi.sayfan/.local/share/virtualenvs/testpipenv-0GShD6dy/bin/python
|
10 |
Installing setuptools, pip, wheel...done. |
11 |
|
12 |
Virtualenv location: /Users/gigi.sayfan/.local/share/virtualenvs/testpipenv-0GShD6dy |
13 |
Requirements.txt found, instead of Pipfile! Converting… |
14 |
Warning: Your Pipfile now contains pinned versions, if your requirements.txt did.
|
15 |
We recommend updating your Pipfile to specify the "*" version, instead.
|
1 |
[packages] |
2 |
|
3 |
requests = "==2.18.4" |
1 |
~/git/testpipenv > pipenv install |
2 |
Pipfile.lock not found, creating… |
3 |
Locking [dev-packages] dependencies…
|
4 |
Locking [packages] dependencies…
|
5 |
Updated Pipfile.lock (0b0daf)! |
6 |
Installing dependencies from Pipfile.lock (0b0daf)… |
7 |
🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 5/5 — 00:00:01 |
8 |
To activate this project's virtualenv, run the following:
|
9 |
$ pipenv shell
|
10 |
~/git/testpipenv >
|
-e
y --dev
:gs:
1 |
> pipenv install '-e .' --dev |
instalación pipenv
admite varias opciones:ns:
-
<
--dev
: instala paquetes de desarrollo y predeterminados desde Pipfile.lock.ck. < --system
: utiliza el comando pip del sistema en lugar del de tu virtualenv.nv. < --ignore-pipfile
: Ignora el Pipfile e instálalo desde Pipfile.lock.ck. < --skip-lock
: Ignora el Pipfile.lock e instálalo desde el Pipfile. Además, no escriba un archivo Pipfile.lock que refleje los cambios en el Pipfile. e. pipenv uninstall<package name>
. Por ejemplo:le:
1 |
~/git/testpipenv > pipenv uninstall requests
|
2 |
Un-installing requests… |
3 |
Uninstalling requests-2.18.4: |
4 |
Successfully uninstalled requests-2.18.4 |
5 |
|
6 |
Removing requests from Pipfile… |
7 |
Locking [dev-packages] dependencies…
|
8 |
Locking [packages] dependencies…
|
9 |
Updated Pipfile.lock (625834)! |
pipenv lock --pre.
e>.
pipenv --rm
.e>.
verificación
, que verifica el cumplimiento con PEP 508: especificación de dependencia para los paquetes de software de Python, así como la seguridad del paquete:ty:
1 |
~/git/testpipenv > pipenv check . |
2 |
Checking PEP 508 requirements… |
3 |
Passed! |
4 |
Checking installed package safety… |
5 |
All good! |