VirtualEnv and Pip - Isolated Python environments
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

VirtualEnv and Pip - Isolated Python environments

  • 3,031 views
Uploaded on

Creating completely separate and isolated Python environments for each project is essential. Pip and VirtualEnv make it easy!

Creating completely separate and isolated Python environments for each project is essential. Pip and VirtualEnv make it easy!

More in: Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
3,031
On Slideshare
3,011
From Embeds
20
Number of Embeds
2

Actions

Shares
Downloads
49
Comments
0
Likes
12

Embeds 20

http://www.linkedin.com 13
https://www.linkedin.com 7

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. VirtualEnv and PipIsolated Python environments Ryan Blunden Brisbane Python Group, May 2011
  • 2. Summary• Global site-packages management• Per project package management• VirtualEnv creates isolated Py envs• VirtualEnvWrapper makes things easy• Pip to install your packages• Next... Automate your deploys
  • 3. About Me• Have been the Front End Tech Lead at Flight Centre• Interested in all things web and am a fan of the new RESTful back-end => thick client movement• Python at work for back office and Systems Admin tasks. Don’t get why SA’s love bash scripts so much• Can’t build Django apps at work so I build them for myself and external clients• I’m hardly a Python expert, but I love it!
  • 4. Global Package Management• Starts out fine• Overtime, things get messy• Not great for development in teams (sync issues)• Challenges in keeping dev, stage and prod environments in sync• Sometimes you don’t even have control of the prod global site packages• Easy for things to break overtime when versions and dependencies not clearly set
  • 5. Per Project Packages• Enter virtualenv, a tool for creating isolated Python environments• Better than workingenv as it doesn’t rely on $PYTHONPATH trickery• Better than virtual-python as it works on Windows (doesn’t rely on symlinks)• Any of these are better than nothing, I just think virtualenv does it best.• Version 1.6.1 supports PyPy 1.5. PyPy!
  • 6. Installing virtualenv• sudo easy_install virtualenv, then add this to your .bashrc• Create a new Python environment • virtualenv myproject --no-site-packages Don’t inherit from global site-packages directory • virtualenv myproject --no-site-packages --distribute Uses distribute instead of setup_tools • I think it’s best to ensure your new env does not inherit packages from your global site-packages directory• This is ok but to make things easier to manage, let’s use virtualenvwrapper (Thanks Doug Hellmann)
  • 7. virtualenvwrapper• Organises your virtualenvs into one spot # Put this in your .bash_profile or .bashrc export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper_bashrc• Separates your virtualenvs from your project source• Nice environment event hooks for automation of repetitive tasks (cd project on post_activate)• Basically, it makes your use of virtualenv more efficient• Now we need the perfect package management tool for installing packages in our env...
  • 8. VirtualEnvWrapper scripts• mkvirtualenv (create a new virtualenv)• rmvirtualenv (remove an existing virtualenv)• workon (change the current virtualenv)• add2virtualenv (add external packages in a .pth file to current virtualenv)• cdsitepackages (cd into the site-packages directory of current virtualenv)• cdvirtualenv (cd into the root of the current virtualenv)• deactivate (deactivate virtualenv, which calls several hooks)
  • 9. Virtual EnvWrapper Hooks• postmkvirtualenv• prermvirtualenv• postrmvirtualenv• postactivate• predeactivate• postdeactivate
  • 10. Introducing pip• Pip - Pip Installs Packages, nothing more, nothing less• It’s good, James Bennet explains - http://goo.gl/F9m2N• Great at re-producing builds of specific versions of packages, just remember to use versioning for each package.• Every project needs at least one requirements file
  • 11. Installing packages• By default, searches PyPi just like easy_install• Can also install from: - tarballs containing a setup.py script - Editable checkouts from VCS repos - Editable checkouts from VCS repos with revision info• Can install packages from Pip bundles
  • 12. Installing packages• Installation - sudo easy_install pip• Workflow 1. Make or activate virtual env 2. Install (and search for) packages• pip search pip install package-name (latest) pip install package-name==1.4.5 (specific) pip install package-name>=1.4.5 (at least...)• Install packages from requirements file pip install -r requirements.txt• Freeze current packages to produce requirements file
  • 13. Terminal action!• Finally, let’s write some code• For those watching at home, go checkout Mathemtism’s excellent screen cast at http://goo.gl/wWkti
  • 14. What about my editor?• Good Python editors and IDE’s should support virtual env• PyCharm does by you telling it which python interpreter should be used for each project, it then nows which site packages directory to use.
  • 15. Summary• Isolated Python environments are a must• VirtualEnv,VirtualEnvWrapper are THE way to manage them
  • 16. Docs• VirtualEnv http://www.virtualenv.org/en/latest/• VirtualEnvWrapper http://www.doughellmann.com/docs/virtualenvwrapper/• Pip http://www.pip-installer.org/en/latest/
  • 17. References• Pip and VirtualEnv (Awesome!) http://mathematism.com/2009/07/30/presentation-pip- and-virtualenv/• Tools of the Modern Python Hacker:Virtualenv, Fabric and Pip http://www.clemesha.org/blog/modern-python-hacker- tools-virtualenv-fabric-pip• Notes on using pip and virtualenv with Django http://www.saltycrane.com/blog/2009/05/notes-using-pip- and-virtualenv-django/
  • 18. Thanks!• Ryan Blunden ryan.blunden@gmail.com @ryan_blunden