• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Virtualenv
 

Virtualenv

on

  • 1,605 views

by Christian Strappazzon

by Christian Strappazzon

Statistics

Views

Total Views
1,605
Views on SlideShare
1,605
Embed Views
0

Actions

Likes
6
Downloads
28
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Virtualenv Virtualenv Presentation Transcript

    • The hitchhickers guide to PIPand Virtualenv (wrapper)...Maybe...
    • Knowing where ones towel is"Hey, you sass that hoopy Ford Prefect? Theres a frood who reallyknows where his towel is." (Sass: know, be aware of, meet, have sex with; hoopy: really together guy; frood: really amazingly together guy.)
    • Index PIP virtual env r r appe l envwv irtua mabye...and e veryt hing :) wers ans
    • Prerequisites It (maybe) works also in other *nix and with a little effort on● Python :) Windows ;)● linux ( unfortunately or fortunately :P )● comfort with shell● patience required● internet connection ( not now, but we work on the web, in the coils of the Python! )
    • What is PIP? - pip installs packages. Python packages. -pip is a tool for installing andmanaging Python packages, such asthose found in the Python PackageIndex (PyPI). It’s a replacement for easy_install.
    • But... what is Easy Install?Easy Install is a python module(easy_install) bundled withsetuptools that lets youautomatically download, build, install,and manage Python packages. 
    • So what? ea sy _i ns I P taP ll
    • PIP installation Using package manager, the installer or from source (root)# apt-get install python-pip# or (root)# curl http://python-distribute.org/distribute_setup.py | python (root)# curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python# or $ curl -O http://[...]/pip-1.0.tar.gz $ tar xvfz pip-x.y.tar.gz ; cd pip-1.0# or $ git clone https://github.com/pypa/pip.git ; cd pip (root)# python setup.py install
    • PIP usage - base options$ pip install SomePackage pip install /path/to/SomePackage.1.1.1.tar.gz pip install http://myrepo.it/SomePakage-1.4.0.zip pip install -e git+http://github.com/django/django.git#egg=django pip install -e path/to/SomePackage pip install django==1.1.1 ==, >=, >, <, <=$ pip install --upgrade package-name$ pip uninstall package-name another way for querying installed packages given from$ pip search "query" yolk$ pip freeze > requirements.txt Django==1.1.2 ipdb==0.2$ pip install -r requirements.txt ipython==0.10.1 ...
    • PIP configuration - quickest look -*nix $HOME/.pip/pip.conf [global] timeout = 60windows %HOME%pippip.ini [freeze] timeout = 10$ export PIP_DEFAULT_TIMEOUT=10 [install]$ pip install django find-links = http://mirror.com Env. variables override config file Each command line options have a PIP_<UPPER_NAME> environment variables$ pip --default-timeout=60 install django
    • PyPI - the Python Pakage Index - PyPI is a repository of software for Python and currently count 20481 packages. The PyPI repository provides alternative locations that store the packages.   [install] use-mirrors = true$ pip --use-mirrors ... mirrors =$ export PIP_USE_MIRRORS=true http://d.pypi.python.org http://b.pypi.python.org You can create your own mirror, following the PEP 381 or using a tool such as pep381client
    • Want to know more? Read the docs! ( Luke! :D ) PIPhttp://www.pip-installer.org/en/latest/index.html
    • WHY virtual environments?● Isolation - Python packages and even version live in their own planet :)● Permissions - No sudoers, the environment is mine!!!● Organization - each project can maintain its own requirements file of Python packages.● No-Globalization - dont require installing stuff globally on the system.
    • PIP + virtualenv● its not recommended install packages globally, but you can :)● pip works fine with virtualenv!● used in conjunction to isolate your installationWell... step forward: setup and play with virtual environments!
    • What is virtualenv?Is a tool to create isolated Pythonenvironments. Every virtualenv has pip installed init automatically (also easy_install)under bin directory. Does not require root access ormodify your system.
    • InstallationUsing package manager, pip, the installer or using single file (root)# apt-get install python-virtualenv# or (root)# pip install virtualenv# or $ curl -O https://raw.github.com/pypa/virtualenv/master/virtualenv.py
    • Basic Usage $ virtualenv ENV This creates a folder ENV in the $PWD Youll find python packages on ENV/lib/pythonX.X/site-packages Interesting options! $ virtualenv --no-site-packages --python=PYTHON_EXE ENVDoesnt inherit global site-packages and use a different Python interpreter
    • Activate and Using ENV$ cd ENV ; . bin/activate(ENV) $ pip install djangoDownloading/unpacking django Downloading Django-1.4.tar.gz (7.6Mb): 5.2Mb downloaded... Also virtualenv has a config file and its looks like pip.conf
    • Extending virtualenv# django-day.pyimport virtualenv, textwrapoutput = virtualenv.create_bootstrap_script(textwrap.dedent())"""def after_install(options, home_dir): subprocess.call([join(home_dir, bin, pip), install, django])"""f = open(django-bootstrap.py, w).write(output)extend_parser(optparse_parser): # add or remove option The script created is anadjust_options(options, args): # change options or args extension to virtualenvafter_install(options, home_dir): and inherit all its options! # after install run this
    • virtualenvwrapper by Doug Hellmann is a set of extensions to Ian Bicking’s virtualenv tool for creating isolated Python development environments.Features, taken verbatim from website:● Organizes all of your virtual environments in one place. ● Wrappers for managing your virtual environments (create, delete, copy). ● Use a single command to switch between environments. ● Tab completion for commands that take a virtual environment as argument. ● User-configurable hooks for all operations. ● Plugin system for more creating sharable extensions. Your homework ;)
    • Installation (root)# pip install virtualenvwrapper $ cd ~ $ mkdir ~/.virtualenvsInclude environment vars needed into ~/.bashrc export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh export PIP_VIRTUALENV_BASE=$WORKON_HOME export PIP_RESPECT_VIRTUALENV=trueA little tips: add to ~/.virtualenvs/postactivate export PYTHONPATH=$PYTHONPATH:$VIRTUAL_ENV
    • Basic Commands A brief introducion to manage virtual enviroment$ mkvirtualenv --no-site-packages --python=PYTHON_EXE ENV # same as virtualenv!$ workon ENV (ENV) $ cdvirtualenv# set virtualenv environment # go to home environment(ENV) $ (ENV) $ deactivate # deactivate current environment $ rmvirtualenv ENV # remove selected environment
    • More Commands :) (ENV) $ lssitepackages # list packages on current environment (ENV) $ cdsitepackages # go to site-packages directory (ENV) $ add2virtualenv directory... # adds the specified directories to the Python path (ENV) $ toggleglobalsitepackages # enable/disable global site-packages Much more commands & options athttp://www.doughellmann.com/docs/virtualenvwrapper/command_ref.html
    • Simple Sample :P$ mkvirtualenv --no-site-packages djangoday-rulez(djangoday-rulez) $ deactivate$ workon djangoday-rulez(djangoday-rulez) $ cdvirtualenv(djangoday-rulez) $ pip install django... do some cool stuff ...(djangoday-rulez) $ lssitepackages(djangoday-rulez) $ deactivate... do other stuff or switch project... --- ...... if its not so cool ;) ...$ rmvirtualenv djangoday-rulez
    • Tips & Hooks virtual environment with "requirements.txt" file $ mkvirtualenv --no-site-packages --python=PYTHON_EXE -r django-requirements.txt $ mkvirtualenv --no-site-packages -r other-requirements.txtUnder ~/.virtualenvs youll find the hook scripts that running pre/postcreate, activate environments. postmkvirtualenvHooks are simply plain-text files with shell commands. prermvirtualenvYou can customize them to change their behavior when an postrmvirtualenv postactivateevent occurs. predeactivate   postdeactivateSee extending virtualenv-wrapper: http://bit.ly/IMP0vh
    • Not entirely unlike... virtualenv   virtualenvwrapper http://bit.ly/141dCr  http://bit.ly/jbFVCe
    • which python ?Latest stable2.4.6, 2.5.6, 2.6.8, 2.7.3Images comes from Wikipedia
    • pythonbrew !pythonbrew is a program to automate thebuilding and installation of Python in theusers $HOME.
    • InstallationRecommended way $ curl -kL http://xrl.us/pythonbrewinstall | bash Add the following line into ~/.bashrc[[ -s $HOME/.pythonbrew/etc/bashrc ]] && source $HOME/.pythonbrew/etc/bashrc Last News!!!The original project seems to be unmantained, but there is a fork https://github.com/saghul/pythonz(forked last week... Wait! I have a presentation to do! :D ) Long live pythonz!
    • Usage Crash course!$ pythonbrew install VERSION $ pythonbrew off # install python VERSION # turn off pythonbrew$ pythonbrew list -k $ pythonbrew use VERSION # list available pythons # use specified python$ pythonbrew list $ pythonbrew uninstall VERSION # list installed pythons # uninstall python VERSION $ pythonbrew switch VERSION # permanently use specified python
    • which python$ pythonbrew use 2.6.5$ which python/home/cstrap/.pythonbrew/pythons/Python-2.5.6/bin/python$ pythonbrew off$ which python/usr/bin/pythonCool! And you can create isolated python environments (it uses virtualenv) ...
    • Create and use pythonbrew Always use the --force, Luke! $ pythonbrew install --force 2.5.6 ... wait... This could take a while... $ pythonbrew use 2.5.6 $ pythonbrew venv init $ pythonbrew venv create djangoday-proj $ pythonbrew venv list # list all environments $ pythonbrew venv use djangoday-proj (djangoday-proj) $ pip install django ... do some cool stuff ... ... if its not so cool ;) ... $ pythonbrew venv delete djangoday-proj
    • Mostly Harmles...● virtualenv & virtualenvwrapper allow you to create and manage environments using the system default Python interpreter● pythonbrew allow you to install different Python interpreter, create and manage virtual environments ...
    • Basic Combo$ pythonbrew install --force 2.6.7 ... wait...$ mkvirtualenv --no-site-packages -p $HOME/.pythonbrew/pythons/Python-2.6.7/bin/python djangoDay ... wait...(djangoDay) $ cdvirtualenv(djangoDay) [djangoDay] $ pip install django... wait...(djangoDay) $ which python ; python -V/home/cstrap/.virtualenvs/djangoDay/bin/pythonPython 2.6.7(djangoDay) [djangoDay] $ deactivate$ which python ; python -V/usr/bin/pythonPython 2.7.2
    • Super Combo$ pythonbrew install --force 2.7.3 ... wait...$ pythonbrew install --force 2.5.6 ... wait...$ pythonbrew use 2.7.3$ pip install virtualenv && pip install virtualenvwrapper# configure .bashrcexport WORKON_HOME=$HOME/.virtualenvsexport VIRTUALENVWRAPPER_PYTHON=$HOME/.pythonbrew/pythons/Python-2.7.3/bin/pythonsource $HOME/.pythonbrew/pythons/Python-2.7.3/bin/virtualenvwrapper.shexport PIP_VIRTUALENV_BASE=$WORKON_HOME$ mkdir ~/.virtualenvs$ mkvirtualenv --no-site-packages -p $HOME/.pythonbrew/pythons/Python-2.5.6/bin/python djangoDay(djangoDay) $ which python ; python -V/home/cstrap/.virtualenvs/djangoDay/bin/pythonPython 2.5.6(djangoDay) [djangoDay] $ deactivate$ which python ; python -V/home/cstrap/.pythonbrew/pythons/Python-2.7.3/bin/pythonPython 2.7.3$ pythonbrew off; python -V# Your current system version of Python
    • Share and Enjoy! :P
    • lab@strap.it @cstrap
    • Answers?Thanks for your patience! ;)