如何找到从一个包中导入的Python模块

如何找到从一个包中导入的Python模块?

包含Python代码、语句、函数或类定义的文件称为模块。我们将构建一个名为“module”的模块,其文件名为module.py。

使用模块可以将复杂的程序分解为更小、更易于理解的部分。模块还可以实现代码重用。

在本文中,我们将讨论查找从包中导入的Python模块的各种方法。

阅读更多:Python 教程

使用列表推导式

列表推导式由表达式指定的括号组成,用于迭代Python列表中的每个元素。

Python列表推导式提供了一个更短的语法,用于从现有列表的元素中创建一个新列表。

示例

以下示例使用sys库和列表推导式,默认情况下返回所有导入的本地模块名称的未排序列表。

使用name(也称为双下划线)迭代sys.modules.values()检查一个项是否是局部作用域的模块。如果是,则保存带模块名称的‘output’。为了更易读,此代码排列‘output’变量,并将其保存回自身。这些‘output’的列表格式输出将发送到终端。

import sys
output = [module.__name__ for module in sys.modules.values() if module]
output = sorted(output)
print('The list of imported Python modules are :',output)

输出

以下是上述代码的输出。

The list of imported Python modules are : ['__main__', '_bootlocale', '_codecs', '_collections', '_functools', 
'_heapq', '_imp', '_locale', '_operator', '_signal', 
'_sitebuiltins', '_stat', 
'_sysconfigdata_m_linux_x86_64-linux-gnu', '_thread', 
'_warnings', '_weakref', '_weakrefset', 'abc', 
'builtins', 'codecs', 'collections', 'collections.abc', 'collections.abc', 'contextlib', 'encodings', 
'encodings.aliases', 'encodings.latin_1', 
'encodings.utf_8', 'errno', 'functools', 'genericpath', 
'heapq', 'importlib', 'importlib._bootstrap', 
'importlib._bootstrap', 'importlib._bootstrap_external', 
'importlib._bootstrap_external', 'importlib.abc', 
'importlib.machinery', 'importlib.util', 'io', 'io', 
'itertools', 'keyword', 'marshal', 'mpl_toolkits', 
'operator', 'os', 'posix', 'posixpath', 'posixpath', 
'reprlib', 'site', 'stat', 'sys', 'sysconfig', 'types', 
'warnings', 'weakref', 'zipimport']

使用pip freeze命令

此函数显示导入的全局模块的所有名称和版本列表,默认情况下按字母顺序排列。

示例

在IDE中打开终端窗口,输入以下命令。要执行,请按Enter键-

C:\Users\Lenovo>pip freeze

输出

终端将收到以下输出。

aspose-cells==22.7.0
click==8.1.3
cloudpickle==2.1.0
colorama==0.4.5
dask==2022.7.0
et-xmlfile==1.1.0
fsspec==2022.5.0
genno==1.11.0
ixmp==3.5.0
JPype1==1.4.0
llvmlite==0.38.1
locket==1.0.0
message-ix==3.5.0
modcall==0.1.0
mysql-connector-python==8.0.29
namespace==0.1.4
native==0.0.0.0a0.dev20210615
numba==0.55.2
numpy==1.22.4
openpyxl==3.0.10
packaging==21.3
pandas==1.4.3
partd==1.2.0
Pint==0.19.2
protobuf==4.21.2
psycopg2==2.9.3
pycparser==2.21
pyparsing==3.0.9
python-dateutil==2.8.2
python-dotenv==0.20.0
python-magic==0.4.27
pytz==2022.1
PyYAML==6.0
scipy==1.9.1
six==1.16.0
sparse==0.13.0
toolz==0.12.0
walk==0.3.5
workbook==1.1
xarray==2022.3.0
xlrd==2.0.1
xlutils==2.0.0
xlwt==1.3.0

使用dir()方法

dir()函数返回给定对象的属性和方法,但并不返回它们相关的值。该函数甚至会返回所有对象默认的内置属性。

示例

在以下示例中,使用dir()方法返回所有本地模块名称的排序列表−

module = dir()
print('导入的Python模块列表为:',module)

输出

下面的输出说明此脚本仅显示与本地作用域有关的名称−

导入的Python模块列表为:['__annotations__', '__builtins__', '__cached__', 
'__doc__', '__file__', '__loader__', '__name__', 
'__package__', '__spec__']

使用inspect.getmember()和Lambda

检查模块、类、方法、函数、追踪、帧对象和代码对象等活动对象的数据需要用到inspect模块提供的一些有用函数。它可以帮助您检查类的内容、检索方法的源代码、提取和格式化函数的参数列表,或者收集显示详细的追踪所需的所有数据,等等。

Lambda是匿名的短函数。虽然lambda函数只能有一个表达式,但它可以有任意数量的参数。

示例

下面的示例使用inspect.getmember()和Lambda以排序方式返回导入的本地模块。

该代码将导入的本地模块名称以及它们在系统上的位置作为可迭代对象返回。这通过for循环迭代并作为一行打印。

import inspect
import os
modules = inspect.getmembers(os)
results = filter(lambda m: inspect.ismodule(m[1]), modules)
for o in results:
   print('导入的Python模块列表为:',o)

输出

下面是上述代码的输出−

导入的Python模块列表为: ('abc', <module 'abc' from '/usr/lib64/python3.6/abc.py'>)
导入的Python模块列表为: ('errno', <module 'errno' (built-in)>)
导入的Python模块列表为: ('path', <module 'posixpath' from '/usr/lib64/python3.6/posixpath.py'>)
导入的Python模块列表为: ('st', <module 'stat' from '/usr/lib64/python3.6/stat.py'>)
导入的Python模块列表为: ('sys', <module 'sys' (built-in)>)

使用sys模块

sys.modules字典可用于发现应用程序正在使用的特定包中的所有Python模块。将模块名称链接到模块的字典称为sys.modules。要查看导入的模块,可以查看它的键。

示例

以下是使用sys模块查找导入的模块的示例。

from datetime import datetime
import sys
print (sys.modules.keys())

输出

以下是上述代码的输出 –

dict_keys(['builtins', 'sys', '_frozen_importlib', 
'_imp', '_warnings', '_thread', '_weakref', 
'_frozen_importlib_external', '_io', 'marshal', 'posix', 
'zipimport', 'encodings', 'codecs', '_codecs', 'encodings.aliases', 'encodings.utf_8', '_signal', 
'__main__', 'encodings.latin_1', 'io', 'abc', '_weakrefset', '_bootlocale', '_locale', 'site', 'os', 
'errno', 'stat', '_stat', 'posixpath', 'genericpath', 
'os.path', '_collections_abc', '_sitebuiltins', 
'sysconfig', '_sysconfigdata_m_linux_x86_64-linux-gnu', 'types', 'functools', '_functools', 'collections', 
'operator', '_operator', 'keyword', 'heapq', '_heapq', 'itertools', 'reprlib', '_collections', 'weakref', 
'collections.abc', 'importlib', 'importlib._bootstrap', 'importlib._bootstrap_external', 'warnings', 
'importlib.util', 'importlib.abc', 'importlib.machinery', 'contextlib', 'mpl_toolkits', 'datetime', 'time', 'math', 
'_datetime'])

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程