in Linux pip Python virtual virtualenv ~ read.

Set Up Package Manager (PIP) And Virtual Environments in Python on Ubuntu 14.04

Introduction

Python has many libraries support and they are not install default when you install python. Packages in Python can be tools, libraries, frameworks and applications. Given the popularity and the beauty of the language, there are tens of thousands of packages available which you can make use of for your own projects. So package management tools will help us manager and use them. One of the most common Python package manager is PIP.

So, if you have many projects and each of projects is used by different packages?? they will be conflicted. And to resolve it, we will use virtualenv package. A virtual environment created using this tool includes a fresh copy of the Python binary itself as well as a copy of the entire Python standard library. And now, we will use a virtual environment for each projects.

Setup and using pip

In ubuntu, you can use ATP to install pip. You will typing in terminal:

sudo apt-get install python-pip # Python2  
#or 
sudo apt-get install python3-pip #(python > 3.0)  

Now, you have pip on your system.

To install package in pip, you can typing:

sudo pip install <package-name>  

To uninstall package in pip, you can typing:

sudo pip uninstall <package-name>  

Setup and using Virtualenv

1) Setup

We will use pip to install virtualenv package. You open your terminal and typing:

sudo pip install virtualenv  

2) Using virtualenv

Creating a virtual environment. You open your terminal and typing:

virtualenv virt_name  

Creating a virtual environment with a custom Python interpreter. You open your terminal and typing:

virtualenv --python=/opt/python-3.4/bin/python virt_name  
# or 
virtualenv -p /usr/bin/python3 virt2  

To use a created virtual environment, you need to activate it.

source virt_name/bin/active  

Now, anything you do will only apply in virtual environment. You can install a new package or run script python.

(virt_name)[email protected]:~/Desktop/myproject$ python demo.py

You can use the which command to check the Python binary that is used in the current virtual environment.

which python  

If everything is working well, you should be get something similar:

/home/ngoccuong/Desktop/myproject/virt_name/bin/python

Deactivating a virtual environment:

deactivate  

And to delete a virtual environment, you can use the rm -rf command:

rm -rf virt_name  
comments powered by Disqus