Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Pipfile, pipenv, pip… what?!

Pipfile, pipenv, pip… what?!

Иван Чернов (Ostrovok.ru) @ Moscow Python Meetup 50

"Python имеет всеми нами любимый пакетный менеджер pip, но прогресс не стоит на месте и сообщество сделало новый удобный иструмент pipenv. Давайте посмотрим на него и ответим пора ли на него переходить?"
Видео: http://www.moscowpython.ru/meetup/50/pip-what/

Moscow Python Meetup

November 23, 2017
Tweet

More Decks by Moscow Python Meetup

Other Decks in Programming

Transcript

  1. About me • Python developer @ Ostrovok.ru • Maintain and

    develop: ◦ Site backend ◦ Email marketing platform ◦ Booking service
  2. Agenda • Your project workflow • Improve it with <spoiler>

    • Why we need Pipfile? • Quick intro to pipenv • Summary
  3. Setup your project $ pip install django $ pip freeze

    > requirements.txt $ cat requirements.txt
  4. Setup your project $ pip install django $ pip freeze

    > requirements.txt $ cat requirements.txt ... docker-compose==1.16.1 docker-pycreds==0.2.1 Django==1.11.5 ...
  5. >_< Virtualenv first $ python -m venv .venv $ .

    .venv/bin/activate (.venv) $ pip install django
  6. >_< Virtualenv first $ python -m venv .venv $ .

    .venv/bin/activate (.venv) $ pip install django (.venv) $ pip freeze > requirements.txt
  7. >_< Virtualenv first $ python -m venv .venv $ .

    .venv/bin/activate (.venv) $ pip install django (.venv) $ pip freeze > requirements.txt (.venv) $ cat requirements.txt
  8. >_< Virtualenv first $ python -m venv .venv $ .

    .venv/bin/activate (.venv) $ pip install django (.venv) $ pip freeze > requirements.txt (.venv) $ cat requirements.txt Django==1.11.5 pytz==2017.2
  9. Deps question II $ cat requirements.txt | wc -l 125

    • Are all of deps pinned? • No conflicts in sub-deps?
  10. Deps question III $ cat requirements.txt | wc -l 125

    • Are all of deps pinned? • No conflicts in sub-deps? • How to view dependency tree?
  11. Deps question IV $ cat requirements.txt | wc -l 125

    • Are all of deps pinned? • No conflicts in sub-deps? • How to view dependency tree? • How to divide prod/dev/test deps?
  12. Intro to pip-tools $ [sudo] pip install pip-tools $ echo

    django >> requirements.in $ pip-compile --output-file requirements.txt requirements.in
  13. Intro to pip-tools $ [sudo] pip install pip-tools $ echo

    django >> requirements.in $ pip-compile --output-file requirements.txt requirements.in $ cat requirements.txt
  14. Intro to pip-tools $ [sudo] pip install pip-tools $ echo

    django >> requirements.in $ pip-compile --output-file requirements.txt requirements.in $ cat requirements.txt django==1.11.5 pytz==2017.2 # via django
  15. Intro to pip-tools $ [sudo] pip install pip-tools $ echo

    django >> requirements.in $ pip-compile --output-file requirements.txt requirements.in $ cat requirements.txt django==1.11.5 pytz==2017.2 # via django
  16. Moving to libraries • We have setuptools • Which is

    used in setup.py • And what if setup.py contains external deps?
  17. Moving to libraries • We have setuptools • Which is

    used in setup.py • And what if setup.py contains external deps? • Pip will fail to install
  18. Pipfile (PEP 518) • Rationale: give dev ability to specify

    build system • Implementation: ◦ toml file in root called Pipfile ◦ json file for locking Pipfile.lock ◦ WIP installation through pip install -p
  19. Rejected formats (from PEP) • JSON - human-readable, but not

    human-editable • Configparser - Python stdlib, but confusing
  20. Rejected formats (from PEP) • JSON - human-readable, but not

    human-editable • Configparser - Python stdlib, but confusing • Python literals (dict) - Cannot be parsed in other languages
  21. Rejected formats (from PEP) • JSON - human-readable, but not

    human-editable • Configparser - Python stdlib, but confusing • Python literals (dict) - Cannot be parsed in other languages • YAML
  22. Rejected formats (from PEP) • JSON - human-readable, but not

    human-editable • Configparser - Python stdlib, but confusing • Python literals (dict) - Cannot be parsed in other languages • YAML ◦ Specification == 86 pages (== too difficult)
  23. Rejected formats (from PEP) • JSON - human-readable, but not

    human-editable • Configparser - Python stdlib, but confusing • Python literals (dict) - Cannot be parsed in other languages • YAML ◦ Specification == 86 pages (== too difficult) ◦ Is not safe by default (command execution vuln.)
  24. Rejected formats (from PEP) • JSON - human-readable, but not

    human-editable • Configparser - Python stdlib, but confusing • Python literals (dict) - Cannot be parsed in other languages • YAML ◦ Specification == 86 pages (== too difficult) ◦ Is not safe by default (command execution vuln.) ◦ Pip will need to vendor PyYAML (~1k LOC Python & C code)
  25. Pipfile II [packages] requests = '>2' django = { git

    = 'https://github.com/django/django.git', ref = '1.11.5', editable = true } pywinusb = { version = "*", os_name = "=='nt'", index="pypi"} [dev-packages] pytest = '*'
  26. Pipfile II [packages] requests = '>2' django = { git

    = 'https://github.com/django/django.git', ref = '1.11.5', editable = true } pywinusb = { version = "*", os_name = "=='nt'", index="pypi"} [dev-packages] pytest = '*'
  27. Pipfile II [packages] requests = '>2' django = { git

    = 'https://github.com/django/django.git', ref = '1.11.5', editable = true } pywinusb = { version = "*", os_name = "=='nt'", index="pypi"} [dev-packages] pytest = '*'
  28. Pipfile II [packages] requests = '>2' django = { git

    = 'https://github.com/django/django.git', ref = '1.11.5', editable = true } pywinusb = { version = "*", os_name = "=='nt'", index="pypi"} [dev-packages] pytest = '*'
  29. Pipfile II [packages] requests = '>2' django = { git

    = 'https://github.com/django/django.git', ref = '1.11.5', editable = true } pywinusb = { version = "*", os_name = "=='nt'", index="pypi"} [dev-packages] pytest = '*'
  30. What is Pipenv? • Sacred Marriage of Pipfile, Pip, Pip-tools,

    & Virtualenv. • From Kenneth Reitz: requests, autoenv • Automatically manages ◦ Pipfile ◦ Pipfile.lock ◦ virtualenv
  31. Custom pypi fail :( $ cd your/project/dir $ pipenv install

    Requirements.txt found, instead of Pipfile! Converting… # but for custom pypi, there will be error on lock :( $ sed -i s/pypi.python.org/pypi.example.org/g Pipfile
  32. How to use with Docker? $ pipenv lock # In

    Dockerfile WORKDIR /your/dir/ COPY Pipfile Pipfile.lock /your/dir/ RUN pipenv install --system # to install libs in system folder
  33. Summary Pros • All your deps are locked • All

    your venvs are in .local • Ability to separate dev/prod Cons
  34. Summary Pros • All your deps are locked • All

    your venvs are in .local • Ability to separate dev/prod • Pip will support this file Cons
  35. Summary Pros • All your deps are locked • All

    your venvs are in .local • Ability to separate dev/prod • Pip will support this file Cons • Not mature :(
  36. Summary Pros • All your deps are locked • All

    your venvs are in .local • Ability to separate dev/prod • Pip will support this file Cons • Not mature :( • Dockerfile will miss cache
  37. Summary Pros • All your deps are locked • All

    your venvs are in .local • Ability to separate dev/prod • Pip will support this file Cons • Not mature :( • Dockerfile will miss cache • Harder to update package
  38. Links • PEP https://www.python.org/dev/peps/pep-0518/ • Pipfile repo https://github.com/pypa/pipfile • Pipenv

    repo https://github.com/pypa/pipfile • Cheetsheet https://robots.thoughtbot.com/how-to-manage-your-python-projects-with- pipenv