As stated, mostly the error codes come from the executed script and sys.exit().

The example with a non-existing file as an argument to the interpreter fall in a different category. Though it's stated nowhere I would guess, that these exit codes are the "standard" Linux error codes. There is a module called errno that provides these error numbers (the exit codes come from linux/include/errno.h.

I.e.: errno.ENOENT (stands for for "No such file or directory") has the number 2 which coincides with your example.

Answer from Martin Thurau on Stack Overflow
🌐
Dataiku Community
community.dataiku.com › questions & discussions › using dataiku
The Python process failed (exit code: 2) — Dataiku Community
November 7, 2023 - This is a free edition DSS but with 2-week entreprise trail. Does anyone know why this happened and how to fix it? ... Those logs just say that the underlying Python process for the Python recipe died. You will see more details (a Python traceback, hopefully) if you scroll higher in the logs. That should give you further clues about why this happened. ... [INFO] [dku.utils] - C:\Users\AppData\Local\Dataiku\DataScienceStudio\Python\python-3.7.13\python.exe: can't open file 'C:\Users\AppData\Local\Dataiku\DataScienceStudio\dss_home\jobs\QS_DATA_PREP\Build_job_postings_python__NP__2023-11-07T02-46-10.093\recipe_from_notebook_admins_Python_notebook_on_job_postings_prepared_NP\python-recipe\pyoutxEPom1HW0yhL\python-exec-wrapper.py': [Errno 2] No such file or directory
Discussions

Executing Python Script fail with error code 2
TS: 2014 Python: Anaconda3 On my development pc all my python scripts are executing fine. After deploying it all python scripts fails with error code 2. what does "2" mean ? I have tested my scripts are working, if I execute them directly outside Teststand. I have made a simple debug python sc... More on forums.ni.com
🌐 forums.ni.com
1
0
February 20, 2017
Run Command - Python error
Hi, I am trying to run a Python script and using the instructions in this thread: But I get an error: Error: External program failed with exit code 2; path=C:\Python\python.exe; args=C:\Users...\script.py; workdir= Source: action "Run program", table "Python Script" Thank you. More on community.easymorph.com
🌐 community.easymorph.com
1
0
April 5, 2022
Python project, exit code 2
This issue has been moved from a ticket on Developer Community. [severity:It's more difficult to complete my work] Hello, im 2 days trying to resolve issue with python project and all seems to ... More on github.com
🌐 github.com
2
June 8, 2022
Getting "Process exit code: 2" when running installed Python script in Inno Setup - Stack Overflow
How to run Python setup.py libraries in Inno Setup? I mean which section is the correct place and how to do? In general, in the terminal the following syntax works python setup.py install But, the More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnpython › error code . returned non-zero exit status 2. help
r/learnpython on Reddit: error code . returned non-zero exit status 2. help
March 2, 2021 -

Hi there!

So short Intro, I am making a python script/project for like a addon to the game "Elite Dangrous", To do this I am using basically a macro to accomplish the task, for those who know this game it's basically a Fleet carrier auto Route plotter for long disctance trips.

The way the script works is it uses 2 librarys called "pyautogui" and "pydirectinput" to navigate the player UI and peform actions.

I decided to make this since i learned how to read csv files in my classes a few weeks ago.

the code runs fine, but the next step is to impliment the script into another program called EDMC(https://github.com/EDCD/EDMarketConnector) , that can run plugins that are fully written in python(https://github.com/EDCD/EDMarketConnector/blob/main/PLUGINS.md) I was asked to make the script a plugin so that more people can use it.

I have spent a few days trying to make it run, but I have now encountered an error that I have no idea how to fix ot troubleshoot.

subprocess.CalledProcessError: Command '['C:\\Program Files (x86)\\EDMarketConnector\\EDMarketConnector.exe', '-m', 'pip', 'install', 'pydirectinput']' returned non-zero exit status 2.

2021-03-02 14:15:05.670 - ERROR - plug.load_plugins:210: Failure loading found Plugin "FCjumper"

EDMC returns a text file with error codes when it runs and this is the extract, If i should post more of it please let me know and ill give the whole thing.

Here is the link to my Git hub which has the files, It is organized with 2 modules and a load.py file.The load file has the functions required for EDMC to recognise it.

https://github.com/Gorfs/FCAutojumper

i created a new branch REDDIT for yall to see the new files plus the full log for the error, in main there are my previous files, for the files without the EDMC mods, look for the GUI.py, Macro.py and main.py, files in the main branch and run those or alternativly run the FCautojumperandfueller.py to get all the functions in one file.

IF those interested want to try out the code without the plugins bits just call the make_GUI in the load file(make sure that you have all 3 files) and have fun

The second link should have everything required to understand how it works, so If you can understand it, it should give some explanations.

here is the link to a short plugin for EDMC that I am using to understand how to make my own work ,

https://github.com/Exynom/EDMC-HourlyIncome

And also second question would be how would i make it so that It could be its own standalone .exe? whenever I launch the file directly without an IDE it does nothing(when calling the make_GUI ) but when i run it using the IDE it all works? (just a little side question).

Final notes:

I know this is probably a dumb question but after a few hours of looking and only finding things that are WAY to complicated for me to understand i got a bit demotivated. And yes i know this isnt at all good compared to the other plugins, I just wanted to make a little thing to help the comunity out.

Anyway I hope that I didnt just turn people off from helping me with this massive hump of text, (if so, sorry) and of course if anyone wants to use the code to make anything they want you have my full consent, you may edit, copy, distribute as you wish but please add me as a contributor is so. :)

Good day (or night)

P.S if you have any problems seeing the files please comment and ill try to fix it as soon as possible thanks :)

