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

Ruby and Python: Packaging and Environment

Ruby and Python: Packaging and Environment

Ruby and Python are similar languages, but have different approaches to packaging, language runtime switching and dependency management. This talk provides an overview the similarities and differences.


Jimmy Cuadra

July 10, 2014


  1. Ruby and Python Packaging and Environment Jimmy Cuadra

  2. Packaging concerns

  3. 1. Creation, Discovery, Installation

  4. 2. Dependency Specification

  5. 3. Environment Isolation

  6. Creation, Discovery, Installation

  7. Creation P Y T H O N

  8. distutils ! ! P Y T H O N

  9. distutils setuptools ! P Y T H O N

  10. distutils setuptools distribute P Y T H O N

  11. distutils setuptools distribute setuptools P Y T H O N

  12. # setup.py from setuptools import setup ! setup( name=‘my_package’, version=‘1.0.0’,

    description=‘Does stuff!’, ) P Y T H O N
  13. $ python setup.py sdist upload P Y T H O

  14. Discovery & Installation P Y T H O N

  15. None
  16. easy_install (setuptools) P Y T H O N

  17. easy_install (setuptools) pip P Y T H O N

  18. $ pip search flask Flask - A microframework based on

    Werkzeug, Jinja2 and good intentions clay-flask - Clay is a framework for building RESTful backend services using best practices. Flask-RESTful - Simple framework for creating REST APIs P Y T H O N
  19. $ pip install Flask Downloading/unpacking Flask Downloading Flask-0.10.1.whl (115kB): 115kB

    downloaded ! ... ! Successfully installed flask itsdangerous Werkzeug Jinja2 markupsafe Cleaning up... P Y T H O N
  20. Creation, Discovery & Installation R U B Y

  21. RubyGems does it all R U B Y

  22. R U B Y # my_gem.gemspec Gem::Specification.new do |spec| spec.name

    = “my_gem” spec.version = “1.0.0” spec.description = “Does stuff!” end
  23. $ gem build my_gem.gemspec $ gem push pkg/my_gem-1.0.0.gem R U

    B Y
  24. None
  25. R U B Y $ gem search rails ! ***

    REMOTE GEMS *** ! rails (4.1.4) rails-api (0.2.1) rails_admin (0.6.2) ...
  26. R U B Y $ gem install rails ! Fetching:

    thread_safe-0.3.4.gem (100%) Successfully installed thread_safe-0.3.4 ... Fetching: sprockets-2.12.1.gem (100%) Successfully installed sprockets-2.12.1 Fetching: sprockets-rails-2.1.3.gem (100%) Successfully installed sprockets-rails-2.1.3 Fetching: rails-4.1.4.gem (100%)
  27. Dependency Specification

  28. Dependency Specification P Y T H O N

  29. pip P Y T H O N

  30. requirements.txt P Y T H O N

  31. $ pip freeze -l > requirements.txt P Y T H

    O N
  32. # requirements.txt coverage==3.7.1 flake8==2.1.0 mock==1.0.1 pyroma==1.6 pytest==2.5.2 nose==1.3.3 Sphinx==1.2.2 sphinx-rtd-theme==0.1.6

    P Y T H O N
  33. $ pip install -r requirements.txt P Y T H O

  34. Requirements file per environment P Y T H O N

  35. Dependency Specification R U B Y

  36. Bundler R U B Y

  37. Gemfile Gemfile.lock R U B Y

  38. R U B Y # Gemfile source 'https://rubygems.org' ! gem

    'rails' ! group :test do gem 'rspec' end
  39. R U B Y $ bundle Fetching gem metadata from

    https://rubygems.org/......... Fetching additional metadata from https://rubygems.org/.. Resolving dependencies... Installing rake 10.3.2 Installing i18n 0.6.11 ... Installing rails 4.1.4 Installing rspec 3.0.0 Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
  40. R U B Y # Gemfile.lock GEM remote: https://rubygems.org/ specs:

    rails (4.1.4) actionmailer (= 4.1.4) actionpack (= 4.1.4) ... railties (= 4.1.4) sprockets-rails (~> 2.0) rspec (3.0.0) rspec-core (~> 3.0.0) rspec-expectations (~> 3.0.0) rspec-mocks (~> 3.0.0)
  41. R U B Y # Gemfile source 'https://rubygems.org' ! gem

    ‘rails’, ‘~> 4.0.5’ ! group :test do gem 'rspec' end
  42. $ bundle update rails R U B Y

  43. $ bundle --without development:test R U B Y

  44. Environment Isolation

  45. Locking the Runtime P Y T H O N

  46. virtualenv P Y T H O N

  47. $ pip install virtualenv P Y T H O N

  48. $ virtualenv env P Y T H O N

  49. $ source env/bin/activate P Y T H O N

  50. $ deactivate P Y T H O N

  51. Using a different version of Python P Y T H

    O N
  52. $ /path/to/python -m virtualenv env P Y T H O

  53. Does not install Python P Y T H O N

  54. Other options P Y T H O N

  55. virtualenvwrapper P Y T H O N

  56. pyenv P Y T H O N

  57. Locking the Runtime R U B Y

  58. rbenv + ruby-build R U B Y

  59. $ brew install rbenv ruby-build R U B Y

  60. $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile R U B Y

  61. $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile R U

    B Y
  62. $ rbenv install 2.1.2 R U B Y

  63. $ rbenv global 2.1.2 R U B Y

  64. $ cd my_ruby_project $ echo ‘2.1.2’ > .ruby-version R U

    B Y
  65. Other options R U B Y

  66. RVM R U B Y

  67. chruby R U B Y

  68. Isolating Dependencies P Y T H O N

  69. virtualenv + pip P Y T H O N

  70. $ pip install -r requirements.txt P Y T H O

  71. Updating dependencies P Y T H O N

  72. Edit requirements.txt and rerun pip install P Y T H

    O N
  73. Previous versions of packages are overwritten P Y T H

    O N
  74. Multiple projects with the same version of Python P Y

    T H O N
  75. Multiple virtualenvs P Y T H O N

  76. Python version is lost when sharing P Y T H

    O N
  77. Isolating Dependencies R U B Y

  78. Multiple versions of the same gem installed at the same

    time R U B Y
  79. Bundler R U B Y

  80. Uses the Gemfile.lock to activate the correct versions R U

    B Y
  81. $ bundle exec rails R U B Y

  82. Requires you to constantly prefix commands R U B Y

  83. $ bundle --binstubs $ bin/rails R U B Y

  84. Does not conflate runtime version with dependency isolation R U

    B Y
  85. Runtime version and dependency versions are easily shared R U

    B Y
  86. Recap

  87. setuptools + pip pip virtualenv R U B Y RubyGems

    Bundler rbenv + Bundler P Y T H O N
  88. Thanks @jimmycuadra