Skip to content

Instantly share code, notes, and snippets.

@terremoth
Last active November 15, 2023 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terremoth/bc2b80c92a480a04db8f571697e5e1b6 to your computer and use it in GitHub Desktop.
Save terremoth/bc2b80c92a480a04db8f571697e5e1b6 to your computer and use it in GitHub Desktop.
Prints to the screen all available and usable Python Modules installed
#!/usr/bin/env python
"""Prints to the screen all available and usable Python Modules (that aren't pip packages) installed
and display their description"""
import importlib
import os
import pkgutil
import platform
import re
__author__ = "Lucas M. Dutra <terremoth>"
__copyright__ = "Copyleft 2023"
__credits__ = ["Many StackOverflow answers"] # :D
__license__ = "GPLv3"
__version__ = "1.0.0"
__maintainer__ = "terremoth"
__email__ = "dutra.astro@gmail.com"
__status__ = "Production"
# This will prevent the pygame module from showing the "hello from community" in the terminal after import the
# pygame package (if it is present)
os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "hide"
# The antigravity python package is just a fun package, it does nothing but to open a web browser instance
# to xkcd.com/353, so we will ignore it to import to avoid this
IGNORE_MODULES_TO_IMPORT = ["antigravity"]
# These packages we will use, so we don't want to "unimport" them
IGNORE_MODULES_TO_DELETE = ["pkgutil", "importlib", "os", "platform", "re"]
# This is used to prevent the pkgutil to import .py files in the same directory that this script is
for file in os.listdir():
if file.endswith(".py"):
filename = os.path.splitext(file)[0]
IGNORE_MODULES_TO_IMPORT.append(filename)
# This part is added so if this script is executed on Windows, these packages can be found but cannot be imported
# neither used after we "import" one of these packages, they will throw an exception, # so we will avoid to import/show
# them on Windows environments
if platform.system() == "Windows":
ignore = ["crypt", "curses", "pty", "tty", "this", "pip"]
for item in ignore:
IGNORE_MODULES_TO_IMPORT.append(item)
# The code itself that will get all default available packages and show them
for package in pkgutil.iter_modules(path=None):
name = package.name
if name not in IGNORE_MODULES_TO_IMPORT and not name.startswith("_"):
imported = importlib.import_module(name)
doc = imported.__doc__
if doc is not None:
# doc = doc.split(".", 1)[0]
doc = doc.strip().replace("\n", " ")
doc = re.sub(r'-+', ' - ', doc)
doc = re.sub(r'=+', ' = ', doc)
doc = re.sub(r'\s+', ' ', doc)
doc = doc[:100]
else:
doc = "No description"
print("{}: {}".format(name, doc))
# TODO here: "unimport" the packages already imported so it will not consume/waste any memory.
# both ways are buggy, fails and throws exception:
# del sys.modules[name]
# try:
# eval("del {}".format(name))
# except:
# pass
@terremoth
Copy link
Author

terremoth commented Oct 13, 2023

You will get something like this:

image

You are free to use this script the way you want.
I truncate the "doc" string at 100 chars, but you can increase the max length you want

@terremoth
Copy link
Author

If you just want the list of the modules names without their respective description, you can just type in the terminal:

$ python -c "help('modules')"

and it will show something like this:

image

@terremoth
Copy link
Author

terremoth commented Oct 13, 2023

This script, I made it with the purpose: "I wanna know which packages comes by DEFAULT with python and WHAT they do, so I want a list of all of them" on terminal.

I will probably do one other version of this, but using TKinter (with GUI) where you can click on each module and see what each has of functions, submodules, constants etc. But this is MAYBE a future project. If one day I do, I will post here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment