我想看看这些库是否已经在python环境中安装了下面的代码。
import os
libraries=['pandas','paramico','fnmatch'] #list of libraries to be installed
for i in libraries:
print("Checking the library", i ," installation")
try:
import i
print("module ", i ," is installed")
except ModuleNotFoundError:
print("module ", i, " is not installed")
数组值没有被转换为模块类型,并且所有内容都显示未安装,因此如何将文本值从数组转换为模块类型。
在上面的例子中,没有安装pandas和paramico,但是安装了fnmatch。
使用importlib.import_module
:
import os
import importlib
libraries=['pandas','paramico','fnmatch'] #list of libraries to be installed
for i in libraries:
print("Checking the library", i ," installation")
try:
importlib.import_module(i)
print("module ", i ," is installed")
except ModuleNotFoundError:
print("module ", i, " is not installed")
输出示例:
Checking the library pandas installation
module pandas is installed
Checking the library paramico installation
module paramico is not installed
Checking the library fnmatch installation
module fnmatch is installed