测试模块的可用性 [英] Testing the availability of a module

查看:72
本文介绍了测试模块的可用性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的清单,


有没有比做更好的方式


试试:

import aModule

除外:

has_aModule = False

else:

has_aModule = True


这里主要关注的是加载aModule是不必要的(并且可能需要花费时间)。


非常感谢提前。

Bo

Dear list,

Is there a better way than doing

try:
import aModule
except:
has_aModule = False
else:
has_aModule = True

The main concern here is that loading aModule is unnecessary (and may
take time).

Many thanks in advance.
Bo

推荐答案

Bo Peng写道:
Bo Peng wrote:
亲爱的名单,

有吗比做更好的方法尝试:
导入aModule
除了:
has_aModule = False
否则:
has_aModule = True
这里主要关注的是加载模块是不必要的(并且可能需要时间)。
Dear list,

Is there a better way than doing

try:
import aModule
except:
has_aModule = False
else:
has_aModule = True

The main concern here is that loading aModule is unnecessary (and may
take time).




不,没有更好的方式。


你*可以*检查文件的存在,但是存在一个文件和它实际上可以成功加载的
作为一个模块是不一样的


此外,为了检查所有类型的文件,重新实现一半的Python导入

机器是很多工作。如果你认真对待它,你还需要处理可从ZIP文件加载的模块,

和.pth文件等。


这对编程来说并不好玩,你最好只是检查

就像你现在所做的那样成功导入。


- Gerhard



No, there is not really a better way.

You *could* check for the existance of files, but a file existing and it
really being successfully loadable as a module are not the same.

Besides, it is a lot of work to reimplement half the Python import
machinery just for checking for all kinds of files. If you''re serious
about it, you also need to cope with modules loadable from ZIP files,
and with .pth files, etc.

This is not fun to program, and you''re really better off just checking
for a successful import like you''re doing now.

-- Gerhard


Gerhard H?ring< gh@ghaering.de>写道:
Gerhard H?ring <gh@ghaering.de> writes:
Bo Peng写道:
Bo Peng wrote:
亲爱的清单,
有没有比做更好的方式尝试尝试:
导入aModule
除了:
has_aModule = False
否则:
has_aModule = True
这里主要关注的是加载aModule是不必要的(并且
可能需要时间)。
Dear list,
Is there a better way than doing
try:
import aModule
except:
has_aModule = False
else:
has_aModule = True
The main concern here is that loading aModule is unnecessary (and
may take time).



不,没有更好的方法。

你*可以*检查文件的存在,但存在一个文件和<它真的可以成功加载,因为模块不一样。

此外,重新实现一半的Python导入机器只是为了检查各种类型的工作需要做很多工作。文件。如果您对此非常认真,那么您还需要处理可从ZIP文件,
和.pth文件等加载的模块。

这对于程序,你最好只是检查
成功导入,就像你现在正在做的那样。



No, there is not really a better way.

You *could* check for the existance of files, but a file existing and
it really being successfully loadable as a module are not the same.

Besides, it is a lot of work to reimplement half the Python import
machinery just for checking for all kinds of files. If you''re serious
about it, you also need to cope with modules loadable from ZIP files,
and with .pth files, etc.

This is not fun to program, and you''re really better off just checking
for a successful import like you''re doing now.




我不觉得以上是完全正确的。 imp.find_module()可能

执行OP需要的东西。


Thomas



I don''t think the above is entirely correct. imp.find_module() probably
does what the OP needs.

Thomas


Gerhard H ?¤ring写道:
Gerhard H?¤ring wrote:
Bo Peng写道:
Bo Peng wrote:
亲爱的名单,

有没有比做更好的方式

尝试:
导入aModule
除了:
has_aModule = False
否则:
has_aModule = True

主要关心这是加载aModule是不必要的(并且可能需要时间)。
Dear list,

Is there a better way than doing

try:
import aModule
except:
has_aModule = False
else:
has_aModule = True

The main concern here is that loading aModule is unnecessary (and may
take time).



不,没有更好的方法。

你*可以*检查文件的存在,但是存在一个文件并且它真的可以成功加载作为一个模块是不一样的。

此外,重新实现一半是很多工作Python导入
机器只是为了检查各种文件。如果您对此非常认真,那么您还需要处理可从ZIP文件,
和.pth文件等加载的模块。



No, there is not really a better way.

You *could* check for the existance of files, but a file existing and it
really being successfully loadable as a module are not the same.

Besides, it is a lot of work to reimplement half the Python import
machinery just for checking for all kinds of files. If you''re serious
about it, you also need to cope with modules loadable from ZIP files,
and with .pth files, etc.




*除非*,否则其他人已经为您完成了所有操作:

http://peak.telecommunity.com/DevCenter/PythonEggs
http://peak.telecommunity.com/DevCenter/PkgResources

-

Robert Kern
ro*********@gmail.com

在地狱的地方,草地长得很高

梦想的坟墓是否能够死亡。

- Richard Harter



*Unless*, someone else has already done all of it for you:

http://peak.telecommunity.com/DevCenter/PythonEggs
http://peak.telecommunity.com/DevCenter/PkgResources

--
Robert Kern
ro*********@gmail.com

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter


这篇关于测试模块的可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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