{"id":252,"date":"2025-11-17T19:26:58","date_gmt":"2025-11-17T19:26:58","guid":{"rendered":"https:\/\/privateblog.nobytesleft.net\/?p=252"},"modified":"2025-11-17T19:26:58","modified_gmt":"2025-11-17T19:26:58","slug":"pycharm-debugging-with-separate-console","status":"publish","type":"post","link":"https:\/\/privateblog.nobytesleft.net\/?p=252","title":{"rendered":"PyCharm debugging with separate console"},"content":{"rendered":"\n<p>In PyCharm, debugging a Python script puts its I\/O in the PyCharm console. Unfortunately, it is not a complete console, so if your script uses advanced I\/O features, such as waiting for key presses or colored output, it will not work correctly.<\/p>\n\n\n\n<p>In order to have it work, you need to run the script in a separate console window, and connect to the PyCharm debugger. This is done as follows.<\/p>\n\n\n\n<p>First of all, create an external tool which will run the script in a dedicated console.<\/p>\n\n\n\n<p>Name: gnome-terminal-&lt;myprogram><br>Program: gnome-terminal<br>Arguments: &#8212; \/bin\/bash -c &#8220;sleep 2; venv\/bin\/python3 pycharm; sleep 2&#8221;<br>Working Directory: $ProjectFileDir$<\/p>\n\n\n\n<p>This runs &lt;myprogram> with the Python executable in the venv\/bin environment, in a dedicated gnome-terminal.<\/p>\n\n\n\n<p>Then, create a new Run\/debug configuration &#8220;Python Debug Server&#8221;.<br>Name: Remote &lt;myprogram><br>IDE host name: localhost<br>Port: 45257<br>Before launch: Add New Task -> Run external tool &#8220;gnome-terminal-&lt;myprogram>&#8221;<\/p>\n\n\n\n<p>At the end, in the program code add the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    if sys.argv&#91;1] == \"pycharm\":\n        # force keeping the original stdin.\n        original_stdin = sys.stdin\n        import pydevd_pycharm\n        pydevd_pycharm.settrace('localhost', port=45257, suspend=False, trace_only_current_thread=False)\n        sys.stdin = original_stdin\n        sys.argv = sys.argv&#91;0:1] + sys.argv&#91;2:]\nexcept IndexError:\n    pass<\/code><\/pre>\n\n\n\n<p>If the first argument passed to the program is &#8220;pycharm&#8221;, it will be erased and the program will connect to the debug server started by the debug configuration.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In PyCharm, debugging a Python script puts its I\/O in the PyCharm console. Unfortunately, it is not a complete console, so if your script uses advanced I\/O features, such as&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[14,13,12,11,15],"class_list":["post-252","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-console","tag-debugging","tag-pycharm","tag-python","tag-terminal"],"_links":{"self":[{"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/posts\/252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=252"}],"version-history":[{"count":1,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":253,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/posts\/252\/revisions\/253"}],"wp:attachment":[{"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}