Installing, uninstalling, or upgrading Python modules using Pip (Linux - RHEL7)

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.

Fully managed 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 for which you would like to install the package. For each version of Python, there is a respective pip manager. In all of the following examples,  replace the variable ‘$PIP’ with the respective pip command. Note that the command python3 and pip3 map to python3.6 and pip3.6, respectively.

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

Installing, uninstalling, and upgrading Python packages using Pip

In the following examples, Flask will be installed and uninstalled. Flask is a package used for front-end Python web-development.

Install

  1. Open a terminal window.
  2. Use the command '$PIP install --user <package-name>' where $PIP is the Pip Version command referenced in the Python version table.
    1. Using Python 2.7: 'pip install --user flask'
    2. Using Python 3.6: 'pip3.6 install --user flask'
  3. It will then import the flask package and its dependencies, if it has any. Example snippets:
    Collecting flask, Downloading https://, Installing collected packages, Successfully installed

Uninstall

Note that you will be unable to remove packages installed directly on the system

  1. Open a terminal window.
  2. Use the command '$PIP uninstall <package-name>'.
    1. Using Python 2.7: 'pip uninstall - flask'
    2. Using Python 3.6: 'pip3.6 uninstall --user flask'
  3. The command will ask for confirmation after listing the files to be removed. Confirm this action by typing ‘y' then the Enter key. Example snippets:
    Uninstalling Flask, /home/campus##/username/.local/bin/flask, Proceed? Y/N, Successfully uninstalled Flask

Upgrade

If you wish to update a Python module you can do so with the '--upgrade' flag. The example will upgrade the numpy package. Note that you will be unable to upgrade packages installed locally on the system

  1. Open a terminal window.
  2. Use the command '$PIP install --upgrade --user <package-name>'
    1. Using Python 3.6:  'pip3.6 install --upgrade --user numpy'
    2. Using Python 2.7: 'pip install --upgrade --user  numpy'\
    3. It will then import the flask package and its dependencies, if it has any. Example snippets:
      Collecting numpy, Downloading https://, Installing collected packages, Successfully installed

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. Your home directory location, also equivalent to $HOME, will be noted by a ~.

  • ​​​​​~/.local/lib/python2.7/site-packages
  • ~/.local/lib/python3.4/site-packages
  • ~/.local/lib/python3.6/site-packages

Using the command cd ~/.local/lib/python2.7/site-packages , then ls,  you may receive a listing similar to:

  • click
  • Click-7.0.dist-info
  • itsdangerous
  • -1.1.0.dist-infoitsdangerous
  • jinja2
  • jinja2-2.10.dist-info
  • numpy
  • numpy-1.15.3.dist-in​​​​​fo

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 search:

  • /usr/lib/python2.7/site-packages
  • /usr/lib/python3.4/site-packages
  • /usr/lib/python3.6/site-packages

Using the command cd /usr/lib/python2.7/site-packages , then ls,  you may receive a listing similar to:

  • ANSI.py
  • ANSI.pyc
  • augeas.py
  • augeas.pyc
  • augeas.pyo
  • babel
  • Babel-0.9.9-py2.7.egg-info

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 outlined in the official Python documentation.  The Virtualenv module is also available for Python 3.4 and Python 3.6.

46% helpful - 41 reviews

Details

Article ID: 66715
Created
Tue 11/6/18 9:58 AM
Modified
Sun 10/15/23 3:28 PM