Using Virtualenv

When using python for various projects, you might need Python 2.7 and other packages in one project and Python 3 and other packages in other project. How do you handle such situations?

Here’s where virtualenv comes in picture for you.

What is virtualenv:

virtualenv is a tool to create isolated Python environments.

Installing virtualenv:

Assuming you have already installed Python

  • Install virtualenv using pip
$ [sudo] pip install virtualenv

you can also install in other ways such as installing from source etc.,

Using virtualenv:

Assuming you have installed virtualenv

  • First start with creating a directory for your project

$ mkdir project

  • Create a virtual environment in the project
  • If you want to use global packages

$ virtualenv --system-site-packages project/

  • If you want to use isolated packages

$ virtualenv --no-site-packages project/

  • Now, your virtualenv is created successfully
  • Change to the project directory

$ cd project

  • you can find a bin directory if you ls in the directory
  • Now, you need to activate your virtualenv

$ source bin/activate

  • Now, you have successfully activated your virtualenv

(project) $ 

  • This is how your terminal turns when you activate your virtualenv.

 

After this all the installations that you do inside project will be isolated with other projects.

Happy Python!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s