e d - environmental dependencies in python
TRANSCRIPT
Building Highly Complex SystemsMany 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
Building Open Source Business SystemsUsing 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
Python ToolsCore 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
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
setuptoolsProject dependency management
Image by ParentingPatch (Own work) [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons
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
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' ],)
pipEnvironment dependency management
Image © Dietmar Rabich, rabich.de - Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=34239265
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.compycrypto==2.6.1requests==2.10.0six==1.10.0
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.
Virtual EnvironmentsDifferent 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
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
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
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
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
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.
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
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