我试图找到一种方法来识别缺少的库/库,同时导入尚未被python安装的库。
我正在制作一个应用程序,它使用用户需要自己安装或由另一个应用程序安装的一些库。例如,python请求库。我不希望我的应用程序安装缺少的依赖项,我也没有任何安装脚本来安装它们。我只想要一种方法,将丢失的库/库打印给用户。有没有办法这样做?如果您打印异常,它会说“没有模块名为模名”,我想为它创建一个自定义消息。例如,“缺少库:模块名”。有没有办法这样做?
到目前为止,在代码中导入的代码如下所示:
import sys, os, time, subprocess, pip
try:
import requests
except ImportError:
print "[!] It Seems Like You Are Missing Some Dependencies!"
ind = str(raw_input("[*] Install Missing Dependencies? [Y/N]:"))
ind = ind.upper()
if 'Y' in ind:
pip.main(['install', 'requests'])
else:
sys.exit()发布于 2018-01-02 01:01:52
将异常数据复制到变量中,并检查它返回的字符串:
import re # for re.match
try:
import requests
except ImportError as e:
errorstring = e.args[0]
print 'Missing library: "'+re.match(r"No module named (.+)", errorstring).group(1)+'"'有关8.3。处理异常的更全面描述,请参见args。
发布于 2018-01-02 00:57:51
使用“尝试”
try:
a = int(input())
except:
raise Exception('There has been an error in the system')https://stackoverflow.com/questions/48054418
复制相似问题