Python: installing packages with pip | venv and virtualenv
 

Installing new package in python using pip

The pip module is part of python installation (Python 2.7.9 and later + Python 3.4 and later). However in some Python installation it may not be installed. In any case it can always be installed through the easy_install module. Simply run
 $ easy_install --user pip
or
 $ easy_install --user pip3
	
Alternatively, you may use Linux package manager, such as apt, and run:
 # apt install python-pip
			
or
 # apt install python3-pip
			
pip is a recursive acronym, it stands for "Pip Installs Packages". After the installation we may use pip by typing
 $ python -m pip 
		
this output pip's help.
We can search packages with pip, for example you we may look for jupyter package by running
 $ python -m pip search jupyter
		
This command search for jupyter package from PyPI · The Python Package Index repository. We may install jupyter package by running
 $ python -m pip --user install jupyter
		
Notice that this is a local installation. Due to security issues it is not recommended to use pip as super user. To find out where jupyter was installed we may run
 $ python -m pip --user show jupyter
		
To display all packages that were installed with pip, run:
 $ python -m pip list
		
To display all upgradable packages, run:
 $ python -m pip list --outdated
		
To upgrade package "foo", run:
 $ python -m pip install --user --upgrade foo	
		
To uninstall package "foo", run
 $ python -m pip --user uninstall foo
		
Not all pip packages are python modules. For example we can install ipython using pip by running:
 $ python -m pip --user install ipython
		 
To run ipython on Linux Debian we may write:
 $ $HOME/.local/bin/ipython
		

Virtual Environment: Using vitualenv and venv

A virtual environment helps in isolating global installed libraries from virtal environment installation. It can serve as a "sandbox" allowing us to to install external libraries or packages that differ in version from our globally installed libraries. This may be helpful when Python program needs an older library version to run or when we don't want to clutter our Python installation with to many libraries.

Python 3.3 (and above) added the venv package to the standard library, so we can use it to create virtual environment without installing third party package such as virtualenv. However in Python 2.7 we still need to instlal virtualenv package, to do this we simply run:

 $ python2.7 -m pip install --user virtualenv
		
and then we can create virtual environment by running
 $ python2.7 -m virtualenv VNEW
		
where VNEW is the name of our new virtual environmnet. In python 3.3 and above we don't need virtualenv package, we can simply run
 $ python -m venv VNEW
		
This create a folder name VNEW which holds self-contained Python installation. Now to use our virtual environment we need to source the activate file, we can do this by running
 $ source ./VNEW/bin/activate
		
This creates a new subshell adding all VNEW/bin directory to the PATH environment variables. As a result, the global installation of pip or python won't be used, instead the subshell will use the one that are located in the bin directory of our virtual environment (or VNEW/bin in our case). The prompt to our virtual enviroment may look like:
 	$ source VNEW/bin/activate
	(VNEW) $ 
We may exit to our previous terminal by running the deactivate command
 	$ source VNEW/bin/activate
	(VNEW) $ deactivate
	$ 
		
 
 
Disclaimer Privacy