[{"data":1,"prerenderedAt":1751},["ShallowReactive",2],{"doc-\u002Fhow-to\u002Fhow-to-exit-a-program-in-python":3},{"id":4,"title":5,"body":6,"description":1744,"extension":1745,"meta":1746,"navigation":53,"path":1747,"seo":1748,"stem":1749,"__hash__":1750},"content\u002Fhow-to\u002Fhow-to-exit-a-program-in-python.md","How to Exit a Program in Python",{"type":7,"value":8,"toc":1695},"minimark",[9,13,17,20,25,111,122,126,152,158,163,169,291,296,315,318,335,341,344,432,435,439,442,445,567,571,588,591,605,609,612,627,630,652,655,688,691,697,707,710,826,830,845,851,856,867,874,881,887,892,966,969,988,993,998,1001,1083,1097,1103,1110,1113,1138,1152,1155,1178,1184,1194,1198,1203,1206,1220,1223,1226,1229,1243,1247,1250,1257,1262,1268,1274,1277,1282,1285,1331,1334,1371,1377,1380,1395,1398,1422,1429,1432,1487,1494,1501,1510,1567,1574,1592,1596,1600,1605,1612,1617,1622,1627,1634,1637,1644,1647,1651,1691],[10,11,5],"h1",{"id":12},"how-to-exit-a-program-in-python",[14,15,16],"p",{},"Sometimes you want a Python program to stop on purpose.",[14,18,19],{},"This page shows the main ways to do that, when to use each one, and what mistakes to avoid. You will also see the difference between stopping a loop, leaving a function, and ending the whole script.",[21,22,24],"h2",{"id":23},"quick-answer","Quick answer",[26,27,32],"pre",{"className":28,"code":29,"language":30,"meta":31,"style":31},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import sys\n\nprint(\"Starting program\")\nsys.exit()\nprint(\"This line will not run\")\n","python","",[33,34,35,48,55,79,95],"code",{"__ignoreMap":31},[36,37,40,44],"span",{"class":38,"line":39},"line",1,[36,41,43],{"class":42},"sVHd0","import",[36,45,47],{"class":46},"su5hD"," sys\n",[36,49,51],{"class":38,"line":50},2,[36,52,54],{"emptyLinePlaceholder":53},true,"\n",[36,56,58,62,66,70,74,76],{"class":38,"line":57},3,[36,59,61],{"class":60},"sptTA","print",[36,63,65],{"class":64},"sP7_E","(",[36,67,69],{"class":68},"sjJ54","\"",[36,71,73],{"class":72},"s_sjI","Starting program",[36,75,69],{"class":68},[36,77,78],{"class":64},")\n",[36,80,82,85,88,92],{"class":38,"line":81},4,[36,83,84],{"class":46},"sys",[36,86,87],{"class":64},".",[36,89,91],{"class":90},"slqww","exit",[36,93,94],{"class":64},"()\n",[36,96,98,100,102,104,107,109],{"class":38,"line":97},5,[36,99,61],{"class":60},[36,101,65],{"class":64},[36,103,69],{"class":68},[36,105,106],{"class":72},"This line will not run",[36,108,69],{"class":68},[36,110,78],{"class":64},[14,112,113,114,117,118,121],{},"Use ",[33,115,116],{},"sys.exit()"," when you want to stop the program early. It raises ",[33,119,120],{},"SystemExit"," and ends the script unless it is caught.",[21,123,125],{"id":124},"what-this-page-helps-you-do","What this page helps you do",[127,128,129,133,136,139],"ul",{},[130,131,132],"li",{},"Stop a Python script on purpose",[130,134,135],{},"Exit early when a condition is met",[130,137,138],{},"Understand the difference between stopping a loop and stopping the whole program",[130,140,141,142,144,145,148,149],{},"Choose between ",[33,143,116],{},", ",[33,146,147],{},"raise SystemExit",", and ",[33,150,151],{},"return",[21,153,113,155,157],{"id":154},"use-sysexit-to-end-the-program",[33,156,116],{}," to end the program",[14,159,160,161,87],{},"The most common way to end a Python script is ",[33,162,116],{},[14,164,165,166,168],{},"You need to import the ",[33,167,84],{}," module first:",[26,170,172],{"className":28,"code":171,"language":30,"meta":31,"style":31},"import sys\n\nprint(\"Program started\")\n\nuser_age = -1\n\nif user_age \u003C 0:\n    print(\"Age cannot be negative\")\n    sys.exit()\n\nprint(\"This line will not run\")\n",[33,173,174,180,184,199,203,219,224,242,259,271,276],{"__ignoreMap":31},[36,175,176,178],{"class":38,"line":39},[36,177,43],{"class":42},[36,179,47],{"class":46},[36,181,182],{"class":38,"line":50},[36,183,54],{"emptyLinePlaceholder":53},[36,185,186,188,190,192,195,197],{"class":38,"line":57},[36,187,61],{"class":60},[36,189,65],{"class":64},[36,191,69],{"class":68},[36,193,194],{"class":72},"Program started",[36,196,69],{"class":68},[36,198,78],{"class":64},[36,200,201],{"class":38,"line":81},[36,202,54],{"emptyLinePlaceholder":53},[36,204,205,208,212,215],{"class":38,"line":97},[36,206,207],{"class":46},"user_age ",[36,209,211],{"class":210},"smGrS","=",[36,213,214],{"class":210}," -",[36,216,218],{"class":217},"srdBf","1\n",[36,220,222],{"class":38,"line":221},6,[36,223,54],{"emptyLinePlaceholder":53},[36,225,227,230,233,236,239],{"class":38,"line":226},7,[36,228,229],{"class":42},"if",[36,231,232],{"class":46}," user_age ",[36,234,235],{"class":210},"\u003C",[36,237,238],{"class":217}," 0",[36,240,241],{"class":64},":\n",[36,243,245,248,250,252,255,257],{"class":38,"line":244},8,[36,246,247],{"class":60},"    print",[36,249,65],{"class":64},[36,251,69],{"class":68},[36,253,254],{"class":72},"Age cannot be negative",[36,256,69],{"class":68},[36,258,78],{"class":64},[36,260,262,265,267,269],{"class":38,"line":261},9,[36,263,264],{"class":46},"    sys",[36,266,87],{"class":64},[36,268,91],{"class":90},[36,270,94],{"class":64},[36,272,274],{"class":38,"line":273},10,[36,275,54],{"emptyLinePlaceholder":53},[36,277,279,281,283,285,287,289],{"class":38,"line":278},11,[36,280,61],{"class":60},[36,282,65],{"class":64},[36,284,69],{"class":68},[36,286,106],{"class":72},[36,288,69],{"class":68},[36,290,78],{"class":64},[292,293,295],"h3",{"id":294},"what-this-does","What this does",[127,297,298,306,312],{},[130,299,300,303,304],{},[33,301,302],{},"import sys"," gives you access to ",[33,305,116],{},[130,307,308,309,311],{},"When Python reaches ",[33,310,116],{},", it stops the script",[130,313,314],{},"Any code after that line will not run",[14,316,317],{},"This is a common choice for command-line scripts.",[14,319,320,321,328,329,87],{},"If you want to learn more about the module itself, see the ",[322,323,325,327],"a",{"href":324},"\u002Fstandard-library\u002Fpython-sys-module-overview",[33,326,84],{}," module overview",". For a deeper explanation of this function, see ",[322,330,332,334],{"href":331},"\u002Fstandard-library\u002Fsys.exit-function-explained",[33,333,116],{}," explained",[292,336,338,339],{"id":337},"pass-a-message-to-sysexit","Pass a message to ",[33,340,116],{},[14,342,343],{},"You can also pass a message:",[26,345,347],{"className":28,"code":346,"language":30,"meta":31,"style":31},"import sys\n\nname = \"\"\n\nif name == \"\":\n    sys.exit(\"Name is required\")\n\nprint(\"Hello\", name)\n",[33,348,349,355,359,369,373,388,407,411],{"__ignoreMap":31},[36,350,351,353],{"class":38,"line":39},[36,352,43],{"class":42},[36,354,47],{"class":46},[36,356,357],{"class":38,"line":50},[36,358,54],{"emptyLinePlaceholder":53},[36,360,361,364,366],{"class":38,"line":57},[36,362,363],{"class":46},"name ",[36,365,211],{"class":210},[36,367,368],{"class":68}," \"\"\n",[36,370,371],{"class":38,"line":81},[36,372,54],{"emptyLinePlaceholder":53},[36,374,375,377,380,383,386],{"class":38,"line":97},[36,376,229],{"class":42},[36,378,379],{"class":46}," name ",[36,381,382],{"class":210},"==",[36,384,385],{"class":68}," \"\"",[36,387,241],{"class":64},[36,389,390,392,394,396,398,400,403,405],{"class":38,"line":221},[36,391,264],{"class":46},[36,393,87],{"class":64},[36,395,91],{"class":90},[36,397,65],{"class":64},[36,399,69],{"class":68},[36,401,402],{"class":72},"Name is required",[36,404,69],{"class":68},[36,406,78],{"class":64},[36,408,409],{"class":38,"line":226},[36,410,54],{"emptyLinePlaceholder":53},[36,412,413,415,417,419,422,424,427,430],{"class":38,"line":244},[36,414,61],{"class":60},[36,416,65],{"class":64},[36,418,69],{"class":68},[36,420,421],{"class":72},"Hello",[36,423,69],{"class":68},[36,425,426],{"class":64},",",[36,428,429],{"class":90}," name",[36,431,78],{"class":64},[14,433,434],{},"In many terminal environments, the message is shown before the program ends.",[21,436,438],{"id":437},"exit-with-a-status-code","Exit with a status code",[14,440,441],{},"Sometimes you want your program to tell the terminal or another program whether it succeeded.",[14,443,444],{},"That is what an exit status code is for.",[26,446,448],{"className":28,"code":447,"language":30,"meta":31,"style":31},"import sys\n\npassword = \"abc\"\n\nif len(password) \u003C 8:\n    print(\"Password is too short\")\n    sys.exit(1)\n\nprint(\"Password accepted\")\nsys.exit(0)\n",[33,449,450,456,460,476,480,503,518,533,537,552],{"__ignoreMap":31},[36,451,452,454],{"class":38,"line":39},[36,453,43],{"class":42},[36,455,47],{"class":46},[36,457,458],{"class":38,"line":50},[36,459,54],{"emptyLinePlaceholder":53},[36,461,462,465,467,470,473],{"class":38,"line":57},[36,463,464],{"class":46},"password ",[36,466,211],{"class":210},[36,468,469],{"class":68}," \"",[36,471,472],{"class":72},"abc",[36,474,475],{"class":68},"\"\n",[36,477,478],{"class":38,"line":81},[36,479,54],{"emptyLinePlaceholder":53},[36,481,482,484,487,489,492,495,498,501],{"class":38,"line":97},[36,483,229],{"class":42},[36,485,486],{"class":60}," len",[36,488,65],{"class":64},[36,490,491],{"class":90},"password",[36,493,494],{"class":64},")",[36,496,497],{"class":210}," \u003C",[36,499,500],{"class":217}," 8",[36,502,241],{"class":64},[36,504,505,507,509,511,514,516],{"class":38,"line":221},[36,506,247],{"class":60},[36,508,65],{"class":64},[36,510,69],{"class":68},[36,512,513],{"class":72},"Password is too short",[36,515,69],{"class":68},[36,517,78],{"class":64},[36,519,520,522,524,526,528,531],{"class":38,"line":226},[36,521,264],{"class":46},[36,523,87],{"class":64},[36,525,91],{"class":90},[36,527,65],{"class":64},[36,529,530],{"class":217},"1",[36,532,78],{"class":64},[36,534,535],{"class":38,"line":244},[36,536,54],{"emptyLinePlaceholder":53},[36,538,539,541,543,545,548,550],{"class":38,"line":261},[36,540,61],{"class":60},[36,542,65],{"class":64},[36,544,69],{"class":68},[36,546,547],{"class":72},"Password accepted",[36,549,69],{"class":68},[36,551,78],{"class":64},[36,553,554,556,558,560,562,565],{"class":38,"line":273},[36,555,84],{"class":46},[36,557,87],{"class":64},[36,559,91],{"class":90},[36,561,65],{"class":64},[36,563,564],{"class":217},"0",[36,566,78],{"class":64},[292,568,570],{"id":569},"common-status-codes","Common status codes",[127,572,573,579,585],{},[130,574,575,578],{},[33,576,577],{},"sys.exit(0)"," usually means success",[130,580,581,584],{},[33,582,583],{},"sys.exit(1)"," usually means an error",[130,586,587],{},"Any non-zero number usually means something went wrong",[14,589,590],{},"This matters most when your script is run:",[127,592,593,596,599,602],{},[130,594,595],{},"from the terminal",[130,597,598],{},"by a shell script",[130,600,601],{},"by another program",[130,603,604],{},"by an automated tool",[292,606,608],{"id":607},"check-the-exit-code-in-a-terminal","Check the exit code in a terminal",[14,610,611],{},"Run your script:",[26,613,617],{"className":614,"code":615,"language":616,"meta":31,"style":31},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python your_script.py\n","bash",[33,618,619],{"__ignoreMap":31},[36,620,621,624],{"class":38,"line":39},[36,622,30],{"class":623},"sbgvK",[36,625,626],{"class":72}," your_script.py\n",[14,628,629],{},"Then check the exit status:",[26,631,633],{"className":614,"code":632,"language":616,"meta":31,"style":31},"python your_script.py; echo $?\n",[33,634,635],{"__ignoreMap":31},[36,636,637,639,642,645,648],{"class":38,"line":39},[36,638,30],{"class":623},[36,640,641],{"class":72}," your_script.py",[36,643,644],{"class":64},";",[36,646,647],{"class":60}," echo",[36,649,651],{"class":650},"s_hVV"," $?\n",[14,653,654],{},"You can also test it directly:",[26,656,658],{"className":614,"code":657,"language":616,"meta":31,"style":31},"python -c \"import sys; sys.exit(0)\"\npython -c \"import sys; sys.exit(1)\"\n",[33,659,660,675],{"__ignoreMap":31},[36,661,662,664,668,670,673],{"class":38,"line":39},[36,663,30],{"class":623},[36,665,667],{"class":666},"stzsN"," -c",[36,669,469],{"class":68},[36,671,672],{"class":72},"import sys; sys.exit(0)",[36,674,475],{"class":68},[36,676,677,679,681,683,686],{"class":38,"line":50},[36,678,30],{"class":623},[36,680,667],{"class":666},[36,682,469],{"class":68},[36,684,685],{"class":72},"import sys; sys.exit(1)",[36,687,475],{"class":68},[14,689,690],{},"These commands help you see how success and error codes work.",[21,692,113,694,696],{"id":693},"use-return-instead-of-exiting-the-whole-program",[33,695,151],{}," instead of exiting the whole program",[14,698,699,701,702,706],{},[33,700,151],{}," does ",[703,704,705],"strong",{},"not"," stop the whole script. It only leaves the current function.",[14,708,709],{},"This is often the better choice when you are inside a helper function.",[26,711,713],{"className":28,"code":712,"language":30,"meta":31,"style":31},"def greet(name):\n    if name == \"\":\n        print(\"No name provided\")\n        return\n\n    print(\"Hello,\", name)\n\n\ngreet(\"\")\nprint(\"Program continues\")\n",[33,714,715,734,747,763,768,772,791,795,799,811],{"__ignoreMap":31},[36,716,717,721,725,727,731],{"class":38,"line":39},[36,718,720],{"class":719},"sbsja","def",[36,722,724],{"class":723},"sGLFI"," greet",[36,726,65],{"class":64},[36,728,730],{"class":729},"sFwrP","name",[36,732,733],{"class":64},"):\n",[36,735,736,739,741,743,745],{"class":38,"line":50},[36,737,738],{"class":42},"    if",[36,740,379],{"class":46},[36,742,382],{"class":210},[36,744,385],{"class":68},[36,746,241],{"class":64},[36,748,749,752,754,756,759,761],{"class":38,"line":57},[36,750,751],{"class":60},"        print",[36,753,65],{"class":64},[36,755,69],{"class":68},[36,757,758],{"class":72},"No name provided",[36,760,69],{"class":68},[36,762,78],{"class":64},[36,764,765],{"class":38,"line":81},[36,766,767],{"class":42},"        return\n",[36,769,770],{"class":38,"line":97},[36,771,54],{"emptyLinePlaceholder":53},[36,773,774,776,778,780,783,785,787,789],{"class":38,"line":221},[36,775,247],{"class":60},[36,777,65],{"class":64},[36,779,69],{"class":68},[36,781,782],{"class":72},"Hello,",[36,784,69],{"class":68},[36,786,426],{"class":64},[36,788,429],{"class":90},[36,790,78],{"class":64},[36,792,793],{"class":38,"line":226},[36,794,54],{"emptyLinePlaceholder":53},[36,796,797],{"class":38,"line":244},[36,798,54],{"emptyLinePlaceholder":53},[36,800,801,804,806,809],{"class":38,"line":261},[36,802,803],{"class":90},"greet",[36,805,65],{"class":64},[36,807,808],{"class":68},"\"\"",[36,810,78],{"class":64},[36,812,813,815,817,819,822,824],{"class":38,"line":273},[36,814,61],{"class":60},[36,816,65],{"class":64},[36,818,69],{"class":68},[36,820,821],{"class":72},"Program continues",[36,823,69],{"class":68},[36,825,78],{"class":64},[292,827,829],{"id":828},"output","Output",[26,831,833],{"className":28,"code":832,"language":30,"meta":31,"style":31},"No name provided\nProgram continues\n",[33,834,835,840],{"__ignoreMap":31},[36,836,837],{"class":38,"line":39},[36,838,839],{"class":46},"No name provided\n",[36,841,842],{"class":38,"line":50},[36,843,844],{"class":46},"Program continues\n",[14,846,847,848,850],{},"This is cleaner than calling ",[33,849,116],{}," from deep inside a reusable function.",[14,852,113,853,855],{},[33,854,151],{}," when:",[127,857,858,861,864],{},[130,859,860],{},"you only want to stop the current function",[130,862,863],{},"the rest of the program should continue",[130,865,866],{},"you are writing reusable code",[14,868,869,870,87],{},"If you are not comfortable with functions yet, see ",[322,871,873],{"href":872},"\u002Flearn\u002Fpython-functions-explained","Python functions explained",[21,875,877,880],{"id":876},"break-is-not-the-same-as-exiting",[33,878,879],{},"break"," is not the same as exiting",[14,882,883,884,886],{},"A very common mistake is using ",[33,885,879],{}," when you really want to stop the whole script.",[14,888,889,891],{},[33,890,879],{}," only stops the current loop.",[26,893,895],{"className":28,"code":894,"language":30,"meta":31,"style":31},"for number in range(5):\n    if number == 2:\n        break\n    print(number)\n\nprint(\"Program continues after the loop\")\n",[33,896,897,918,931,936,947,951],{"__ignoreMap":31},[36,898,899,902,905,908,911,913,916],{"class":38,"line":39},[36,900,901],{"class":42},"for",[36,903,904],{"class":46}," number ",[36,906,907],{"class":42},"in",[36,909,910],{"class":60}," range",[36,912,65],{"class":64},[36,914,915],{"class":217},"5",[36,917,733],{"class":64},[36,919,920,922,924,926,929],{"class":38,"line":50},[36,921,738],{"class":42},[36,923,904],{"class":46},[36,925,382],{"class":210},[36,927,928],{"class":217}," 2",[36,930,241],{"class":64},[36,932,933],{"class":38,"line":57},[36,934,935],{"class":42},"        break\n",[36,937,938,940,942,945],{"class":38,"line":81},[36,939,247],{"class":60},[36,941,65],{"class":64},[36,943,944],{"class":90},"number",[36,946,78],{"class":64},[36,948,949],{"class":38,"line":97},[36,950,54],{"emptyLinePlaceholder":53},[36,952,953,955,957,959,962,964],{"class":38,"line":221},[36,954,61],{"class":60},[36,956,65],{"class":64},[36,958,69],{"class":68},[36,960,961],{"class":72},"Program continues after the loop",[36,963,69],{"class":68},[36,965,78],{"class":64},[292,967,829],{"id":968},"output-1",[26,970,972],{"className":28,"code":971,"language":30,"meta":31,"style":31},"0\n1\nProgram continues after the loop\n",[33,973,974,979,983],{"__ignoreMap":31},[36,975,976],{"class":38,"line":39},[36,977,978],{"class":217},"0\n",[36,980,981],{"class":38,"line":50},[36,982,218],{"class":217},[36,984,985],{"class":38,"line":57},[36,986,987],{"class":46},"Program continues after the loop\n",[14,989,990,991,87],{},"If you want to stop repeating, use ",[33,992,879],{},[14,994,995,996,87],{},"If you want to stop the entire script, use ",[33,997,116],{},[14,999,1000],{},"Here is the difference:",[26,1002,1004],{"className":28,"code":1003,"language":30,"meta":31,"style":31},"import sys\n\nfor number in range(5):\n    if number == 2:\n        sys.exit()\n    print(number)\n\nprint(\"This line will not run\")\n",[33,1005,1006,1012,1016,1032,1044,1055,1065,1069],{"__ignoreMap":31},[36,1007,1008,1010],{"class":38,"line":39},[36,1009,43],{"class":42},[36,1011,47],{"class":46},[36,1013,1014],{"class":38,"line":50},[36,1015,54],{"emptyLinePlaceholder":53},[36,1017,1018,1020,1022,1024,1026,1028,1030],{"class":38,"line":57},[36,1019,901],{"class":42},[36,1021,904],{"class":46},[36,1023,907],{"class":42},[36,1025,910],{"class":60},[36,1027,65],{"class":64},[36,1029,915],{"class":217},[36,1031,733],{"class":64},[36,1033,1034,1036,1038,1040,1042],{"class":38,"line":81},[36,1035,738],{"class":42},[36,1037,904],{"class":46},[36,1039,382],{"class":210},[36,1041,928],{"class":217},[36,1043,241],{"class":64},[36,1045,1046,1049,1051,1053],{"class":38,"line":97},[36,1047,1048],{"class":46},"        sys",[36,1050,87],{"class":64},[36,1052,91],{"class":90},[36,1054,94],{"class":64},[36,1056,1057,1059,1061,1063],{"class":38,"line":221},[36,1058,247],{"class":60},[36,1060,65],{"class":64},[36,1062,944],{"class":90},[36,1064,78],{"class":64},[36,1066,1067],{"class":38,"line":226},[36,1068,54],{"emptyLinePlaceholder":53},[36,1070,1071,1073,1075,1077,1079,1081],{"class":38,"line":244},[36,1072,61],{"class":60},[36,1074,65],{"class":64},[36,1076,69],{"class":68},[36,1078,106],{"class":72},[36,1080,69],{"class":68},[36,1082,78],{"class":64},[14,1084,1085,1086,87],{},"To learn more about loop control, see ",[322,1087,1089,1090,1092,1093,1096],{"href":1088},"\u002Flearn\u002Fpython-break-and-continue-statements","Python ",[33,1091,879],{}," and ",[33,1094,1095],{},"continue"," statements",[21,1098,1100,1102],{"id":1099},"raise-systemexit-as-another-option",[33,1101,147],{}," as another option",[14,1104,1105,1107,1108,87],{},[33,1106,116],{}," works by raising a special exception called ",[33,1109,120],{},[14,1111,1112],{},"So these are closely related:",[26,1114,1116],{"className":28,"code":1115,"language":30,"meta":31,"style":31},"import sys\n\nsys.exit()\n",[33,1117,1118,1124,1128],{"__ignoreMap":31},[36,1119,1120,1122],{"class":38,"line":39},[36,1121,43],{"class":42},[36,1123,47],{"class":46},[36,1125,1126],{"class":38,"line":50},[36,1127,54],{"emptyLinePlaceholder":53},[36,1129,1130,1132,1134,1136],{"class":38,"line":57},[36,1131,84],{"class":46},[36,1133,87],{"class":64},[36,1135,91],{"class":90},[36,1137,94],{"class":64},[26,1139,1141],{"className":28,"code":1140,"language":30,"meta":31,"style":31},"raise SystemExit\n",[33,1142,1143],{"__ignoreMap":31},[36,1144,1145,1148],{"class":38,"line":39},[36,1146,1147],{"class":42},"raise",[36,1149,1151],{"class":1150},"sZMiF"," SystemExit\n",[14,1153,1154],{},"You can also include a message or code:",[26,1156,1158],{"className":28,"code":1157,"language":30,"meta":31,"style":31},"raise SystemExit(\"Stopping program\")\n",[33,1159,1160],{"__ignoreMap":31},[36,1161,1162,1164,1167,1169,1171,1174,1176],{"class":38,"line":39},[36,1163,1147],{"class":42},[36,1165,1166],{"class":1150}," SystemExit",[36,1168,65],{"class":64},[36,1170,69],{"class":68},[36,1172,1173],{"class":72},"Stopping program",[36,1175,69],{"class":68},[36,1177,78],{"class":64},[14,1179,1180,1181,1183],{},"For most beginners, ",[33,1182,116],{}," is the clearer choice.",[14,1185,1186,1187,1189,1190,87],{},"Still, it helps to know about ",[33,1188,120],{}," because you may see it in error output or in advanced code. See ",[322,1191,1193],{"href":1192},"\u002Ferrors\u002Fkeyboardinterrupt-exception-in-python-explained","SystemExit exception in Python explained",[21,1195,1197],{"id":1196},"what-happens-in-interactive-environments","What happens in interactive environments",[14,1199,1200,1202],{},[33,1201,116],{}," can look a little different depending on where you run your code.",[14,1204,1205],{},"Examples:",[127,1207,1208,1211,1214,1217],{},[130,1209,1210],{},"IDLE",[130,1212,1213],{},"Jupyter notebooks",[130,1215,1216],{},"some IDEs",[130,1218,1219],{},"the terminal",[14,1221,1222],{},"In a terminal script, the Python process usually ends completely.",[14,1224,1225],{},"In an IDE or notebook, the script may stop running, but the environment itself stays open.",[14,1227,1228],{},"That is normal. The important thing is this:",[127,1230,1231,1237],{},[130,1232,1233,1234,1236],{},"code before ",[33,1235,116],{}," runs",[130,1238,1239,1240,1242],{},"code after ",[33,1241,116],{}," does not run",[21,1244,1246],{"id":1245},"common-mistakes","Common mistakes",[14,1248,1249],{},"Here are some common problems beginners run into.",[292,1251,1253,1254,1256],{"id":1252},"using-break-when-you-meant-to-stop-the-whole-program","Using ",[33,1255,879],{}," when you meant to stop the whole program",[14,1258,1259,1261],{},[33,1260,879],{}," only exits the current loop.",[14,1263,1264,1265,1267],{},"If you want to end the script, use ",[33,1266,116],{}," instead.",[292,1269,1253,1271,1273],{"id":1270},"using-return-outside-a-function",[33,1272,151],{}," outside a function",[14,1275,1276],{},"This is not allowed in normal Python code.",[14,1278,1279,1281],{},[33,1280,151],{}," can only be used inside a function.",[14,1283,1284],{},"Correct:",[26,1286,1288],{"className":28,"code":1287,"language":30,"meta":31,"style":31},"def check_value(x):\n    if x \u003C 0:\n        return\n    print(x)\n",[33,1289,1290,1304,1317,1321],{"__ignoreMap":31},[36,1291,1292,1294,1297,1299,1302],{"class":38,"line":39},[36,1293,720],{"class":719},[36,1295,1296],{"class":723}," check_value",[36,1298,65],{"class":64},[36,1300,1301],{"class":729},"x",[36,1303,733],{"class":64},[36,1305,1306,1308,1311,1313,1315],{"class":38,"line":50},[36,1307,738],{"class":42},[36,1309,1310],{"class":46}," x ",[36,1312,235],{"class":210},[36,1314,238],{"class":217},[36,1316,241],{"class":64},[36,1318,1319],{"class":38,"line":57},[36,1320,767],{"class":42},[36,1322,1323,1325,1327,1329],{"class":38,"line":81},[36,1324,247],{"class":60},[36,1326,65],{"class":64},[36,1328,1301],{"class":90},[36,1330,78],{"class":64},[14,1332,1333],{},"Incorrect:",[26,1335,1337],{"className":28,"code":1336,"language":30,"meta":31,"style":31},"x = -1\n\nif x \u003C 0:\n    return\n",[33,1338,1339,1350,1354,1366],{"__ignoreMap":31},[36,1340,1341,1344,1346,1348],{"class":38,"line":39},[36,1342,1343],{"class":46},"x ",[36,1345,211],{"class":210},[36,1347,214],{"class":210},[36,1349,218],{"class":217},[36,1351,1352],{"class":38,"line":50},[36,1353,54],{"emptyLinePlaceholder":53},[36,1355,1356,1358,1360,1362,1364],{"class":38,"line":57},[36,1357,229],{"class":42},[36,1359,1310],{"class":46},[36,1361,235],{"class":210},[36,1363,238],{"class":217},[36,1365,241],{"class":64},[36,1367,1368],{"class":38,"line":81},[36,1369,1370],{"class":42},"    return\n",[292,1372,1374,1375],{"id":1373},"forgetting-to-import-sys","Forgetting to import ",[33,1376,84],{},[14,1378,1379],{},"This will fail:",[26,1381,1383],{"className":28,"code":1382,"language":30,"meta":31,"style":31},"sys.exit()\n",[33,1384,1385],{"__ignoreMap":31},[36,1386,1387,1389,1391,1393],{"class":38,"line":39},[36,1388,84],{"class":46},[36,1390,87],{"class":64},[36,1392,91],{"class":90},[36,1394,94],{"class":64},[14,1396,1397],{},"This works:",[26,1399,1400],{"className":28,"code":1115,"language":30,"meta":31,"style":31},[33,1401,1402,1408,1412],{"__ignoreMap":31},[36,1403,1404,1406],{"class":38,"line":39},[36,1405,43],{"class":42},[36,1407,47],{"class":46},[36,1409,1410],{"class":38,"line":50},[36,1411,54],{"emptyLinePlaceholder":53},[36,1413,1414,1416,1418,1420],{"class":38,"line":57},[36,1415,84],{"class":46},[36,1417,87],{"class":64},[36,1419,91],{"class":90},[36,1421,94],{"class":64},[292,1423,1425,1426,1428],{"id":1424},"expecting-code-after-sysexit-to-still-run","Expecting code after ",[33,1427,116],{}," to still run",[14,1430,1431],{},"It will not.",[26,1433,1435],{"className":28,"code":1434,"language":30,"meta":31,"style":31},"import sys\n\nprint(\"Before exit\")\nsys.exit()\nprint(\"After exit\")\n",[33,1436,1437,1443,1447,1462,1472],{"__ignoreMap":31},[36,1438,1439,1441],{"class":38,"line":39},[36,1440,43],{"class":42},[36,1442,47],{"class":46},[36,1444,1445],{"class":38,"line":50},[36,1446,54],{"emptyLinePlaceholder":53},[36,1448,1449,1451,1453,1455,1458,1460],{"class":38,"line":57},[36,1450,61],{"class":60},[36,1452,65],{"class":64},[36,1454,69],{"class":68},[36,1456,1457],{"class":72},"Before exit",[36,1459,69],{"class":68},[36,1461,78],{"class":64},[36,1463,1464,1466,1468,1470],{"class":38,"line":81},[36,1465,84],{"class":46},[36,1467,87],{"class":64},[36,1469,91],{"class":90},[36,1471,94],{"class":64},[36,1473,1474,1476,1478,1480,1483,1485],{"class":38,"line":97},[36,1475,61],{"class":60},[36,1477,65],{"class":64},[36,1479,69],{"class":68},[36,1481,1482],{"class":72},"After exit",[36,1484,69],{"class":68},[36,1486,78],{"class":64},[14,1488,1489,1490,1493],{},"Only ",[33,1491,1492],{},"\"Before exit\""," is printed.",[292,1495,1497,1498,1500],{"id":1496},"catching-systemexit-by-accident","Catching ",[33,1499,120],{}," by accident",[14,1502,1503,1504,1506,1507,1509],{},"Because ",[33,1505,116],{}," raises ",[33,1508,120],{},", a broad exception handler can sometimes interfere with it.",[26,1511,1513],{"className":28,"code":1512,"language":30,"meta":31,"style":31},"import sys\n\ntry:\n    sys.exit()\nexcept BaseException:\n    print(\"Caught something\")\n",[33,1514,1515,1521,1525,1532,1542,1552],{"__ignoreMap":31},[36,1516,1517,1519],{"class":38,"line":39},[36,1518,43],{"class":42},[36,1520,47],{"class":46},[36,1522,1523],{"class":38,"line":50},[36,1524,54],{"emptyLinePlaceholder":53},[36,1526,1527,1530],{"class":38,"line":57},[36,1528,1529],{"class":42},"try",[36,1531,241],{"class":64},[36,1533,1534,1536,1538,1540],{"class":38,"line":81},[36,1535,264],{"class":46},[36,1537,87],{"class":64},[36,1539,91],{"class":90},[36,1541,94],{"class":64},[36,1543,1544,1547,1550],{"class":38,"line":97},[36,1545,1546],{"class":42},"except",[36,1548,1549],{"class":1150}," BaseException",[36,1551,241],{"class":64},[36,1553,1554,1556,1558,1560,1563,1565],{"class":38,"line":221},[36,1555,247],{"class":60},[36,1557,65],{"class":64},[36,1559,69],{"class":68},[36,1561,1562],{"class":72},"Caught something",[36,1564,69],{"class":68},[36,1566,78],{"class":64},[14,1568,1569,1570,1573],{},"In this example, the program may not exit as expected because ",[33,1571,1572],{},"BaseException"," is very broad.",[14,1575,1576,1577,1092,1579,1581,1582,87],{},"In beginner code, it is usually better to catch specific exceptions with ",[33,1578,1529],{},[33,1580,1546],{},". See ",[322,1583,1585,1586,1588,1589,1591],{"href":1584},"\u002Fhow-to\u002Fhow-to-use-try-except-blocks-in-python\u002F","how to use ",[33,1587,1529],{},"\u002F",[33,1590,1546],{}," blocks in Python",[21,1593,1595],{"id":1594},"faq","FAQ",[292,1597,1599],{"id":1598},"what-is-the-easiest-way-to-exit-a-python-program","What is the easiest way to exit a Python program?",[14,1601,113,1602,1604],{},[33,1603,116],{}," in most scripts. It is clear and commonly used.",[292,1606,1608,1609,1611],{"id":1607},"does-break-exit-the-program","Does ",[33,1610,879],{}," exit the program?",[14,1613,1614,1615,891],{},"No. ",[33,1616,879],{},[292,1618,1608,1620,1611],{"id":1619},"does-return-exit-the-program",[33,1621,151],{},[14,1623,1614,1624,1626],{},[33,1625,151],{}," exits the current function. The rest of the program may continue.",[292,1628,1630,1631,1633],{"id":1629},"what-does-sysexit1-mean","What does ",[33,1632,583],{}," mean?",[14,1635,1636],{},"It ends the program with a non-zero exit status, which usually signals an error.",[292,1638,1640,1641,1643],{"id":1639},"why-does-my-ide-stay-open-after-sysexit","Why does my IDE stay open after ",[33,1642,116],{},"?",[14,1645,1646],{},"The script stops, but the IDE or notebook environment itself usually keeps running.",[21,1648,1650],{"id":1649},"see-also","See also",[127,1652,1653,1660,1666,1674,1678,1682],{},[130,1654,1655],{},[322,1656,1657,1659],{"href":331},[33,1658,116],{}," function explained",[130,1661,1662],{},[322,1663,1089,1664,327],{"href":324},[33,1665,84],{},[130,1667,1668],{},[322,1669,1089,1670,1092,1672,1096],{"href":1088},[33,1671,879],{},[33,1673,1095],{},[130,1675,1676],{},[322,1677,873],{"href":872},[130,1679,1680],{},[322,1681,1193],{"href":1192},[130,1683,1684],{},[322,1685,1686,1687,1588,1689,1591],{"href":1584},"How to use ",[33,1688,1529],{},[33,1690,1546],{},[1692,1693,1694],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sFwrP, html code.shiki .sFwrP{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":31,"searchDepth":50,"depth":50,"links":1696},[1697,1698,1699,1705,1709,1713,1717,1719,1720,1732,1743],{"id":23,"depth":50,"text":24},{"id":124,"depth":50,"text":125},{"id":154,"depth":50,"text":1700,"children":1701},"Use sys.exit() to end the program",[1702,1703],{"id":294,"depth":57,"text":295},{"id":337,"depth":57,"text":1704},"Pass a message to sys.exit()",{"id":437,"depth":50,"text":438,"children":1706},[1707,1708],{"id":569,"depth":57,"text":570},{"id":607,"depth":57,"text":608},{"id":693,"depth":50,"text":1710,"children":1711},"Use return instead of exiting the whole program",[1712],{"id":828,"depth":57,"text":829},{"id":876,"depth":50,"text":1714,"children":1715},"break is not the same as exiting",[1716],{"id":968,"depth":57,"text":829},{"id":1099,"depth":50,"text":1718},"raise SystemExit as another option",{"id":1196,"depth":50,"text":1197},{"id":1245,"depth":50,"text":1246,"children":1721},[1722,1724,1726,1728,1730],{"id":1252,"depth":57,"text":1723},"Using break when you meant to stop the whole program",{"id":1270,"depth":57,"text":1725},"Using return outside a function",{"id":1373,"depth":57,"text":1727},"Forgetting to import sys",{"id":1424,"depth":57,"text":1729},"Expecting code after sys.exit() to still run",{"id":1496,"depth":57,"text":1731},"Catching SystemExit by accident",{"id":1594,"depth":50,"text":1595,"children":1733},[1734,1735,1737,1739,1741],{"id":1598,"depth":57,"text":1599},{"id":1607,"depth":57,"text":1736},"Does break exit the program?",{"id":1619,"depth":57,"text":1738},"Does return exit the program?",{"id":1629,"depth":57,"text":1740},"What does sys.exit(1) mean?",{"id":1639,"depth":57,"text":1742},"Why does my IDE stay open after sys.exit()?",{"id":1649,"depth":50,"text":1650},"Master how to exit a program in python in our comprehensive Python beginner guide.","md",{},"\u002Fhow-to\u002Fhow-to-exit-a-program-in-python",{"title":5,"description":1744},"how-to\u002Fhow-to-exit-a-program-in-python","0THTIjKXCBngLlSgDq87AWXw_RI0AYNnvlwJv3Np0PU",1777585483742]