简体   繁体   English

如何捕获python ImportError作为Shell错误

[英]How to catch a python ImportError as a Shell error

I have a following script which checks whether a python module is installed or not. 我有一个以下脚本,它检查是否安装了python模块。

{

ipython -c 'from package import module' && echo "Successful import"

} || echo "package not found!"

This is a typlical try..catch syntax of bash. 这是bash的典型try..catch语法。 If the command before && would work it will execute the command after that otherwise execute the command after ||. 如果&&之前的命令有效,它将执行命令,否则在||之后执行命令。

But in this case, whether package is installed or not, or should I say whether the command 但在这种情况下,无论是否安装package ,或者我应该说是否命令

ipython -c 'from package import module'

returns None or ImportError , bash reads it as successful execution and prints "Successful import" 返回NoneImportError ,bash将其读作成功执行并打印“成功导入”

How can I check for a successful import of a python package using bash? 如何使用bash检查成功导入python包?

EDIT : And return with exit status 1? 编辑:并返回退出状态1?

You can use the exception SystemExit(msg=None) with any message that's not None or 0 . 您可以将异常SystemExit(msg=None)与任何非None0消息一起使用。 If it's uncaught, it'll cause the program to end, and if the msg is not None or 0 , with an exit code of 1 . 如果它未被捕获,它将导致程序结束,并且如果msg不是None0 ,则退出代码为1 (see: https://docs.python.org/3/library/exceptions.html#SystemExit ). (参见: https//docs.python.org/3/library/exceptions.html#SystemExit )。

test.py test.py

try:
    import Blue
except ImportError:
    raise SystemExit("Import Error!")

in bash: 在bash中:

[user]@[host] $ python test.py 
Import Error!

[user]@[host] $ python test.py || echo "blarg"
Import Error!
blarg

[user]@[host] $ python test.py && echo "blarg"
Import Error!

Use the python command for running code snippets programmatically, instead of ipython . 使用python命令以编程方式运行代码片段,而不是ipython

There's an outstanding bug about this: https://github.com/ipython/ipython/issues/6912 有一个很好的错误: https//github.com/ipython/ipython/issues/6912

You have at least two possibilities: 你至少有两种可能性:

  • Using "pip" on your shell script to get the list of installed packages then grep for tour package: 在shell脚本上使用“pip”获取已安装软件包的列表,然后使用grep for tour package:

    pip list | 点子列表| grep yourpackage && echo installed || grep yourpackage && echo installed || echo "NOT Installed" 回声“未安装”

  • Using Python-bash script, I don't know exactly how to call it but I know how to use it: 使用Python-bash脚本,我不知道如何调用它,但我知道如何使用它:

Something like: 就像是:

output=$(
python - <<EOF
try:
    import your_package;
    print "Package Installed"
except Exception:
    print "Package not installed"
EOF
)

echo $output

here you have alot of alternatives to check for the existence of the package/module, so, intead of trying to import the package, you can check it's existence in a pythonic way. 在这里你有很多替代品来检查包/模块的存在,所以,尝试导入包,你可以用pythonic方式检查它的存在。 You can grab the different solutions for this answer or this and replace the import your_package; 你可以抓住不同的方案来解决这个答案还是和更换import your_package; with one of them. 和其中一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM