Installing, Uninstalling, or Upgrading Python Modules in Linux using Pip

Python modules are packages, or modules, which can be imported into a project to use. This includes packages like matplotlib, numpy, flask, and many more. To install and uninstall packages for Python you need to use Pip; PIP is a package manager for Python packages, or modules.

Python modules that are stored on your home drive, will only be accessible by you. Other students, faculty, and staff that require the same packages will have to individually run the following commands as well for shared Python projects.

On-domain RHEL7 Linux systems currently support Python 2.7, Python 3.4, and Python 3.6, these can be launched via their respective commands: “python, python3.4, and python3.6”.

Determining which version of Pip to use

Determine which version of Python you would like to install the package for, for each version of python there is a respective pip manager.

Python version table
Python Version Python Command Pip Command
Python 2.7 python pip
Python 3.4 python3.4 pip3.4
Python 3.6 python3.6 pip3.6

In all of the following examples below replace the variable ‘$PIP’ with the respective pip command.

Note: The command python3 and pip3 currently map to python3.6 and pip3.6, respectively.

Installing Python packages using Pip

In this example, flask will be installed and uninstalled. Flask is a package used for front-end Python web-development.

  1. Open a terminal.
  2. To install a package use the command '$PIP install --user <package-name>' where $PIP is the Pip Version command in the above table.
    1. This example will install the flask package.
      1. In this example using Python 2.7: 'pip install --user flask'
      2. For Python 3.6 this would be 'pip3.6 install --user flask'
    2. This will import the flask package and its dependencies if it has any.Installing with Pip

Uninstalling/Removing Python packages using Pip

  1. Open a terminal window.
  2. To uninstall, or remove, a package use the command '$PIP uninstall <package-name>'.
    1. This example will remove the flask package.
      1. In this example using Python 2.7: 'pip uninstall - flask'
      2. For Python 3.6 this would be 'pip3.6 uninstall --user flask'
    2. NOTE: You will be unable to remove packages installed directly on the system
  3. The command will ask for confirmation after listing the files to be removed. Confirm this action by typing ‘y' then Enter key.Uninstall using Pip

Upgrading a Python package using Pip

If you wish to update a Python module you can do so with the '--upgrade' flag

  1. Open a terminal.
  2. To upgrade a package use the command '$PIP install --upgrade --user <package-name>'
    1. This example will upgrade the numpy package:
      1. In this example using Python 2.7: 'pip install --upgrade --user  numpy'
      2. For Python 3.6 this would be 'pip3.6 install --upgrade --user numpy'
    2. NOTE: You will be unable to upgrade packages installed locally on the systemUpgrading using Pip

Location of installed Python packages through Pip

By default Python packages installed through pip using the '--user' flag will be stored in one of the following locations in your home directory, depending on which version of python you installed the package for:

Note: ~ is your home directory location, also equivalent to $HOME .

~/.local/lib/python2.7/site-packages

~/.local/lib/python3.4/site-packages

~/.local/lib/python3.6/site-packages

Pip Location User

By default, Python packages installed locally on each system will be stored in one of the following directories, depending on which version of python packages you are looking for:

/usr/lib/python2.7/site-packages

/usr/lib/python3.4/site-packages

/usr/lib/python3.6/site-packages

Pip Location System

Cleanly managing python modules

When working on many different python programs you may run into issues regarding conflicts between modules that you have installed.  One option python offers to manage this is the usage of Virtual Environments which contain everything needed to run the program inside of your virtual environment directory.

Python 2.7 does not incorporate virtual environments by default, however, there is a module to handle this called Virtualenv.

Python 3.4 and 3.6 have this feature built-in, as seen from here from the official Python documentation.  The Virtualenv module is also available for Python 3.4 and Python 3.6.

Was this helpful?
0% helpful - 1 review

Details

Article ID: 66715
Created
Tue 11/6/18 9:58 AM
Modified
Mon 11/11/19 3:14 PM