How Do You Troubleshoot the Python Error “subprocess.CalledProcessError: Command ‘…’ returned non-zero exit status 1”?

Problem scenario
You are automating a Linux infrastructure task with Python using subprocess calls. You get this error "subprocess.CalledProcessError: Command '…' returned non-zero exit status 1", what should you do?

Run the Linux command without Python. Then run echo $? to determine the exit code. If you see a 1, that means Python notices this command is not considered to have run successfully. The solution should be to break down the processing into atomic Python commands. Try to use Python as much as possible for whatever you were doing. Make sure the Linux command returns an exit code of 0 every time the command is run.

Use the env > /path/to/analyze.txt command. When Python runs, the env variables will be different from the commands you interactively run. Therefore the contents of the env command should help you.

Leave a comment

Your email address will not be published. Required fields are marked *