btw if you post a response you are a boss :)

edit1: forgot to say, If anyone here can't figure out what my crappy code means then post in the comments, I'll try to answer question about what I made. :)

🌐
National Instruments
forums.ni.com › community › discussion forums › most active software boards › ni teststand › executing python script fail with error code 2
Solved: Executing Python Script fail with error code 2 - NI Community
February 20, 2017 - Solved: TS: 2014 Python: Anaconda3 On my development pc all my python scripts are executing fine. After deploying it all python scripts fails with
🌐
Notes
henryleach.com › 2025 › 02 › controlling-python-exit-codes-and-shell-scripts
Controlling Python Exit Codes and Shell Scripts - Henry Leach
February 9, 2025 - Also note that some programs, like grep(1) exit 0 only when something has been found, 1 when nothing has been found (but this doesn't mean an error) and >1 when there has been an error2. If you choose to try and make the error codes to your program mean something (and document this!) then you can use it in more complex scripts like: #!/bin/sh eval python exit-examples.py return_code=$? if [ $return_code = 0 ]; then echo "Success!" elif [ $return_code = 1 ]; then echo "Mild panic!" elif [ $return_code = 42 ]; then echo "Other fallback" else echo "Real Failure" exit $return_code fi
🌐
pytest
docs.pytest.org › en › stable › reference › exit-codes.html
Exit codes - pytest documentation
Python version support · Sponsor ... · Exit code 1: Tests were collected and run but some of the tests failed · Exit code 2: Test execution was interrupted by the user ·...
Find elsewhere
🌐
Super Fast Python
superfastpython.com › home › tutorials › process exit codes in python
Process Exit Codes in Python - Super Fast Python
September 11, 2022 - You can set an exit code for a process via sys.exit() and retrieve the exit code via the exitcode attribute on the multiprocessing.Process class. In this tutorial you will discover how to get and set exit codes for processes in Python. Let’s get started. Need Process Exit Codes A process is a running instance of […]
🌐
GitHub
github.com › microsoft › PTVS › issues › 7037
Python project, exit code 2 · Issue #7037 · microsoft/PTVS
June 8, 2022 - This issue has been moved from a ticket on Developer Community. [severity:It's more difficult to complete my work] Hello, im 2 days trying to resolve issue with python project and all seems to ...
Author   vsfeedback
🌐
Cisco
orbital.amp.cisco.com › help › Content › Topics › Script-Exit-Codes.htm
Script Exit Codes - Orbital
Script exit codes inform Orbital whether a Python script completed successfully.
🌐
Linux Hint
linuxhint.com › python-exit-codes
Python Exit Codes – Linux Hint
Python has only two standard codes, i.e., a one and a zero. The exit code of 0 means that the process has been executed and exited successfully. This means no error was encountered. On the other hand, an error code of 1 indicates that the process exited with a failure.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360009990099-Non-zero-exit-code-2
Non-zero exit code (2) – IDEs Support (IntelliJ Platform) | JetBrains
December 1, 2020 - /Users/rodelaporte/Documents/code/python/djangoProject/venv/bin/python -m pip install [options] -r <requirements file> [package-index-options] ... /Users/rodelaporte/Documents/code/python/djangoProject/venv/bin/python -m pip install [options] [-e] <vcs project url> ... /Users/rodelaporte/Documents/code/python/djangoProject/venv/bin/python -m pip install [options] [-e] <local project path> ... /Users/rodelaporte/Documents/code/python/djangoProject/venv/bin/python -m pip install [options] <archive url/path> ... ... I met this problem last night. And it shows that it was caused by the update of pip. I solve it by run "pip install pip==20.2.2", as some say "--build-dir" does not work well in pip 20.3.
Top answer
1 of 1
6

First of all it would be nice to know what OS you are using. Basically the stderr and stdout are file descriptors (1 and 2 by default.)

In your case the subcommand writes nothing to 1 and 2 file descriptors (so to stderr and stdout) and of course the return code is 2.

If you run the following code:

import subprocess

result = subprocess.run(["echo", "test"])
print(result.returncode, result.stdout, result.stderr)

You get:

>>> python3 test.py
test
0 None None

It means the echo runs correctly and writes the test to the STDOUT but the result.stdout doesn't contain it. This happens but you don't redirect the STDOUT of the command to a PIPE.

If you change the code to:

import subprocess

result = subprocess.run(["echo", "test"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(result.returncode, result.stdout, result.stderr)

You will get:

>>> python3 test.py
0 b'test\n' b''

So now the result.stdout contains the STDOT of the called command in bytes. It is True for STDERR.

If a set the shell parameter to True, I can run invalid (not existed) command as you can see below:

import subprocess

result = subprocess.run(["invalid_cmd"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
print(result.returncode, result.stdout, result.stderr)

In this case the output is:

>>> python3 test.py
127 b'' b'/bin/sh: invalid_cmd: command not found\n'

It means the return code is 127 and the STDOUT is empty but the STDERR contains /bin/sh: invalid_cmd: command not found\n message in bytes.

If you do the above things but you don't see any output then your called command doesn't write the STDOUT or STDERR so you should look into the called command.

🌐
YouTube
youtube.com › python and pandas with reuven lerner
Python and exit codes - YouTube
What is an "exit code," and why do you sometimes see that a Python program had "exit code 0"? In this video, I explain what they are, show you how to grab t...
Published   April 1, 2021
Views   39K
🌐
Alteryx Community
community.alteryx.com › t5 › Alteryx-Designer-Desktop-Discussions › Run-Command-Tool-Code-2-Error › td-p › 340689
Solved: Run Command Tool - Code 2 Error - Alteryx Community
December 16, 2025 - Also - I have tested the script and am able to run it successfully via the Windows CMD tool. ... Solved! Go to Solution. ... Solved! Go to Solution. ... An error code of 2 is usually (not always) file not found.
🌐
Medium
medium.com › @anupkumarray › working-with-exit-codes-between-python-shell-scripts-177931204291
Working with exit codes between Python & Shell Scripts | by Anup Kumar Ray | Medium
October 17, 2021 - Note: If you pass anything other than an integer to sys.exit() call, the passed value will be printed and system exit status will be 1 . Refer the below example: ... Since exception is properly handled in python code, it will exit with a success return code 0 when Exceptions.sh is executed.
🌐
Nagios
support.nagios.com › board index › community support forums for nagios open source projects › open source nagios projects
Exit code - Python script - Nagios Support Forum
May 21, 2014 - Alarm_Virginia = Cloudwatch_Connection_Virginia.describe_alarms(None, None, None, None, "ALARM", None) Alarm_Europe = Cloudwatch_Connection_Europe.describe_alarms(None, None, None, None, "ALARM", None) # Check if there are Errors If NO -> print OK and exit with OK status code. if len (Alarm_Virginia) == 0 and len (Alarm_Europe) == 0: print "OK - No Alarms in Virginia & Europe Regions , Everything is Fine !!" sys.exit(0) # Initialize error variable : error = "" # Start the FOR Loop to get all alarms in EU & Virginia Regions : for alarm_v in Alarm_Virginia: error = error + alarm_v.name + " ," fo
🌐
Piccolo-orm
piccolo-orm.com › blog › understanding-sys-exit
Understanding sys.exit - Piccolo Blog
April 21, 2021 - You can then use these exit codes in things like if statements. >>> if python successful_script.py; then echo "successful"; else echo "error"; fi; successful >>> if python failing_script.py; then echo "successful"; else echo "error"; fi; error