[Python] How to use Python virtualenv

What is Virtualenv?

A Virtual Environment, put simply, is an isolated working copy of Python which
allows you to work on a specific project without worry of affecting other projects

It enables multiple side-by-side installations of Python, one for each project.

It doesn’t actually install separate copies of Python, but it does provide a
clever way to keep different project environments isolated.

 

Verify if Virtualenv is installed

$ virtualenv --version

 

Install Virtualenv

$ sudo apt-get install python-virtualenv
or
$ sudo easy_install virtualenv
or
$ sudo pip install virtualenv

 

Setup and Use Virtualenv

First create a directory for your new shiny isolated environment
$ mkdir ~/virtualenvironment
To create a folder for your project that includes a clean copy of Python,
simply run:
$ virtualenv ~/virtualenvironment/myproject
or
$ virtualenv --system-site-packages ~/virtualenvironment/myproject

You can also use the Python interpreter of your choice (like python2.7). 
$ virtualenv -p /usr/bin/python2.7 myproject

 

To begin working with your project, you have to cd into your directory (project)
and activate the virtual environment.
$ cd ~/virtualenvironment/myproject/bin
$ source activate

 

Install a package in your Virtualenv

If you look at the bin directory in your virtualenv, you’ll see easy_install which
has been modified to put eggs and packages in the virtualenv’s site-packages 
directory.

To install an app in your Virtualenv:
$ sudo pip install --upgrade tensorflow

 

To exit your virtualenv just type “deactivate”.

댓글 남기기