• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / How to use Virtualenv in Python

How to use Virtualenv in Python

Author: PFB Staff Writer
Last Updated: May 22, 2020

This post will describe what Virtualenv is and how you can use it.

What is Virtualenv?


Virtualenv is a tool to create isolated Python environments, it's perhaps the 
easiest way to configure a custom Python environment. 

Virtualenv allows you to add and modify Python modules without access to the
global installation.

What does it do?


The basic problem being addressed is one of dependencies and versions, and 
indirectly permissions. 

Imagine you have an application that needs version 1 of LibFoo, but another
application requires version 2. 

How can you use both these applications? 

If you install everything into /usr/lib/python2.7/site-packages (or whatever your 
platform’s standard location is), it’s easy to end up in a situation where you 
unintentionally upgrade an application that shouldn’t be upgraded.

Or more generally, what if you want to install an application and leave it be? 

If an application works, any change in its libraries or the versions of those
libraries can break the application.

Also, what if you can’t install packages into the global site-packages directory? 

For instance, on a shared host.

In all these cases, virtualenv can help you. 

It creates an environment that has its own installation directories, that doesn’t
share libraries with other virtualenv environments

How to install Virtualenv?


There are a few ways to install virtualenv on your machine. 

You can use either the source tarball, pip or by using easy_install.
Easy_Install

$ sudo easy_install virtualenv

Searching for virtualenv
Reading http://pypi.python.org/simple/virtualenv/
Reading http://www.virtualenv.org
Reading http://virtualenv.openplans.org
Best match: virtualenv 1.8.2
Downloading http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.8.2.tar.gz... 
processing virtualenv-1.8.2.tar.gz
.....
....
Processing dependencies for virtualenv
Finished processing dependencies for virtualenv
Source ball installation

Get the latest version from here: http://pypi.python.org/packages/source/v/virtualenv/
wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.8.tar.gz
tar xzvf virtualenv-1.8.tar.gz
python virtualenv-1.8/virtualenv.py $HOME/env
Pip installation

pip install virtualenv

Usage


To create virtual environments, you can use the virtualenv command. 

Create an environment called "foobar":
virtualenv foobar

Activate the environment by sourcing its activate script, which is located in the 
environment's bin/ directory:
source foobar/bin/activate

This will change your $PATH so its first entry is the virtualenv’s bin/ directory.

If you install a package in your virtual environment, you'll see that executable 
scripts are placed in foobar/bin/ and eggs in foobar/lib/python2.X/site-packages/
easy_install yolk

Yolk is a small command line tool which can, among other things, list the
currently installed Python packages on your system:
yolk -l


Virtualenv inherits packages from the system's default site-packages directory. 

This is especially useful when relying on certain packages being available,
so you don't have to go through installing them in every environment.

To leave an environment, simply run deactivate:
deactivate

If you execute he yolk command now, you will see that it won't work because the
package was installed only in your virtual environment. 

Once you reactivate your environment it will be available again.
Sources

I used different sources to find information for this article: 
the official virtualenv website, from Chris Scott "A Primer on virtualenv" and
from Arthur Koziel 'Working with virtualenv'

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us