Arbeiten mit distribute, pip und virtualenv

Post on 23-Jan-2015

1.833 views 2 download

description

distribute und pip als Ersatz für setuptools und easy_install bieten im Zusammenspiel mit virtualenv viele neue Möglichkeiten bei der Entwicklung und dem Deployment von Python-Applikationen. In diesem Vortrag stelle ich alle Werkzeuge kurz vor und zeige, wie man sie zusammen einsetzen kann.

Transcript of Arbeiten mit distribute, pip und virtualenv

Arbeiten mit distribute, pip und virtualenv

Markus Zapke-Gründemann11. DZUG-Tagung zu Zope, Plone und Python

MarkusZapke-Gründemann

• Softwareentwickler seit 2001

• Schwerpunkt: Web Application Development mit Python und PHP

• Django, symfony & Zend Framework

• Freier Softwareentwickler und Berater seit 2008

• www.keimlink.de

Überblick• Vorstellung der Werkzeuge

• distribute

• distutils2

• pip

• virtualenv

• virtualenvwrapper

• Einsatz in der Praxis

• modern-package-template

• virtualenv

• virtualenvwrapper

• pip

Vorstellung der Werkzeuge

Python Packaging

Quelle: http://guide.python-distribute.org/introduction.html#current-state-of-packaging

distribute

• Fork des setuptools Pakets

• Erweitert distutils

• Stellt setuptools für Pakete zur Verfügung, die davon abhängen

• Python 3 Unterstützung

• Maintainer: Tarek Ziadé

distribute installieren

$ wget http://python-distribute.org/distribute_setup.py$ python distribute_setup.py

1. Entfernen der bestehenden setuptools Installation2. Installieren einer Fake setuptools Installation3. Installation von distribute

distutils2

• Tarek Ziadé: „A toolbox for third packaging tools“

• PEP 345: Metadata for Python Software Packages 1.2

• PEP 376: Database of Installed Python Distributions

• PEP 386: Changing the version comparison module in Distutils

pip• „pip installs packages“

• Ersatz für easy_install

• Kann Pakete wieder deinstallieren

• Nur vollständige Installationen werden durchgeführt

• „Sinnvolle“ Ausgaben

• Protokollieren der Aktionen

• Kann einen URL oder ein VCS als Quelle benutzen

• „requirements“

• Unterstüzung für virtualenv

• Autor: Ian Bicking

$ easy_install pip

