什么是“安全进口"?在Python中意味着什么? [英] What does "import safe" mean in Python?

查看:76
本文介绍了什么是“安全进口"?在Python中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是碰到鼻子不会运行标记为可执行的测试的行为(如

I just hit the behaviour where nose will not run tests marked as executable (as described in a previous question). I found this surprising, and I wasted some time trying to find out why nose wasn't running my tests before I learned about nose's behaviour here.

在鼻子测试手册中,它描述了覆盖默认行为的选项:

In the manpage for nosetests, it describes an option to override the default behaviour:

--exe               Look for tests in python modules that are executable.
                    Normal behavior is to exclude executable modules,
                    since they may not be import-safe [NOSE_INCLUDE_EXE]

我的问题是:进口安全"是什么意思?非导入安全模块的示例是什么?可以通过删除可执行位来使非导入安全模块成为导入安全模块吗?

My question is: what does "import-safe" mean? What is an example of a non-import-safe module? And can a non-import-safe module be made import-safe by removing the executable bit, or is there more to it than that?

推荐答案

我不熟悉鼻子,但是我很确定导入安全"的含义是导入模块只会定义东西,而不是跑去跑东西.

I'm not familiar with nose, but I'm pretty sure what it means by "import safe" is that importing the module will just define things, not go off and run stuff.

该想法是,如果将.py文件设计为作为脚本执行,则将在执行模块作用域代码时启动其功能.可以防止使用__name__ == '__main__'技巧进行导入,但事实并非如此.如果不是这样,导入它可能会执行与不带参数调用时的脚本相同的操作,在某些情况下可能会很糟糕.

The idea would be that if a .py file is designed to be executed as a script, then its functionality will be initiated while executing module-scope code. This could be guarded against importing with the __name__ == '__main__' trick, but it might not be. If it isn't, importing it will probably do the same thing it would do as a script when invoked with no arguments, which in some cases could be bad.

因此,您可以通过--exe标志明确告诉鼻子,没有这样的可执行脚本可能会导致危险导入,或者您可以清除脚本中的可执行权限.

So, you can explicitly tell nose that there are no such executable scripts that would be dangerous to import by passing the --exe flag, or you can clear the executable permission from your scripts.

这篇关于什么是“安全进口"?在Python中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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