Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to install a Python Module?
A Python module is a file containing Python definitions and statements. Modules help us organize code into smaller, manageable pieces and enable code reuse across multiple programs. To use external modules in your Python projects, you need to install them using package managers like pip or conda.
Using PIP to Install Modules
PIP is the standard package manager for Python modules. It comes pre-installed with Python 3.4 and later versions. Use pip to install packages from the Python Package Index (PyPI).
Installing a Package
Open a command prompt or terminal and use the following command to install a module ?
C:\Users\Username> pip install requests
This installs the latest version of the requests module and its dependencies.
Collecting requests Downloading requests-2.31.0-py3-none-any.whl (62 kB) Installing collected packages: requests Successfully installed requests-2.31.0
Installing Specific Versions
To install a specific version of a package ?
pip install requests==2.28.0
To install within a version range ?
pip install 'requests>=2.25.0,<2.30.0'
Checking PIP Version
Verify your pip installation and version ?
pip --version
pip 22.2.2 from C:\Python310\lib\site-packages\pip (python 3.10)
Installing PIP (if missing)
If pip is not available on Python 3.4+, run this command ?
python -m ensurepip --upgrade
Using Conda Package Manager
Conda is an open-source package and environment management system that runs on Windows, macOS, and Linux. It can install packages from multiple channels and manage dependencies efficiently.
Searching for Packages
Check if a package is available before installing ?
conda search numpy
Loading channels: done # Name Version Build Channel numpy 1.21.5 py38h2bbff1b_0 pkgs/main numpy 1.21.5 py39h2bbff1b_0 pkgs/main numpy 1.24.0 py38h2bbff1b_0 pkgs/main numpy 1.24.0 py39h2bbff1b_0 pkgs/main
Installing Packages with Conda
Install a package using conda ?
conda install numpy
Collecting package metadata: done
Solving environment: done
## Package Plan ##
environment location: C:\Users\Username\anaconda3
added / updated specs:
- numpy
The following NEW packages will be INSTALLED:
numpy pkgs/main/win-64::numpy-1.24.0-py39h2bbff1b_0
Proceed ([y]/n)? y
Downloading and Extracting Packages
numpy-1.24.0 | 4.6 MB | ############ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Comparison
| Feature | PIP | Conda |
|---|---|---|
| Default with Python | Yes (3.4+) | No (Anaconda/Miniconda) |
| Package Source | PyPI | Conda channels + PyPI |
| Dependency Management | Basic | Advanced |
| Environment Management | No | Yes |
Verifying Installation
After installing a module, verify it works by importing it in Python ?
import requests
print("Requests version:", requests.__version__)
Requests version: 2.31.0
Conclusion
Use pip for standard Python package installation from PyPI. Use conda when you need advanced dependency management and virtual environments. Both tools make it easy to install and manage Python modules for your projects.