Design done by Idan Gazit (http://pixane.com) - License: cc-by-3.0

virtualenv

• Isolierte Python Umgebungen

• Unterstützung für distribute

• Kann mit WSGI Containern benutzt werden

• Autor: Ian Bicking

$ pip install virtualenv

virtualenv: Isolierte Python Umgebungen

• Python Version wählbar (zum Teil als Symlink)

• Eigenes bin Verzeichnis

• Eigenes site-packages Verzeichnis

virtualenv: Strukturtestenv/|-- bin| |-- activate| |-- activate_this.py| |-- easy_install| |-- easy_install-2.6| |-- pip| |-- python| `-- python2.6 -> python|-- include| `-- python2.6 -> /PREFIX/include/python2.6`-- lib `-- python2.6 |-- UserDict.py -> /PREFIX/lib/python2.6/UserDict.py |-- ... |-- distutils |-- ... |-- orig-prefix.txt |-- ... |-- site-packages |-- site.py |-- ... `-- warnings.py -> /PREFIX/lib/python2.6/warnings.py

virtualenvwrapper

• Erweiterung für virtualenv

• Erleichtern das Erstellen, Löschen und Management der virtualenvs

• Autor: Doug Hellmann

$ pip install virtualenvwrapper$ mkdir ~/.virtualenvs$ export WORKON_HOME=$HOME/.virtualenvs$ source /usr/local/bin/virtualenvwrapper.sh

Einsatz in der Praxis

modern-package-template

modern-package-template

• PasteScript Template

• distribute

• buildout

• Autor: Sridhar Ratnakumar

$ pip install modern-package-template

modern-package-template$ paster create -t modern_package testpackageSelected and implied templates: modern-package-template#modern_package Package with distribute and buildout support

Variables: egg: testpackage package: testpackage project: testpackageEnter version (Version (like 0.1)) ['0.1']: Enter description (One-line description of the package) ['']: A test package.Enter keywords (Space-separated keywords/tags) ['']: test packageEnter author (Author name) ['']: Markus Zapke-GründemannEnter author_email (Author email) ['']: info@keimlink.deEnter url (URL of homepage) ['']: http://www.keimlink.de/Enter license_name (License name) ['']: BSDCreating template modern_packageCreating directory ./testpackage Copying HACKING.txt to ./testpackage/HACKING.txt Copying MANIFEST.in to ./testpackage/MANIFEST.in Copying NEWS.txt to ./testpackage/NEWS.txt Copying README.rst to ./testpackage/README.rst Copying bootstrap.py to ./testpackage/bootstrap.py Copying buildout.cfg_tmpl to ./testpackage/buildout.cfg Copying setup.py_tmpl to ./testpackage/setup.pyRunning /usr/bin/python setup.py egg_info

modern-package-templateVerzeichnisstrukturtestpackage/|-- .gitignore|-- .hgignore|-- HACKING.txt|-- MANIFEST.in|-- NEWS.txt|-- README.rst|-- bootstrap.py|-- buildout.cfg|-- setup.py`-- src |-- testpackage `-- testpackage.egg-info

buildoutdistribute

virtualenv

virtualenv anlegen

zappi ~$ virtualenv testenvNew python executable in testenv/bin/pythonInstalling setuptools............done.

zappi ~$ virtualenv --distribute testenvNew python executable in testenv/bin/pythonInstalling distribute.................................................................................................................................................................................done.

$ export VIRTUALENV_USE_DISTRIBUTE=1

virtualenv aktivierenund deaktivieren

zappi ~/$ cd testenvzappi ~/testenv$ . bin/activate(testenv)zappi ~/testenv$ deactivatezappi ~/testenv$

virtualenvwrapper

mkvirtualenv

zappi ~$ mkvirtualenv testenvNew python executable in testenv/bin/pythonInstalling setuptools............done.virtualenvwrapper.user_scripts Creating /Users/zappi/.virtualenvs/testenv/bin/predeactivatevirtualenvwrapper.user_scripts Creating /Users/zappi/.virtualenvs/testenv/bin/postdeactivatevirtualenvwrapper.user_scripts Creating /Users/zappi/.virtualenvs/testenv/bin/preactivatevirtualenvwrapper.user_scripts Creating /Users/zappi/.virtualenvs/testenv/bin/postactivatevirtualenvwrapper.user_scripts Creating /Users/zappi/.virtualenvs/testenv/bin/get_env_details(testenv)zappi ~$

mkvirtualenvzappi ~$ mkvirtualenv --no-site-packages --distribute testenvNew python executable in testenv/bin/pythonInstalling distribute.................................................................................................................................................................................done.virtualenvwrapper.user_scripts Creating /Users/zappi/.virtualenvs/testenv/bin/predeactivatevirtualenvwrapper.user_scripts Creating /Users/zappi/.virtualenvs/testenv/bin/postdeactivatevirtualenvwrapper.user_scripts Creating /Users/zappi/.virtualenvs/testenv/bin/preactivatevirtualenvwrapper.user_scripts Creating /Users/zappi/.virtualenvs/testenv/bin/postactivatevirtualenvwrapper.user_scripts Creating /Users/zappi/.virtualenvs/testenv/bin/get_env_details(testenv)zappi ~$

cdvirtualenv

(testenv)zappi ~$ cdvirtualenv (testenv)zappi ~/.virtualenvs/testenv$

lssitepackages

(testenv)zappi ~$ lssitepackages distribute-0.6.10-py2.6.egg easy-install.pth pip-0.7.1-py2.6.egg setuptools.pth

(testenv)zappi ~$ lssitepackages -ltotal 8drwxr-xr-x 10 zappi zappi 340 Aug 10 19:22 distribute-0.6.10-py2.6.egg-rw-r--r-- 1 zappi zappi 237 Aug 10 19:22 easy-install.pthdrwxr-xr-x 4 zappi zappi 136 Aug 10 19:22 pip-0.7.1-py2.6.egg-rw-r--r-- 1 zappi zappi 30 Aug 10 19:22 setuptools.pth

cdsitepackages

(testenv)zappi ~$ cdsitepackages (testenv)zappi ~/.virtualenvs/testenv/lib/python2.6/site-packages$ ls -ltotal 8drwxr-xr-x 10 zappi zappi 340 Aug 10 19:22 distribute-0.6.10-py2.6.egg-rw-r--r-- 1 zappi zappi 237 Aug 10 19:22 easy-install.pthdrwxr-xr-x 4 zappi zappi 136 Aug 10 19:22 pip-0.7.1-py2.6.egg-rw-r--r-- 1 zappi zappi 30 Aug 10 19:22 setuptools.pth

add2virtualenv

(testenv)zappi ~/Projekte/Python$ add2virtualenv django-devWarning: Converting "django-dev" to "/Users/zappi/Projekte/Python/django-dev"(testenv)zappi ~/Projekte/Python$ lssitepackages distribute-0.6.10-py2.6.egg easy-install.pth pip-0.7.1-py2.6.egg setuptools.pth virtualenv_path_extensions.pth

virtualenv_path_extensions.pth:/Users/zappi/Projekte/Python/django-dev

deactivate

(testenv)zappi ~$ deactivate zappi ~$

workon

zappi ~$ workon testenv(testenv)zappi ~$

rmvirtualenv

(testenv)zappi ~$ rmvirtualenv testenvERROR: You cannot remove the active environment ('testenv').Either switch to another environment, or run 'deactivate'.(testenv)zappi ~$ deactivate zappi ~$ rmvirtualenv testenv

Hooks~/.virtualenvs/testenv/bin/|-- get_env_details|-- postactivate|-- postdeactivate|-- preactivate`-- predeactivate

~/.virtualenvs/|-- get_env_details|-- initialize|-- postactivate|-- postdeactivate|-- postmkvirtualenv|-- postrmvirtualenv|-- preactivate|-- predeactivate|-- premkvirtualenv`-- prermvirtualenv

pip

pip install$ pip install MarkdownDownloading/unpacking Markdown Downloading Markdown-2.0.3.zip (94Kb): 94Kb downloaded Running setup.py egg_info for package MarkdownInstalling collected packages: Markdown Running setup.py install for Markdown changing mode of build/scripts-2.6/markdown from 644 to 755 changing mode of /Users/zappi/.virtualenvs/testenv/bin/markdown to 755Successfully installed MarkdownCleaning up...

pip install$ pip install Django==1.1.2

$ pip install http://effbot.org/downloads/Imaging-1.1.6.tar.gz

$ pip install -e svn+http://svn.myproject.org/svn/MyProject/trunk#egg=MyProject

$ pip install -e svn+http://svn.myproject.org/svn/MyProject/trunk@2019#egg=MyProject

$ pip install --upgrade celery

pip install$ cat requirements.txt Django==1.1.2nose==0.11-e svn+http://mycompany/svn/MyApp/trunk@1045#egg=MyApp-e svn+http://mycompany/svn/MyLibrary/trunk@1058#egg=MyLibrary

$ pip install -r requirements.txtDownloading/unpacking nose==0.11 (from -r requirements.txt (line 2)) Downloading nose-0.11.0.tar.gz (249Kb): 249Kb downloaded Running setup.py egg_info for package nose no previously-included directories found matching 'doc/.build'Downloading/unpacking Django==1.1.2 (from -r requirements.txt (line 1)) Downloading Django-1.1.2.tar.gz (5.7Mb): 5.7Mb downloaded Running setup.py egg_info for package DjangoInstalling collected packages: Django, nose Running setup.py install for Django changing mode of build/scripts-2.6/django-admin.py from 644 to 755 changing mode of /Users/zappi/.virtualenvs/devenv/bin/django-admin.py to 755 Running setup.py install for nose no previously-included directories found matching 'doc/.build' Installing nosetests-2.6 script to /Users/zappi/.virtualenvs/devenv/bin Installing nosetests script to /Users/zappi/.virtualenvs/devenv/binSuccessfully installed Django noseCleaning up...

pip install$ pip install -E ~/.virtualenvs/devenv -r requirements.txtDownloading/unpacking nose==0.11 (from -r requirements.txt (line 2)) Downloading nose-0.11.0.tar.gz (249Kb): 249Kb downloaded Running setup.py egg_info for package nose no previously-included directories found matching 'doc/.build'Downloading/unpacking Django==1.1.2 (from -r requirements.txt (line 1)) Downloading Django-1.1.2.tar.gz (5.7Mb): 5.7Mb downloaded Running setup.py egg_info for package DjangoInstalling collected packages: Django, nose Found existing installation: Django 1.1.1 Not uninstalling Django at /Library/Python/2.6/site-packages, outside environment /Users/zappi/.virtualenvs/devenv/bin/.. Running setup.py install for Django changing mode of build/scripts-2.6/django-admin.py from 644 to 755 changing mode of /Users/zappi/.virtualenvs/devenv/bin/django-admin.py to 755 Found existing installation: nose 0.11.1 Not uninstalling nose at /Library/Python/2.6/site-packages, outside environment /Users/zappi/.virtualenvs/devenv/bin/.. Running setup.py install for nose no previously-included directories found matching 'doc/.build' Installing nosetests-2.6 script to /Users/zappi/.virtualenvs/devenv/bin Installing nosetests script to /Users/zappi/.virtualenvs/devenv/binSuccessfully installed Django noseCleaning up...

pip install

$ pip install anyjson booDownloading/unpacking anyjson Downloading anyjson-0.2.4.tar.gz Running setup.py egg_info for package anyjsonDownloading/unpacking boo Could not find any downloads that satisfy the requirement booNo distributions at all found for booStoring complete log in /Users/zappi/.pip/pip.log

pip uninstall

$ pip uninstall MarkdownUninstalling Markdown: /Users/zappi/.virtualenvs/testenv/bin/markdown /Users/zappi/.virtualenvs/testenv/lib/python2.6/site-packages/Markdown-2.0.3-py2.6.egg-info /Users/zappi/.virtualenvs/testenv/lib/python2.6/site-packages/markdownProceed (y/n)? y Successfully uninstalled Markdown

pip & PyPI Mirrors

$ pip install --use-mirrors Django

oder

$ export PIP_USE_MIRRORS=true

PEP 381: Mirroring infrastructure for PyPI

pip freeze$ pip freezeDjango==1.2.1amqplib==0.6.1anyjson==0.2.4billiard==0.3.1carrot==0.10.5celery==1.0.5distribute==0.6.10django-debug-toolbar==0.8.3django-picklefield==0.1.6importlib==1.0.2python-dateutil==1.5wsgiref==0.1.2

$ pip freeze > requirements.txt

pip search$ pip search markdownMarkdoc - A lightweight Markdown-based wiki build tool.markdown2 - markdown2: A fast and complete Python implementaion of Markdown.Argot - argot text markup -- a markdown dialectFlask-Markdown - Small extension to make using markdown easyMarkdown - Python implementation of Markdown. INSTALLED: 2.0.3 (latest)django-markdown2 - This is a simple app, which supplies a single template tag for markdown markup.tiddlywebplugins.markdown - Markdown rendering for TiddlyWebdiscount - A Python interface for Discount, the C Markdown parsertiddlywebplugins.simplewiki - A simple markdown based wiki in TiddlyWebdjango-markdown-deux - a Django app that provides template tags for using Markdown (using the python-markdown2 processor)pymarkdown_minisite - Parse a list of markdown files to a website with index.html2text - Turn HTML into equivalent Markdown-structured text.markdown2latex - Extensiondjango-autolinks - App for storing links and automatic link extraction from markdown texts.collective.transform.multimarkdown - MultiMarkdown text transform for Plone

pip completion

$ pip completion --bash >> ~/.profile

$ pip completion --zsh >> ~/.zprofile

Links

• http://pypi.python.org/pypi/distribute

• http://guide.python-distribute.org/

• http://pip.openplans.org/

• http://pypi.python.org/pypi/virtualenv

• http://pypi.python.org/pypi/virtualenvwrapper

• http://pypi.python.org/pypi/modern-package-template

Lizenz

Dieses Werk ist unter einem Creative Commons Namensnennung-Weitergabe unter gleichen Bedingungen 3.0

Unported Lizenzvertrag lizenziert. Um die Lizenz anzusehen, gehen Sie bitte zu

http://creativecommons.org/licenses/by-sa/3.0/ oder schicken Sie einen Brief an Creative Commons, 171 Second Street,

Suite 300, San Francisco, California 94105, USA.