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

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. You can install and uninstall packages for Python with the Pip package manager. Python modules that are stored on your home drive will only be accessible by you, but there may be modules that are globally installed on some systems. 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 RedHat Linux systems come with various Python versions installed by default. There may be additional versions available for installation, but by default, only the following versions are on the machine based on the RedHat Linux version.

  • RHEL8 systems by default have Python 2.7 and Python 3.6 installed.
  • RHEL9 systems by default have Python 3.9 installed.

Python and Pip can be run using the python[VERSION] and pip[VERSION] commands, where the version is inserted after “python” or “pip”. For instance, to run the default version of Python 3, the python3 command will have to be used. Additional commands may be available depending on the versions of python installed. For example, if Python 3.11 is also installed on a machine, it will not be the default version, and thus must be run with python3.11 and pip3.11 respectively. In this documentation, we will be using the pip3 and python3 commands.

The conda command is also installed on all fully managed Linux systems, which can be used for installing and managing different Python versions through conda environments. Please see the official Conda documentation for information on how to set-up a conda environment.

Installing, uninstalling, and upgrading Python packages using Pip

There are many packages available for installation for both Python 2 and Python 3. A list of available packages can be found at the Python Package Index for installation (and any additional usage or dependency details)

Install a Python package

  1. Open a terminal window.
  2. Use the command 'pip3 install --user <package-name>'  to install the requested package. Replace the pip3 command with a different version if you are trying to install the package on a version of Python that’s not the default.
    1. Installing with the --user flag installs the requested package under the user who is running the command. This allows you to download packages without having administrator privileges on the machine.
  3. It will then import the requested package and its dependencies, if it has any. If a package is already installed on the machine, then it will provide the output ‘Requirement already satisfied

Uninstall a Python package

Packages that were not installed locally to a user cannot be removed from the machine. When installing the software, be sure to utilize the --user flag so that all packages are installed on your user’s home directory.

  1. Open a terminal window.
  2. Use the command 'pip3 uninstall <package-name>'. Depending on how the package was installed, you may need to replace the pip3 command with the version of Pip that was used to originally install the package.
  3. The command will ask for confirmation after listing the files to be removed. Confirm this action by typing ‘y' then the Enter key. Uninstalling a package will not remove its dependencies, so additional commands may need to be run to undo an installation.

Upgrade a Python package

Python modules do not automatically update, so if you wish to update one to the latest available version, you can do so with the '--upgrade' flag. Note that you will be unable to upgrade packages installed globally on the system

  1. Open a terminal window.
  2. Use the command 'pip3 install --upgrade --user <package-name>'. Depending on how the package was installed, you may need to replace the pip3 command with the version of Pip that was used to originally install the package.
    1. Installing with the --user flag installs the requested package under the user who is running the command. This allows you to download packages without having administrator privileges on the machine.
  3. It will then upgrade the requested package and its dependencies, if it has any updates pending.

Location of installed Python packages through Pip

By default, Python packages installed through pip using the '--user' flag will be stored in your home directory depending on the specific version of Python they were installed with. Your home directory location, also equivalent to $HOME, will be noted by a ~.
~/.local/lib/python[VERSION]/site-packages

Alternatively, some packages may already be installed globally onto the system. All of the packages that are currently installed on the system are located at the following location, depending on the specific version of Python they were installed with.
/usr/lib/python[VERSION]/site-packages

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 creation 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, they can be achieved through the usage of the Virtualenvmodule.

Python 3.6 and 3.9 have Virtual Environments built-in, as outlined in the official Python documentation, and more information on how to configure Virtual Environments is available on the official documentation. The Virtualenv module is also available for Python 3.6 and Python 3.9.

46% helpful - 41 reviews
Print Article

Details

Article ID: 66715
Created
Tue 11/6/18 9:58 AM
Modified
Mon 4/22/24 4:18 PM