_stdlib.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. """List of Python standard library modules.
  2. Sadly, there is no reliable way to tell whether a module is part of the
  3. standard library except by comparing to a canonical list.
  4. This is taken from https://github.com/PyCQA/isort/tree/develop/isort/stdlibs,
  5. which itself is sourced from the Python documentation.
  6. """
  7. import sys
  8. def is_stdlib_module(module: str) -> bool:
  9. base_module = module.partition(".")[0]
  10. return base_module in _get_stdlib_modules()
  11. def _get_stdlib_modules():
  12. if sys.version_info.major == 3:
  13. if sys.version_info.minor == 8:
  14. return stdlib3_8
  15. if sys.version_info.minor == 9:
  16. return stdlib3_9
  17. if sys.version_info.minor >= 10:
  18. return sys.stdlib_module_names # type: ignore[attr-defined]
  19. elif sys.version_info.major > 3:
  20. return sys.stdlib_module_names # type: ignore[attr-defined]
  21. raise RuntimeError(f"Unsupported Python version: {sys.version_info}")
  22. stdlib3_8 = {
  23. "_dummy_thread",
  24. "_thread",
  25. "abc",
  26. "aifc",
  27. "argparse",
  28. "array",
  29. "ast",
  30. "asynchat",
  31. "asyncio",
  32. "asyncore",
  33. "atexit",
  34. "audioop",
  35. "base64",
  36. "bdb",
  37. "binascii",
  38. "binhex",
  39. "bisect",
  40. "builtins",
  41. "bz2",
  42. "cProfile",
  43. "calendar",
  44. "cgi",
  45. "cgitb",
  46. "chunk",
  47. "cmath",
  48. "cmd",
  49. "code",
  50. "codecs",
  51. "codeop",
  52. "collections",
  53. "colorsys",
  54. "compileall",
  55. "concurrent",
  56. "configparser",
  57. "contextlib",
  58. "contextvars",
  59. "copy",
  60. "copyreg",
  61. "crypt",
  62. "csv",
  63. "ctypes",
  64. "curses",
  65. "dataclasses",
  66. "datetime",
  67. "dbm",
  68. "decimal",
  69. "difflib",
  70. "dis",
  71. "distutils",
  72. "doctest",
  73. "dummy_threading",
  74. "email",
  75. "encodings",
  76. "ensurepip",
  77. "enum",
  78. "errno",
  79. "faulthandler",
  80. "fcntl",
  81. "filecmp",
  82. "fileinput",
  83. "fnmatch",
  84. "formatter",
  85. "fractions",
  86. "ftplib",
  87. "functools",
  88. "gc",
  89. "getopt",
  90. "getpass",
  91. "gettext",
  92. "glob",
  93. "grp",
  94. "gzip",
  95. "hashlib",
  96. "heapq",
  97. "hmac",
  98. "html",
  99. "http",
  100. "imaplib",
  101. "imghdr",
  102. "imp",
  103. "importlib",
  104. "inspect",
  105. "io",
  106. "ipaddress",
  107. "itertools",
  108. "json",
  109. "keyword",
  110. "lib2to3",
  111. "linecache",
  112. "locale",
  113. "logging",
  114. "lzma",
  115. "mailbox",
  116. "mailcap",
  117. "marshal",
  118. "math",
  119. "mimetypes",
  120. "mmap",
  121. "modulefinder",
  122. "msilib",
  123. "msvcrt",
  124. "multiprocessing",
  125. "netrc",
  126. "nis",
  127. "nntplib",
  128. "ntpath",
  129. "numbers",
  130. "operator",
  131. "optparse",
  132. "os",
  133. "ossaudiodev",
  134. "parser",
  135. "pathlib",
  136. "pdb",
  137. "pickle",
  138. "pickletools",
  139. "pipes",
  140. "pkgutil",
  141. "platform",
  142. "plistlib",
  143. "poplib",
  144. "posix",
  145. "posixpath",
  146. "pprint",
  147. "profile",
  148. "pstats",
  149. "pty",
  150. "pwd",
  151. "py_compile",
  152. "pyclbr",
  153. "pydoc",
  154. "queue",
  155. "quopri",
  156. "random",
  157. "re",
  158. "readline",
  159. "reprlib",
  160. "resource",
  161. "rlcompleter",
  162. "runpy",
  163. "sched",
  164. "secrets",
  165. "select",
  166. "selectors",
  167. "shelve",
  168. "shlex",
  169. "shutil",
  170. "signal",
  171. "site",
  172. "smtpd",
  173. "smtplib",
  174. "sndhdr",
  175. "socket",
  176. "socketserver",
  177. "spwd",
  178. "sqlite3",
  179. "sre",
  180. "sre_compile",
  181. "sre_constants",
  182. "sre_parse",
  183. "ssl",
  184. "stat",
  185. "statistics",
  186. "string",
  187. "stringprep",
  188. "struct",
  189. "subprocess",
  190. "sunau",
  191. "symbol",
  192. "symtable",
  193. "sys",
  194. "sysconfig",
  195. "syslog",
  196. "tabnanny",
  197. "tarfile",
  198. "telnetlib",
  199. "tempfile",
  200. "termios",
  201. "test",
  202. "textwrap",
  203. "threading",
  204. "time",
  205. "timeit",
  206. "tkinter",
  207. "token",
  208. "tokenize",
  209. "trace",
  210. "traceback",
  211. "tracemalloc",
  212. "tty",
  213. "turtle",
  214. "turtledemo",
  215. "types",
  216. "typing",
  217. "unicodedata",
  218. "unittest",
  219. "urllib",
  220. "uu",
  221. "uuid",
  222. "venv",
  223. "warnings",
  224. "wave",
  225. "weakref",
  226. "webbrowser",
  227. "winreg",
  228. "winsound",
  229. "wsgiref",
  230. "xdrlib",
  231. "xml",
  232. "xmlrpc",
  233. "zipapp",
  234. "zipfile",
  235. "zipimport",
  236. "zlib",
  237. }
  238. stdlib3_9 = {
  239. "_thread",
  240. "abc",
  241. "aifc",
  242. "argparse",
  243. "array",
  244. "ast",
  245. "asynchat",
  246. "asyncio",
  247. "asyncore",
  248. "atexit",
  249. "audioop",
  250. "base64",
  251. "bdb",
  252. "binascii",
  253. "binhex",
  254. "bisect",
  255. "builtins",
  256. "bz2",
  257. "cProfile",
  258. "calendar",
  259. "cgi",
  260. "cgitb",
  261. "chunk",
  262. "cmath",
  263. "cmd",
  264. "code",
  265. "codecs",
  266. "codeop",
  267. "collections",
  268. "colorsys",
  269. "compileall",
  270. "concurrent",
  271. "configparser",
  272. "contextlib",
  273. "contextvars",
  274. "copy",
  275. "copyreg",
  276. "crypt",
  277. "csv",
  278. "ctypes",
  279. "curses",
  280. "dataclasses",
  281. "datetime",
  282. "dbm",
  283. "decimal",
  284. "difflib",
  285. "dis",
  286. "distutils",
  287. "doctest",
  288. "email",
  289. "encodings",
  290. "ensurepip",
  291. "enum",
  292. "errno",
  293. "faulthandler",
  294. "fcntl",
  295. "filecmp",
  296. "fileinput",
  297. "fnmatch",
  298. "formatter",
  299. "fractions",
  300. "ftplib",
  301. "functools",
  302. "gc",
  303. "getopt",
  304. "getpass",
  305. "gettext",
  306. "glob",
  307. "graphlib",
  308. "grp",
  309. "gzip",
  310. "hashlib",
  311. "heapq",
  312. "hmac",
  313. "html",
  314. "http",
  315. "imaplib",
  316. "imghdr",
  317. "imp",
  318. "importlib",
  319. "inspect",
  320. "io",
  321. "ipaddress",
  322. "itertools",
  323. "json",
  324. "keyword",
  325. "lib2to3",
  326. "linecache",
  327. "locale",
  328. "logging",
  329. "lzma",
  330. "mailbox",
  331. "mailcap",
  332. "marshal",
  333. "math",
  334. "mimetypes",
  335. "mmap",
  336. "modulefinder",
  337. "msilib",
  338. "msvcrt",
  339. "multiprocessing",
  340. "netrc",
  341. "nis",
  342. "nntplib",
  343. "ntpath",
  344. "numbers",
  345. "operator",
  346. "optparse",
  347. "os",
  348. "ossaudiodev",
  349. "parser",
  350. "pathlib",
  351. "pdb",
  352. "pickle",
  353. "pickletools",
  354. "pipes",
  355. "pkgutil",
  356. "platform",
  357. "plistlib",
  358. "poplib",
  359. "posix",
  360. "posixpath",
  361. "pprint",
  362. "profile",
  363. "pstats",
  364. "pty",
  365. "pwd",
  366. "py_compile",
  367. "pyclbr",
  368. "pydoc",
  369. "queue",
  370. "quopri",
  371. "random",
  372. "re",
  373. "readline",
  374. "reprlib",
  375. "resource",
  376. "rlcompleter",
  377. "runpy",
  378. "sched",
  379. "secrets",
  380. "select",
  381. "selectors",
  382. "shelve",
  383. "shlex",
  384. "shutil",
  385. "signal",
  386. "site",
  387. "smtpd",
  388. "smtplib",
  389. "sndhdr",
  390. "socket",
  391. "socketserver",
  392. "spwd",
  393. "sqlite3",
  394. "sre",
  395. "sre_compile",
  396. "sre_constants",
  397. "sre_parse",
  398. "ssl",
  399. "stat",
  400. "statistics",
  401. "string",
  402. "stringprep",
  403. "struct",
  404. "subprocess",
  405. "sunau",
  406. "symbol",
  407. "symtable",
  408. "sys",
  409. "sysconfig",
  410. "syslog",
  411. "tabnanny",
  412. "tarfile",
  413. "telnetlib",
  414. "tempfile",
  415. "termios",
  416. "test",
  417. "textwrap",
  418. "threading",
  419. "time",
  420. "timeit",
  421. "tkinter",
  422. "token",
  423. "tokenize",
  424. "trace",
  425. "traceback",
  426. "tracemalloc",
  427. "tty",
  428. "turtle",
  429. "turtledemo",
  430. "types",
  431. "typing",
  432. "unicodedata",
  433. "unittest",
  434. "urllib",
  435. "uu",
  436. "uuid",
  437. "venv",
  438. "warnings",
  439. "wave",
  440. "weakref",
  441. "webbrowser",
  442. "winreg",
  443. "winsound",
  444. "wsgiref",
  445. "xdrlib",
  446. "xml",
  447. "xmlrpc",
  448. "zipapp",
  449. "zipfile",
  450. "zipimport",
  451. "zlib",
  452. "zoneinfo",
  453. }