python 如何区分一个模块是标准库还是第三方库,如ConfigParser模块,求大神解答。

2025-03-25 00:50:54
推荐回答(3个)
回答1:

一、标准库全部列在官方文档里面了。可以去查找对照。


二、通过REPL来确定。例如

$ python
Python 2.7.9 (default, Mar  1 2015, 12:57:24) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> json.__file__
'/usr/lib/python2.7/json/__init__.pyc'
>>> import requests
>>> requests.__file__
'/home/sylecn/.local/lib/python2.7/site-packages/requests/__init__.pyc'

路径里面有 site-packages 就是第三方库。没有的就是标准库。

回答2:

$: pip freeze | grep xxx
输入上面的命令,xxx为需要查询的库。第三方库就有版本信息,系统库就没有。
比如我要查询 json库是否是第三方库:pip freeze | grep json 输出里面是没有的,因为json是系统库

回答3:

显然在python的交互式shell中import一下就知道了。