Toothpick Anemone
Toothpick Anemone

Reputation: 4636

Are any standard library modules missing from `print( help('modules'))`

In a very brief python script, we can write:

print( help('modules'))

This lists a great number of things, including the following:

PIL                 _weakrefset         html                secrets
__future__          _winapi             http                select
_abc                _xxsubinterpreters  idlelib             selectors
_ast                abc                 imaplib             setuptools

Note that some non-standard libraries are included. For example, I have PIL installed for image processing. My question is, are any of the standard libraries missing from print( help('modules'))? Basically, I want to print a list of the names of all python standard libraries to file. My attempt is shown below:

import os
import pathlib as pl
import string
from contextlib import redirect_stdout


class FileFilter:

    def __init__(self, filepath):
        filepath = pl.Path(str(filepath))
        if os.path.exists(filepath):
            os.remove(filepath)
        # open in `append` mode
        self._file = open(filepath, "a")
        self.prev_was_comma = False

    def write(self, stuff):
        lamby = lambda ch:\
            "," if ch in string.whitespace else ch
        mahp = map(lamby, str(stuff))
        for ch in mahp:
            if ch != "," or not self.prev_was_comma:
                self._file.write(ch)
            if ch == ",":
                self.prev_was_comma = True
            else:
                self.prev_was_comma = False

    def __del__(self):
        self._file.close()


with redirect_stdout(FileFilter("stdlibs.txt")):
    help('modules')

# TODO:
# remove opening line
#     ,Please,wait,a,moment,while,I,gather,a,list,of,all,available,modules...,
# remove closing line
#     ,Enter,any,module,name,to,get,more,help.,Or,type,"modules,spam"
#     to,search,for,modules,whose,name,or,summary,contain,the,string,"spam"., 

Upvotes: 0

Views: 185

Answers (1)

CCCC_David
CCCC_David

Reputation: 581

In Python 3.10 you can use sys.stdlib_module_names.

You can also take a look at sys.builtin_module_names if that is what you need.

Upvotes: 1

Related Questions