A Python module is a file that contains reusable code, such as functions, classes, and variables, which can be imported into Python programs. Before using a module that is not included with Python, it must be installed in your Python environment.
Prerequisites
Before installing a module, ensure that Python and pip are installed on your system.
To install PIP refer to these articles: Windows, Linux and MacOS.
To install Python, refer to this article: Download and Install Python.
If pip is not installed or configured on your system, install it first before proceeding. Some modules that are not available through pip can also be installed manually.
Using pip
pip is the default package manager for Python and a common way to install Python modules. It downloads the required package and its dependencies from the Python Package Index (PyPI).
Step 1: Open Command Prompt or Terminal
Open the terminal based on your operating system:
- Windows: Open Command Prompt, PowerShell, or Windows Terminal.
- Linux: Open the Terminal application.
- macOS: Open the Terminal application.
Step 2: Install Python Module
Run the following command by replacing <module_name> with the name of the module you want to install:
pip install <module name>
Replace <module name> with the name of the module you want to install. For example, to install the popular numpy module, you would use:
pip install numpy
Once you run the command, pip will download the module from the Python Package Index (PyPI) and install it on your computer.
Note: If you are using Python 3.x, you may need to use the command pip3 instead of pip.
Step 3: Verify the Installation
After the installation is complete, verify it by importing the module in a Python file or the Python interpreter.
import numpy
print(numpy.__version__)
It prints the installed version of the module. If no error occurs, the module has been installed correctly and is ready to use.
Upgrade pip (Optional)
If you are using an older version of pip, it is recommended to upgrade it before installing Python modules.
Step 1: Check pip Version
Run the following command:
pip --version
This command displays the currently installed version of pip. If you have an older version, you can upgrade it before installing modules.
Step 2: Upgrade pip
Run the following command to update pip to the latest version:
pip install --upgrade pip
This command downloads and installs the latest version of pip, ensuring compatibility with the latest Python packages.
Module Management Commands
After installing a Python module, you can also upgrade, uninstall, or install modules from external sources using pip.
1. Upgrade an Installed Module: To update an installed module to its latest available version, run:
pip install --upgrade <module_name>
This command downloads and installs the latest version of the specified module while replacing the older version.
2. Uninstall a Module: To remove an installed module from your Python environment, run:
pip uninstall <module_name>
This command removes the specified module from your system. You may be prompted to confirm the uninstallation.
3. Install a Module from a Git Repository: Some Python modules are hosted on Git repositories instead of PyPI. You can install them directly using:
pip install git+https://github.com/username/repository.git
This command downloads the project directly from the Git repository and installs it in your current Python environment. It is commonly used to install development versions or packages that are not available on PyPI.
Manual Installation (If Required)
Most Python modules can be installed directly using pip. However, some modules may be available only as source code or in a local project folder. In such cases, you can install the module manually.
Step 1: Download or Clone the Project
Download the module as a ZIP file or clone it from its source repository, then extract or navigate to the project directory.
Step 2: Install the Module
Open the Command Prompt or Terminal in the project directory and run:
pip install .
This command installs the module from the current directory along with its dependencies, following the modern Python packaging standard.