How to prevent pyinvoke “UnexpectedExit: Encountered a bad command exit code!”


When you try running your command using pyinvoke’s, the native command returns a non-zero exit code making your script fail instead of just continuing (e.g. to evaluate the output):

Traceback (most recent call last):
File "/home/user/", line 7, in <module>
result ="mycommand", out_stream=outStream)
# ...
File "/usr/local/lib/python3.10/dist-packages/invoke/", line 518, in _finish
raise UnexpectedExit(result)
invoke.exceptions.UnexpectedExit: Encountered a bad command exit code!


Use warn=True as an argument to

This will tell invoke to just warn you about the command exiting with non-zero exit status instead of throwing an UnexpectedExit