Skip to content

Instantly share code, notes, and snippets.

@mateor
Last active October 27, 2020 13:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mateor/89a9cef41532021549739a12eae7790a to your computer and use it in GitHub Desktop.
Save mateor/89a9cef41532021549739a12eae7790a to your computer and use it in GitHub Desktop.
Function to programmatically generate the list of importable modules that shipped with the invoking Python distribution.
# The MIT License (MIT)
# Copyright (c) 2016 mateor
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions
# of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
>>> from setuptools import distutils
>>> import glob, pkgutil, os, sys
>>> print(sys.version)
2.7.10 (default, Jul 13 2015, 12:05:58)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]
>>> print(sys.hexversion)
34015984
>>> def get_system_modules():
... modules = {module for _, module, package in list(pkgutil.iter_modules()) if package is False}
... top_level_txt = glob.iglob(os.path.join(os.path.dirname(os.__file__) + '/site-packages', '*-info', 'top_level.txt'))
... modules -= {open(txt).read().strip() for txt in list(top_level_txt)}
... system_modules = set(sys.builtin_module_names)
... _, top_level_libs, _ = list(os.walk(distutils.sysconfig.get_python_lib(standard_lib=True)))[0]
... return sorted(top_level_libs + list(modules | system_modules))
...
>>> python_stdlib = get_system_modules()
>>> len(python_stdlib)
403
>>> for imprt in python_stdlib:
... print(imprt)
...
Audio_mac
BaseHTTPServer
Bastion
CGIHTTPServer
Canvas
ColorPicker
ConfigParser
Cookie
Dialog
DocXMLRPCServer
EasyDialogs
FileDialog
FixTk
FrameWork
HTMLParser
IN
MacOS
MimeWriter
MiniAEFrame
Nav
OSATerminology
PixMapWrapper
Queue
ScrolledText
SimpleDialog
SimpleHTTPServer
SimpleXMLRPCServer
SocketServer
StringIO
Tix
Tkconstants
Tkdnd
Tkinter
UserDict
UserList
UserString
_AE
_AH
_App
_CF
_CG
_CarbonEvt
_Cm
_Ctl
_Dlg
_Drag
_Evt
_File
_Fm
_Folder
_Help
_IBCarbon
_Icn
_LWPCookieJar
_Launch
_List
_Menu
_Mlte
_MozillaCookieJar
_OSA
_Qd
_Qdoffs
_Qt
_Res
_Scrap
_Snd
_TE
_Win
__builtin__
__future__
__main__
_abcoll
_ast
_bisect
_codecs
_codecs_cn
_codecs_hk
_codecs_iso2022
_codecs_jp
_codecs_kr
_codecs_tw
_collections
_csv
_ctypes
_ctypes_test
_curses
_curses_panel
_elementtree
_functools
_hashlib
_heapq
_hotshot
_io
_json
_locale
_lsprof
_multibytecodec
_multiprocessing
_osx_support
_pyio
_random
_scproxy
_socket
_sqlite3
_sre
_ssl
_strptime
_struct
_symtable
_sysconfigdata
_testcapi
_threading_local
_tkinter
_warnings
_weakref
_weakrefset
abc
aepack
aetools
aetypes
aifc
antigravity
anydbm
applesingle
appletrawmain
appletrunner
argparse
argvemulator
array
ast
asynchat
asyncore
atexit
audiodev
audioop
autoGIL
base64
bdb
bgenlocations
binascii
binhex
bisect
bsddb
buildtools
bundlebuilder
bz2
cPickle
cProfile
cStringIO
calendar
cfmfile
cgi
cgitb
chunk
cmath
cmd
code
codecs
codeop
collections
colorsys
commands
compileall
compiler
config
contextlib
cookielib
copy
copy_reg
cov_core
cov_core_init
crypt
csv
ctypes
curses
datetime
dbhash
dbm
decimal
difflib
dircache
dis
distutils
doctest
dumbdbm
dummy_thread
dummy_threading
easy_install
email
encodings
ensurepip
errno
exceptions
fcntl
filecmp
fileinput
findertools
fnmatch
formatter
fpformat
fractions
ftplib
functools
future_builtins
gc
gdbm
genericpath
gensuitemodule
gestalt
getopt
getpass
gettext
glob
grp
gzip
hashlib
heapq
hmac
hotshot
htmlentitydefs
htmllib
httplib
ic
icglue
icopen
idlelib
ihooks
imaplib
imghdr
imp
importlib
imputil
inspect
io
itertools
json
keyword
lib-dynload
lib-tk
lib2to3
linecache
locale
logging
macerrors
macostools
macpath
macresource
macurl2path
mailbox
mailcap
markupbase
marshal
math
md5
mhlib
mimetools
mimetypes
mimify
mmap
modulefinder
multifile
multiprocessing
mutex
netrc
new
nis
nntplib
ntpath
nturl2path
numbers
opcode
operator
optparse
os
os2emxpath
parser
pdb
pickle
pickletools
pimp
pipes
pkg_resources
pkgutil
plat-darwin
plat-mac
platform
plistlib
popen2
poplib
posix
posixfile
posixpath
pprint
profile
pstats
pty
pwd
py_compile
pyclbr
pydoc
pydoc_data
pyexpat
pytest
quopri
random
re
readline
repr
resource
rexec
rfc822
rlcompleter
robotparser
runpy
sched
select
sets
sgmllib
sha
shelve
shlex
shutil
signal
site
site-packages
sitecustomize
smtpd
smtplib
sndhdr
socket
sqlite3
sre
sre_compile
sre_constants
sre_parse
ssl
stat
statvfs
string
stringold
stringprep
strop
struct
subprocess
sunau
sunaudio
symbol
symtable
sys
sysconfig
syslog
tabnanny
tarfile
telnetlib
tempfile
terminalcommand
termios
test
textwrap
this
thread
threading
time
timeit
tkColorChooser
tkCommonDialog
tkFileDialog
tkFont
tkMessageBox
tkSimpleDialog
toaiff
token
tokenize
trace
traceback
ttk
tty
turtle
types
unicodedata
unittest
urllib
urllib2
urlparse
user
uu
uuid
videoreader
virtualenv
warnings
wave
weakref
webbrowser
whichdb
wsgiref
xdrlib
xml
xmllib
xmlrpclib
xxsubtype
zipfile
zipimport
zlib
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment