{"id":7632,"date":"2019-08-08T02:47:14","date_gmt":"2019-08-08T09:47:14","guid":{"rendered":"https:\/\/www.coretechnologies.com\/blog\/?p=7632"},"modified":"2023-07-26T16:33:47","modified_gmt":"2023-07-26T23:33:47","slug":"python-windows-service-output","status":"publish","type":"post","link":"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/python-windows-service-output\/","title":{"rendered":"Q&#038;A: How Do I See the Output\/Traceback From my Python Windows Service?"},"content":{"rendered":"<div align=\"center\"><img loading=\"lazy\" decoding=\"async\" class=\"no-lazy-load\" src=\"\/blog\/images\/qa-python-output.webp\" title=\"Q&#038;A - Python Output\" alt=\"Q&#038;A - Python Output\" border=\"0\" width=\"380\" height=\"154\" \/><\/div>\n<div class=\"blog-qa-question-box\">\n<img loading=\"lazy\" decoding=\"async\" class=\"no-lazy-load\" src=\"https:\/\/cdn.coretechnologies.com\/images\/quotes-transparent-21x21.webp\" width=\"21\" height=\"21\" \/>&nbsp;&nbsp;We&#8217;re using AlwaysUp to run a <a href=\"\/products\/AlwaysUp\/Apps\/RunPythonScriptAsAService.html\">Python program as a service<\/a>. Do you have any recommendations about logging?<\/p>\n<p>Ideally I would see the output of the python program as if it were run in command line, to see the traceback if there is an error. For example:<\/p>\n<pre>\r\n    Traceback (most recent call last):\r\n    File \"<stdin>\", line 1, in <module>\r\n    NameError: name 'r' is not defined\r\n<\/pre>\n<p align=\"right\">&mdash; Liam<\/p>\n<\/div>\n<p>Hi Liam. I see your problem. It&#8217;s important to know when Python runs into a problem, but errors can be difficult to spot when the service&#8217;s console &mdash; alive and well in the <a href=\"\/WindowsServices\/FAQ.html#WhatIsSession0Isolation\">isolated Session 0<\/a> &mdash; isn&#8217;t visible on your desktop.<\/p>\n<p>You have a couple of options:<\/p>\n<h2 class=\"blog-caption-numbered\">1. Capture the output of your Python program in a text file<\/h2>\n<p>AlwaysUp can capture Python&#8217;s command line output to a file of your choosing. That option is available on the <b>Extras<\/b> tab:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/alwaysup-extras-tab-capture-output.png\" class=\"zoomPopup\" title=\"Capture Output from the Extras tab\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/alwaysup-extras-tab-capture-output.png\" title=\"Capture Output from the Extras tab (click to enlarge)\" alt=\"Capture Output from the Extras tab\" border=\"0\" ><\/a><\/div>\n<p>Check the <b>Capture output to this log file<\/b> box and enter the full path to a (new) text file.<\/p>\n<p>If your script generates lots of text, you may want to prevent the file from growing too large. Check the <b>Automatically trim<\/b> box and specify the maximum size in megabytes. When the file grows to the maximum size, the oldest 10% will be discarded to make room for new entries. Be sure to tune the max size so that you don&#8217;t lose useful data.<\/p>\n<p>With those settings in place, your Python service will write all console\/traceback output to the file. Both the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Standard_streams#Standard_output_(stdout)\" target=\"_blank\" rel=\"noopener noreferrer\">standard output<\/a> and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Standard_streams#Standard_error_(stderr)\" target=\"_blank\" rel=\"noopener noreferrer\">standard error<\/a> streams will be captured.<\/p>\n<p>Open the file to see what&#8217;s going on with your script. Or even better, use the free <a href=\"https:\/\/tailforwin32.sourceforge.net\/\" target=\"_blank\" rel=\"noopener noreferrer\">Tail for Win32<\/a> to &#8220;track&#8221; the file. New lines will appear in real-time &mdash; just if you were looking at the Python console.<\/p>\n<p>To confirm our advice, we ran a Python script with a deliberate error (borrowed from <a href=\"https:\/\/en.wikipedia.org\/wiki\/Stack_trace\" target=\"_blank\" rel=\"noopener noreferrer\">this page<\/a>) and captured the output. Here is the result (with WinTail monitoring the output file):<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/python-script-traceback.png\" class=\"zoomPopup\" title=\"Python script: Traceback output\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/python-script-traceback.png\" title=\"Python script: Traceback output (click to enlarge)\" alt=\"Python script: Traceback output\" border=\"0\" width=\"520\"><\/a><\/div>\n<p>It works!<\/p>\n<h2 class=\"blog-caption-numbered\">2. Run your Python script in the current session, where you will see the console<\/h2>\n<p>If you don&#8217;t want to configure logging &mdash; or you just want to see the console temporarily while debugging a problem &mdash; you can instruct AlwaysUp to run the Python console on your desktop.<\/p>\n<p>Simply select <b>Application &gt; Restart in this session<\/b> and AlwaysUp will temporarily stop Python and &#8220;re-parent&#8221; it onto your screen:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/restart-python-program-in-this-session.png\" class=\"zoomPopup\" title=\"Restart Python program in this session\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/restart-python-program-in-this-session.png\" title=\"Restart Python program in this session (click to enlarge)\" alt=\"Restart Python program in this session\" border=\"0\" width=\"520\"><\/a><\/div>\n<p>Your program will run visibly while you are logged in. When you logout, AlwaysUp will automatically return your application to the background (i.e. Session 0).<\/p>\n<h2 class=\"blog-caption-numbered\">One final bit of advice&#8230;<\/h2>\n<p>We recommend that you do your best to thoroughly debug your Python script <b>before<\/b> running it as a set-it-and-forget-it Windows Service. Dynamic languages like Python need extra attention in that area.<\/p>\n<p>The fewer surprises in production the better, right?<\/p>\n<!-- relpost-thumb-wrapper --><div class=\"relpost-thumb-wrapper\"><!-- filter-class --><div class=\"relpost-thumb-container\"><style>.relpost-block-single-image, .relpost-post-image { margin-bottom: 10px; }<\/style><h3>You may also like...<\/h3><div style=\"clear: both\"><\/div><div style=\"clear: both\"><\/div><!-- relpost-block-container --><div class=\"relpost-block-container relpost-block-column-layout\" style=\"--relposth-columns: 3;--relposth-columns_t: 2; --relposth-columns_m: 2\"><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/alwaysup-feature-spotlight-run-your-app-without-admin-rights\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"AlwaysUp Feature Spotlight: Run your App Without Admin Rights\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/windows-service-no-admin-rights-150x150-1.webp\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">AlwaysUp Feature Spotlight: Run your App Without Admin Rights<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/multiple-automatic-logons\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Q&amp;A: Does AlwaysUp Support Multiple Automatic Logons?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/multiple-autologon-services.webp\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Q&amp;A: Does AlwaysUp Support Multiple Automatic Logons?<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/run-batch-file-once-a-day\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Q&amp;A: How Do I Run My Batch File Once A Day With AlwaysUp?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/run-batch-file-once-daily-150x150-1.webp\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Q&amp;A: How Do I Run My Batch File Once A Day With AlwaysUp?<\/h2><\/div><\/div><\/a><\/div><!-- close relpost-block-container --><div style=\"clear: both\"><\/div><\/div><!-- close filter class --><\/div><!-- close relpost-thumb-wrapper -->","protected":false},"excerpt":{"rendered":"<p>&nbsp;&nbsp;We&#8217;re using AlwaysUp to run a Python program as a service. Do you have any recommendations about logging? Ideally I would see the output of the python program as if it were run in command line, to see the traceback &hellip; <a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/python-windows-service-output\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":7673,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[26,49,58,126,127,199],"class_list":["post-7632","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-alwaysup","tag-alwaysup-tag","tag-capture-output","tag-console-program","tag-python-script","tag-qa","tag-wintail"],"_links":{"self":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/7632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=7632"}],"version-history":[{"count":3,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/7632\/revisions"}],"predecessor-version":[{"id":12121,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/7632\/revisions\/12121"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media\/7673"}],"wp:attachment":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=7632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=7632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=7632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}