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 can I get a list of locally installed Python modules?
Python is a flexible programming language with thousands of libraries and modules. As your Python environment grows with new installations, it becomes important to check which packages are currently available. Whether you're debugging, documenting your environment, or managing dependencies, listing locally installed Python modules is a valuable skill.
In this article, we will explore different ways to get a list of locally installed Python modules using built-in tools and command-line utilities.
Using pip list Command
The pip package installer provides a list subcommand that displays all installed packages in your current Python environment. This is the most common and reliable method for checking installed packages.
Basic Usage
You can run this command directly in your terminal or command prompt ?
pip list
The output shows package names and their versions ?
Package Version ---------- ------- numpy 1.24.3 pandas 2.0.2 pip 24.3.1 requests 2.31.0
From Python Script or Jupyter
In Jupyter notebooks or IPython, use the ! prefix to execute shell commands ?
!pip list
Additional Options
You can format the output differently or filter results ?
# Show only outdated packages !pip list --outdated # Show packages in JSON format !pip list --format=json # Show only user-installed packages !pip list --user
Using help("modules")
The help() function is Python's built-in interactive help system. When you pass "modules" as a parameter, it scans and displays all available modules, including both built-in and third-party packages.
Example
This command lists all available modules in your Python environment ?
help("modules")
The output includes built-in modules and installed packages ?
Please wait a moment while I gather a list of all available modules... __future__ difflib json sqlite3 _abc dis keyword ssl _ast doctest lib2to3 stat collections math numpy string datetime os pandas sys email random re time
Note: This method may take longer to execute as it loads and checks all modules.
Using pkgutil Module
The pkgutil module provides utilities for working with Python packages. You can use it programmatically to list installed modules ?
import pkgutil
# List all modules
installed_modules = [name for _, name, _ in pkgutil.iter_modules()]
print("Installed modules:")
for module in sorted(installed_modules)[:10]: # Show first 10
print(f"- {module}")
Installed modules: - calendar - collections - datetime - email - json - math - os - random - re - sys
Using subprocess with pip
You can programmatically get the pip list output within your Python script ?
import subprocess
import sys
# Get pip list output
result = subprocess.run([sys.executable, '-m', 'pip', 'list'],
capture_output=True, text=True)
print("Installed packages:")
print(result.stdout)
Installed packages: Package Version ---------- ------- pip 24.3.1 setuptools 68.0.0 wheel 0.40.0
Comparison of Methods
| Method | Speed | Output Format | Best For |
|---|---|---|---|
pip list |
Fast | Package + Version | Checking installed packages |
help("modules") |
Slow | Module names only | Comprehensive module listing |
pkgutil |
Medium | Programmatic access | Scripting and automation |
subprocess |
Fast | Customizable | Integration with scripts |
Conclusion
Use pip list for quick package checking with versions. Use help("modules") for comprehensive module discovery. Use pkgutil or subprocess for programmatic access within your Python scripts.
