使用importlib.util检查库时出错 [英] Error when using importlib.util to check for library

查看:1163
本文介绍了使用importlib.util检查库时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用importlib库来验证是否在执行Python 3.5.2脚本的计算机上安装了nmap库

I'm trying to use the importlib library to verify whether the nmap library is installed on the computer executing the script in Python 3.5.2

我正在尝试使用importlib.util.find_spec("nmap"),但收到以下错误.

I'm trying to use importlib.util.find_spec("nmap") but receive the following error.

>>> import importlib
>>> importlib.util.find_spec("nmap")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'importlib' has no attribute 'util'

有人可以告诉我我要去哪里错吗?

Can someone tell me where I'm going wrong?

编辑

我可以使用以下代码使该功能正常工作.

I was able to get the function to work using the following code.

#!/usr/bin/pythonw

import importlib
from importlib import util

#check to see if nmap module is installed
find_nmap = util.find_spec("nmap")
if find_nmap is None:
    print("Error")

推荐答案

尝试一下:

from importlib import util
util.find_spec("nmap")

我打算进行调查,但老实说,我不知道为什么一个可行,而另一个却不可行.另外,请观察以下交互式会话:

I intend to investigate, but honestly I don't know why one works and the other doesn't. Also, observe the following interactive session:

>>> import importlib
>>> importlib.util
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'importlib' has no attribute 'util'
>>> from importlib import util
>>> util
<module 'importlib.util' from '/usr/lib/python3.5/importlib/util.py'>
>>> importlib.util
<module 'importlib.util' from '/usr/lib/python3.5/importlib/util.py'>

所以...是的.我确信这对某人来说是完全合理的,但对我而言却不是.一旦弄清楚,我将进行更新.

So...yeah. I am sure this makes perfect sense to someone, but not to me. I will update once I figure it out.

将此与以下内容进行比较:

Comparing this to something like:

>>> import datetime
>>> datetime
<module 'datetime' from '/usr/lib/python3.5/datetime.py'>
>>> datetime.datetime
<class 'datetime.datetime'>

我认为不同之处在于,在这种情况下,第一个datetime是一个模块,第二个是一个类,而在importlib.util情况下,两个都是模块.因此,除非已经加载了两个模块中的代码,否则module.module可能无法正常运行,而module.class则可以正常运行,因为导入模块时会加载类代码.

I think the difference is that in this case the first datetime is a module and the second is a class, while in the importlib.util case both are modules. So perhaps module.module is not OK unless the code from both modules has been loaded, while module.class is OK, because the class code is loaded when the module is imported.

不,在许多情况下module.module似乎都很好.例如:

Nope, it seems like in many cases module.module is fine. For example:

>>> import urllib
>>> urllib
<module 'urllib' from '/usr/lib/python3.5/urllib/__init__.py'>
>>> urllib.error
<module 'urllib.error' from '/usr/lib/python3.5/urllib/error.py'>

所以也许是importlib特有的.

正如 @kfb 在评论中指出的,它似乎确实与importlib有关.请参见 __init__.py中的importlib注释:

As @kfb pointed out in the comments, it does seem to be related to importlib specifically. See the following comment from the __init__.py for importlib:

# Until bootstrapping is complete, DO NOT import any modules that attempt
# to import importlib._bootstrap (directly or indirectly). Since this
# partially initialised package would be present in sys.modules, those
# modules would get an uninitialised copy of the source version, instead
# of a fully initialised version (either the frozen one or the one
# initialised below if the frozen one is not available).

importlib/util.py 确实导入importlib._bootstrap,所以我认为这是现实的.如果我的理解是正确的,那么在执行import importlib时,子模块将被初始化,但不会为已导入的importlib模块对象初始化.此时,如果您执行dir(importlib),则不会看到util.有趣的是,在之后,您尝试访问importlib.util并获得了AttributeErrorutil(以及其他子模块)被加载/初始化,现在您可以访问importlib.util

importlib/util.py does import importlib._bootstrap so I would assume that this is realted. If my understanding is correct, when you do import importlib the submodules will be initialized, but are not initialized for the importlib module object that you have imported. At this point, if you do dir(importlib) you will not see util. Interestingly, after you have tried to access importlib.util and gotten an AttributeError, util (along with the other submodules) gets loaded/initialized, and now you can access importlib.util!

>>> import importlib
>>> dir(importlib)
['_RELOADING', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__import__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_bootstrap', '_bootstrap_external', '_imp', '_r_long', '_w_long', 'find_loader', 'import_module', 'invalidate_caches', 'reload', 'sys', 'types', 'warnings']
>>> importlib.util
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'importlib' has no attribute 'util'
>>> importlib.util
<module 'importlib.util' from '/usr/lib/python3.5/importlib/util.py'>
>>> dir(importlib)
['_RELOADING', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__import__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_bootstrap', '_bootstrap_external', '_imp', '_r_long', '_w_long', 'abc', 'find_loader', 'import_module', 'invalidate_caches', 'machinery', 'reload', 'sys', 'types', 'util', 'warnings']

这篇关于使用importlib.util检查库时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