Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

E D - Environmental Dependencies in Python

182 views

Published on

Overview of dependency management and virtual environments in python. This a very high level primer with links for further reading.

Published in: Software
  • Be the first to comment

  • Be the first to like this

E D - Environmental Dependencies in Python

  1. 1. E D ENVIRONMENTAL DEPENDENCIES IN PYTHON
  2. 2. Building Highly Complex Systems Many pieces acting as one Image by Alan_D from Crawley, United Kingdom - Nissan GTR Cutaway, CC BY 2.0, https://commons.wikimedia.org/w/index.php?curid=18072247
  3. 3. Building Open Source Business Systems Using what you can find to build what you need Image byy Mick from England (Go Kart Buggy) [CC BY 2.0 (http://creativecommons.org/licenses/by/2.0)], via Wikimedia Commons
  4. 4. Python Tools Core tools for dependency management in Python Image by Alexander Baxevanis (Flickr: Cooper's tools) [CC BY 2.0 (http://creativecommons.org/licenses/by/2.0)], via Wikimedia Commons
  5. 5. One Language Multiple Tools Dependency Management  Easy Install – deprecated  Distutils – deprecated  Setuptools – project dependencies  PIP – environment dependencies Environment Management  venv – Python 3.3+  Virtualenv – Virtual environments  Virtualenvwrapper – Enhancements for Virtualenv
  6. 6. setuptools Project dependency management Image by ParentingPatch (Own work) [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons
  7. 7. Setuptools – Project Dependency Tool  Defines package information: name, version, authors, etc.  Defines package and test dependency requirements for project source  Should define as wide a range as possible for dependency versions  Builds distributions  Publishes distributions to PyPI  Defines scripts to install as binaries  Provides runtime support for C modules  Can be used for test bootstrapping
  8. 8. Project Dependency LaunchKey SDK pycrypto requests six
  9. 9. Setuptools setup.py … setup(name='launchkey-python', version='1.3.0', … install_requires= [ 'pycrypto >= 2.6.1', 'pycrypto < 3.0.0', 'requests >= 2.5.1', 'requests < 3.0.0', 'six >= 1.10.0', 'six < 2.0.0', ], tests_require=[ 'Mocker==1.1.1', 'mock==1.3.0' ], )
  10. 10. pip Environment dependency management Image © Dietmar Rabich, rabich.de - Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=34239265
  11. 11. PIP Requirements File Overview  Contains information for repeatable installs  Can include command line options  Can use a constraints file for multi-project dependency version overrides. Helpful for using patched forks  Can be built with the freeze command Example --index-url=https://my-pypi.example.com pycrypto==2.6.1 requests==2.10.0 six==1.10.0
  12. 12. PIP Freeze $ pip freeze > requirements.txt cat requirements.txt launchkey-python==1.3.0 pycrypto==2.6.1 requests==2.10.0 six==1.10.0 Use the PIP freeze command to generate a requirements file based on the currently installed versions.
  13. 13. Virtual Environments Different parts for different projects Image by EncMstr (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html) or CC BY-SA 4.0-3.0-2.5-2.0-1.0 (http://creativecommons.org/licenses/by-sa/4.0-3.0-2.5-2.0-1.0)], via Wikimedia Commons
  14. 14. Virtualenv  Can be installed via PIP  Allows you to switch between “virtual” environments  Virtual environments are full copies with symbolically linked, or optionally copied, python binaries including pip.  Private set of environment variables  Private set of installed packages  Can be told which Python executable to use  Environments can be easily activated and deactivated  Deleting created directory remove virtual environments
  15. 15. Usage $ which python /usr/local/bin/python $ virtualenv --prompt "(sdk) ” .venv … Installing setuptools, pip, wheel...done. $ source .venv/bin/activate (sdk) $ which python /.venv/bin/python (sdk) $ deactivate $ which python /usr/local/bin/python Simple example for creating, activating, and deactivating virtual environments with Virtualenv
  16. 16. Virtualenvwrapper  Can be installed via PIP  Adds additional functionality to Virtualenv  Uses named rather than location based environments  Places reusable environments in user directory  Auto-complete for enabling and removing environments  Defaults prompt prefix to environment name  If directory is same name as project. Enable with directory location
  17. 17. Usage $ which python /usr/local/bin/python $ mkvirtualenv example … Installing setuptools, pip, wheel...done. (example) $ which python ~/.virtualenvs/example/bin/python (sdk) $ deactivate $ which python /usr/local/bin/python Simple example for creating, activating, and deactivating virtual environments
  18. 18. Pyenv  Manage any number of Python executable versions  Great for open source projects that need to run on multiple versions  Has Virtualenv support via an add-on to support mixing Python executable versions and virtual environments in a manner similar to rbenv.  On Mac, make sure XCode and Command Line Developer Tools are up to date.
  19. 19. Usage $ which python3 $ pyenv install 3.5.2 Downloading Python-3.5.2.tgz... -> https://www.python.org/ftp/python/3.5.2/Python- 3.5.2.tgz Installing Python-3.5.2... Installed Python-3.5.2 to ~/.pyenv/versions/3.5.2 $ pyenv global 3.5.2 $ python3 --version Python 3.5.2 Simple example for installing and using a Python version
  20. 20. Further Reading  Packaging: https://packaging.python.org/  Python Versioning: https://www.python.org/dev/peps/pep-0440/  Setuptools: https://setuptools.readthedocs.io/en/latest/  PIP: https://pip.pypa.io/en/stable/  PIP Version Resolving: https://github.com/pypa/pip/issues/988  Virtualenv: https://virtualenv.pypa.io/en/stable/  Virtualenvwrapper: http://virtualenvwrapper.readthedocs.io/en/latest/index.html  Pyenv: https://github.com/yyuu/pyenv

×