17

I'm looking for a way to call a python script from a batch file and getting a return code from the python script. Confusing I know, but it's based on a system that's currently in use. I'd re-write it, but this way would be much, much quicker.

So:

Bat ---------------------> Python
     * call python file *

Bat <--------------------------------- Python
      * python does a load of work *
      * and returns a return code  *

3 Answers 3

23

The windows shell saves the return code in the ERRORLEVEL variable:

python somescript.py
echo %ERRORLEVEL%

In the python script you can exit the script and set the return value by calling exit():

exit(15)

In older versions of python you might first have to import the exit() function from the sys module:

from sys import exit
exit(15)
Sign up to request clarification or add additional context in comments.

3 Comments

Errorlevel is only a pseudo-variable, it isn't actually anywhere in the environment.
This will NOT work if %ERRORLEVEL% variable already exists. Python will not overwrite it in that case (but does return the correct code - it will just be hidden by the variable!).
@fmuecke I don't see what you mean. I catch the return from different python calls using %ERRORLEVEL%. Different values on %ERRORLEVEL% from the different python scripts
3

Try:

import os
os._exit(ret_value)

You should also check:

2 Comments

And thanks to you, both of your answers were correct, I just needed the other side to it from the bat file as well. Thanks again.
I prefer the use of sys.exit(). for instance when writing a script to be run on a Visual Studio build process, and you want to display script output on the Build Output window. the os._exit() command does not allow you to see python's 'stdout'. Bottom line: sys.exit() can be more robust than os._exit() in some cases. whenever one doesn't behave as expected consider user the other method.
1

You can try this batch script for this:

@echo off

REM     %1 - This is the parameter we pass with the desired return code for the Python script that will be captured by the ErrorLevel env. variable.  
REM     A value of 0 is the default exit code, meaning it has all gone well. A value greater than 0 implies an error
REM     and this value can be captured and used for any error control logic and handling within the script
   
set ERRORLEVEL=
set RETURN_CODE=%1

echo (Before Python script run) ERRORLEVEL VALUE IS: [ %ERRORLEVEL% ]
echo.

call python -c "import sys; exit_code = %RETURN_CODE%; print('(Inside python script now) Setting up exit code to ' + str(exit_code)); sys.exit(exit_code)"

echo.
echo (After Python script run) ERRORLEVEL VALUE IS: [ %ERRORLEVEL% ]
echo.

And when you run it a couple of times with different return code values you can see the expected behaviour:

PS C:\Scripts\ScriptTests> & '\TestPythonReturnCodes.cmd' 5

(Before Python script run) ERRORLEVEL VALUE IS: [ 0 ]

(Inside python script now) Setting up exit code to 5

(After Python script run) ERRORLEVEL VALUE IS: [ 5 ]

PS C:\Scripts\ScriptTests> & '\TestPythonReturnCodes.cmd' 3

(Before Python script run) ERRORLEVEL VALUE IS: [ 0 ]

(Inside python script now) Setting up exit code to 3

(After Python script run) ERRORLEVEL VALUE IS: [ 3 ]

PS C:\Scripts\ScriptTests

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.