{"id":1618,"date":"2022-08-04T14:43:17","date_gmt":"2022-08-04T14:43:17","guid":{"rendered":"https:\/\/www.pyxll.com\/blog\/?p=1618"},"modified":"2025-09-18T14:01:43","modified_gmt":"2025-09-18T14:01:43","slug":"debug-python-code-in-excel-with-pycharm","status":"publish","type":"post","link":"https:\/\/www.pyxll.com\/blog\/debug-python-code-in-excel-with-pycharm\/","title":{"rendered":"Debug Python in Excel with PyCharm"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"h-what-is-pycharm\">What is PyCharm?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PyCharm is an extremely popular Python IDE by JetBrains. Among it&#8217;s many features is its excellent visual debugger. The debugger lets you step through Python code, line by line, and inspect variables. We can use this to debug our Python code running in Excel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For professional developers getting a development environment set up that works for you is essential. There&#8217;s no one right choice, but PyCharm offers a lot that can help increase productivity when writing Python code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When writing Python in Excel using PyXLL, you may find at times that you want to use a debugger. Stepping through your code can help you identify what&#8217;s going on. A good debugger can be a life saver, and much easier to work with than countless print statements!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PyCharm works well with PyXLL. It&#8217;s debugger can attach to the running Excel process, allowing you to step through your Python code in Excel, line by line. It works just in the same way as you can use PyCharm to step through a Python script!<\/p>\n\n\n\n<p class=\"has-text-color has-background wp-block-paragraph\" style=\"color:#475057;background-color:#c3c3c399\">At the time of writing you will require the professional (paid) version for the &#8220;Attach to Process&#8221; feature. This is required to attach the PyCharm debugger to PyXLL and debug Python in Excel.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-debugging-python-in-excel-with-pycharm\">Debugging Python in Excel with PyCharm<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PyCharm\u2019s debugger has an \u201cAttach To Process\u201d feature. That means you can attach the debugger to a running Python process, even one not started by PyCharm. This is perfect for attaching to the Python interpreter running in Excel through PyXLL!<\/p>\n\n\n\n<p class=\"has-background wp-block-paragraph\" style=\"background-color:#fcdf0091\"><strong>WARNING<\/strong>:<br><br>PyCharm does not yet properly support debugging of <strong>Python 3.10<\/strong>.<br><br><a href=\"https:\/\/youtrack.jetbrains.com\/issue\/PY-48037\/Support-attach-to-process-on-Windows-with-Python-310\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/youtrack.jetbrains.com\/issue\/PY-48037\/Support-attach-to-process-on-Windows-with-Python-310<\/a><br><br>If you are using Python 3.10 until this is resovled by JetBrains you will need to use the old-style &#8220;Remote Debugger&#8221; documented in the second half of this post.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-python-debugger-configuration\">Python Debugger Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">By default, you will only see processes called \u201cpython\u201d when attaching to a process in PyCharm. Before you can attach to Excel to debug your PyXLL functions you need to change. Go to File -&gt; Settings, and under the \u201cBuild, Execution, Deployment\u201d section find the \u201cPython Debugger\u201d settings. Here you will see the setting \u201cFor Attach To Process show processes with names containing:\u201d. This setting is configured as \u201cpython\u201d by default. Change this to \u201cexcel\u201d to show only Excel processes, or leave it blank to show all processes.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image.png\"><img decoding=\"async\" width=\"1024\" height=\"537\" data-attachment-id=\"1613\" data-permalink=\"https:\/\/www.pyxll.com\/blog\/debugging-your-python-excel-add-in\/image-17\/\" data-orig-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image.png\" data-orig-size=\"1114,584\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-1024x537.png\" src=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-1024x537.png\" alt=\"\" class=\"wp-image-1613\" srcset=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-1024x537.png 1024w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-300x157.png 300w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-768x403.png 768w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image.png 1114w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Python Debugger Settings: Attach to Excel<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-attaching-to-python-in-excel\">Attaching to Python in Excel<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">With Excel runinng and the PyXLL add-in loaded, in PyCharm go to \u201cRun -&gt; Attach to Process\u2026\u201d.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-1.png\"><img decoding=\"async\" width=\"1024\" height=\"667\" data-attachment-id=\"1614\" data-permalink=\"https:\/\/www.pyxll.com\/blog\/debugging-your-python-excel-add-in\/image-1-6\/\" data-orig-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-1.png\" data-orig-size=\"1551,1011\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-1-1024x667.png\" src=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-1-1024x667.png\" alt=\"\" class=\"wp-image-1614\" srcset=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-1-1024x667.png 1024w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-1-300x196.png 300w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-1-768x501.png 768w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-1-1536x1001.png 1536w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-1.png 1551w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">PyCharm: Attach to Process\u2026<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Next PyCharm will present you with a list of processes to choose from. If you followed the steps above to filter the processes containing \u201cexcel\u201d you will only see a single process. That is the Excel process you want to debug.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-2.png\"><img decoding=\"async\" width=\"601\" height=\"172\" data-attachment-id=\"1615\" data-permalink=\"https:\/\/www.pyxll.com\/blog\/debugging-your-python-excel-add-in\/image-2-6\/\" data-orig-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-2.png\" data-orig-size=\"601,172\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-2.png\" src=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-2.png\" alt=\"\" class=\"wp-image-1615\" srcset=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-2.png 601w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-2-300x86.png 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/a><figcaption class=\"wp-element-caption\">Choose the Excel process to attach to<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-choosing-the-right-process\">Choosing the right process<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If there are multiple Excel processes running check which process you want to attach to using the Windows Task Manager. If you are not sure which process to choose then a quick PyXLL function can tell you the process id. For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom pyxll import xl_func\nimport os \n\n@xl_func\ndef getpid():\n    # This will return the Excel process ID so you know which\n    # process to attach the debugger to\n    return os.getpid()\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-breakpoints-and-stepping-through-the-code\">Breakpoints and stepping through the code<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">After you\u2019ve selected the Excel process, PyCharm will start connecting. Once connected you will see the following in the debug console in PyCharm:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-3.png\"><img decoding=\"async\" width=\"1024\" height=\"669\" data-attachment-id=\"1616\" data-permalink=\"https:\/\/www.pyxll.com\/blog\/debugging-your-python-excel-add-in\/image-3-5\/\" data-orig-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-3.png\" data-orig-size=\"1644,1074\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-3\" data-image-description=\"&lt;p&gt;Debug Python code in Excel with PyCharm and PyXLL&lt;\/p&gt;\n\" data-image-caption=\"\" data-large-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-3-1024x669.png\" src=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-3-1024x669.png\" alt=\"\" class=\"wp-image-1616\" srcset=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-3-1024x669.png 1024w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-3-300x196.png 300w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-3-768x502.png 768w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-3-1536x1003.png 1536w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-3.png 1644w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">With the debugger connected you can now set breakpoints. When you run your functions in Excel it will stop on those breakpoints, allowing you to step through your code in the usual way.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-common-problems-attaching-to-excel\">Common Problems Attaching to Excel<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you have any trouble in attaching the debugger (or if PyCharm shows a timeout error when attempting to connect) check the following:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>You do not have any of the packages &#8220;pydevd_pycharm&#8221;, &#8220;pydevd&#8221;, &#8220;debugpy&#8221;, or &#8220;ptvsd&#8221; installed. These will interfer with the operation of PyCharm\u2019s Attach To Process feature unless they are exactly the right version; it is better to not have them installed at all and to let PyCharm use it\u2019s own version of pydevd.<br><br>To uninstall these packages run:<br><code>pip uninstall pydevd_pycharm pydevd debugpy ptvsd<\/code><br><br>Check they have been removed by looking in your \u201csite-packages\u201d folder.<br>\u00a0<\/li>\n\n\n\n<li>You are using the latest version of PyCharm. PyCharm 2022.2 (Professional Edition) was used while writing this post.<br>\u00a0<\/li>\n\n\n\n<li>Look in your PyXLL log file for any errors.<\/li>\n<\/ol>\n\n\n\n<p class=\"has-background wp-block-paragraph\" style=\"background-color:#fcdf0091\">For more information about attaching to a process see the PyCharm docs:<br><a href=\"https:\/\/www.jetbrains.com\/help\/pycharm\/attaching-to-local-process.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.jetbrains.com\/help\/pycharm\/attaching-to-local-process.html<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-another-older-way-to-attach-pycharm-to-excel\">Another (older) way to attach PyCharm to Excel<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Prior to PyCharm&#8217;s &#8220;Attach to Process&#8221; feature there was another way to attach PyCharm to Excel. PyCharm is also capable to attaching to a <em>remote processs<\/em>, even one running on another computer. You can use this to attach to Excel running locally as well, though it takes a little more work than the method described above.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>This method is no longer the recommend method. It is left here for anyone unable to upgrade to a newer version of PyCharm.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The method of debugging Excel using PyCharm&#8217;s &#8220;Remote Debug&#8221; feature is explained in this video:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/GOnOb-4wb3w?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox\"><\/iframe><\/span>\n<\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-creating-a-new-debug-configuration\">Creating a new Debug Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To begin with you first need to set up a new Debug Configuration in PyCharm so that you can connect to PyCharm and debug your code. Go to &#8220;Add Configuration&#8221; or &#8220;Edit Configurations&#8221; on the drop down list of configurations next to the Run and Debug buttons at the top right of your PyCharm window. Click the Plus button to add a new configuration and select &#8216;Python Debug Server&#8217; (or &#8216;Python Remove Debug&#8217; in older versions of PyCharm) from the list of templates.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"746\" data-attachment-id=\"1438\" data-permalink=\"https:\/\/www.pyxll.com\/blog\/debugging-your-python-excel-add-in\/image-2-2\/\" data-orig-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2020\/11\/image-2.png\" data-orig-size=\"1265,921\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2020\/11\/image-2-1024x746.png\" src=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2020\/11\/image-2-1024x746.png\" alt=\"\" class=\"wp-image-1438\" srcset=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2020\/11\/image-2-1024x746.png 1024w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2020\/11\/image-2-300x218.png 300w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2020\/11\/image-2-768x559.png 768w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2020\/11\/image-2.png 1265w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Give your new configuration a name and set the port to 5000. This port is used to connect the PyCharm debugger to the Python code running in Excel. Don&#8217;t close this window just yet. Take a note of the string in point 1. on this screen (&#8220;pip install pydevd-pycharm~=&lt;version&gt;&#8221;) as we will need that in a second.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-installing-the-pydevd-pycharm-package\">Installing the pydevd-pycharm package<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next start a Python prompt using the same Python environment that you are using for PyXLL. If you are using a virtual env or a conda env you should activate that. Install the &#8220;pydevd-pycharm&#8221; package with pip using the command shown in the previous step. You must use the exact same string to ensure the correct version of the package is installed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are using an old version of PyCharm or you cannot use pip for any reason, you can find the pydevd-pycharm.egg file (or pycharm-debug.egg, depending on your version of PyCharm) in the &#8220;debug-eggs&#8221; folder in your PyCharm installation. To use these egg files add them to the pythonpath setting in your pyxll.cfg file, for example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;PYTHON]\npythonpath =\n    C:\\Program Files\\JetBrains\\PyCharm 2018.2.4\\debug-eggs\\pycharm-debug.egg\n...\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Depending on the version of PyCharm you are using you may find there are two eggs, one for Python 2.x and one for Python 3.x. If there are two, make sure you use the correct one. Recent versions of PyCharm just have a single egg to be used for all versions of Python. If you have any other versions of pydevd or pycharm-debug installed you may have to uninstall them so they don&#8217;t conflict (check your lib\\site-packages folder in your Python environment).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once you have install pydevd-pycharm using pip, or you have added the egg to your pythonpath, and you have the Python Debug Server configuration added to PyCharm you are ready to connect Excel to PyCharm.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-setting-up-excel-to-connect-to-pycharm\">Setting up Excel to connect to PyCharm<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">As of PyXLL 5, the easiest way to debug PyXLL add-ins using PyCharm is to install the &#8220;pyxll-pycharm&#8221; package by running &#8220;pip install pyxll-pycharm&#8221;.  After this package is installed, restart Excel. You will now have a &#8220;PyCharm Debug&#8221; button in the PyXLL ribbon tab. Before you press the &#8220;PyCharm Debug&#8221; button in Excel, start the Python Debug Server in PyCharm by selecting the new Debug Configuration you created earlier and press the green Debug button. See <a href=\"https:\/\/pypi.org\/project\/pyxll-pycharm\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/pypi.org\/project\/pyxll-pycharm<\/a> for further details.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" width=\"563\" height=\"399\" data-attachment-id=\"1437\" data-permalink=\"https:\/\/www.pyxll.com\/blog\/debugging-your-python-excel-add-in\/image-1-2\/\" data-orig-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2020\/11\/image-1.png\" data-orig-size=\"563,399\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2020\/11\/image-1.png\" src=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2020\/11\/image-1.png\" alt=\"\" class=\"wp-image-1437\" style=\"width:422px;height:299px\" srcset=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2020\/11\/image-1.png 563w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2020\/11\/image-1-300x213.png 300w\" sizes=\"(max-width: 563px) 100vw, 563px\" \/><figcaption class=\"wp-element-caption\">The &#8220;pyxll-pycharm&#8221; package adds the &#8220;PyCharm Debug&#8221; button to the ribbon.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If you are using a version of PyXLL prior to version 5 you will need to add some code to your PyXLL add-in to connect Excel to PyCharm. To connect to the Python Debug Server you must call &#8220;pydevd.settrace&#8221; with the host and port to connect to. You can use a PyXLL menu function to do this, as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom pyxll import xl_menu\nimport pydevd\nimport sys\n\n@xl_menu(&quot;Connect to PyCharm&quot;)\ndef connect_to_pycharm():\n    pydevd.settrace(&#039;localhost&#039;,\n                    port=5000,\n                    suspend=True,\n                    stdoutToServer=True,\n                    stderrToServer=True)\n\n    # reset excepthook to the default to avoid a PyCharm bug\n    sys.excepthook = sys.__excepthook__\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">If you used a port number other than 5000 when creating your run configuration you should change the port in the above code too.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that when the code above is executed, it will pause the execution of your program until you continue in PyCharm. If instead you want to allow the code to continue once the connection is made pass the option &#8216;suspend=False&#8217; to settrace.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is a bug in recent versions of PyCharm where an uncaught Exception will cause the debugger to detach and Excel to terminate. The line that sets &#8220;sys.excepthook&#8221; in the above code is a workaround for that problem.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After adding this code to your PyXLL modules, when you reload PyXLL you will now see a new menu item in the PyXLL menu in the Add-ins tab:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"519\" height=\"311\" data-attachment-id=\"434\" data-permalink=\"https:\/\/www.pyxll.com\/blog\/debugging-your-python-excel-add-in\/connect-to-pycharm\/\" data-orig-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2018\/07\/connect-to-pycharm.png\" data-orig-size=\"519,311\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"connect-to-pycharm\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2018\/07\/connect-to-pycharm.png\" src=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2018\/07\/connect-to-pycharm.png\" alt=\"\" class=\"wp-image-434\" srcset=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2018\/07\/connect-to-pycharm.png 519w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2018\/07\/connect-to-pycharm-300x180.png 300w\" sizes=\"(max-width: 519px) 100vw, 519px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-connecting-the-remote-debugger-to-python-in-excel\">Connecting the Remote Debugger to Python in Excel<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before you select &#8220;Connect to PyCharm&#8221; in Excel, run the &#8220;Python Debug Server&#8221; in PyCharm. Do that by selecting the Debug Configuration you made earlier and pressing the green Debug button in PyCharm. Once that&#8217;s running you can select this &#8220;Connect to PyCharm&#8221; menu item and it will attempt to connect to the PyCharm debugger.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once PyCharm is connected you can set break points in the IDE and it will stop when reaching those lines. You can then inspect variables and step though the code line by line.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"954\" height=\"686\" data-attachment-id=\"436\" data-permalink=\"https:\/\/www.pyxll.com\/blog\/debugging-your-python-excel-add-in\/pycharm-breakpoint\/\" data-orig-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2018\/07\/pycharm-breakpoint.png\" data-orig-size=\"954,686\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"pycharm-breakpoint\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2018\/07\/pycharm-breakpoint.png\" src=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2018\/07\/pycharm-breakpoint.png\" alt=\"\" class=\"wp-image-436\" srcset=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2018\/07\/pycharm-breakpoint.png 954w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2018\/07\/pycharm-breakpoint-300x216.png 300w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2018\/07\/pycharm-breakpoint-768x552.png 768w\" sizes=\"(max-width: 954px) 100vw, 954px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Warning:<\/strong> If you get an error in PyCharm about path mappings not existing in your project with the path containing odd escaped characters, check the encoding of your pyxll.cfg file is ANSI or UTF-8. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the &#8216;Resume Program&#8217; button in the debug window in PyCharm to continue after hitting a breakpoint. If you press the &#8216;stop&#8217; button it will disconnect your Python code from the debugger.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For more information about remote debugging with PyCharm, see <a rel=\"noopener noreferrer\" href=\"https:\/\/www.jetbrains.com\/help\/pycharm\/remote-debugging.html\" target=\"_blank\">https:\/\/www.jetbrains.com\/help\/pycharm\/remote-debugging.html<\/a>.<\/p>\n\n\n\n<p class=\"has-background wp-block-paragraph\" style=\"background-color:#fcdf0091\"><strong>TIP: <\/strong>Rather than write these menu functions yourself you can pip install &#8220;pyxll-pycharm&#8221; and have a &#8220;PyCharm Debug&#8221; button added to PyXLL ribbon toolbar automatically. See <a href=\"https:\/\/pypi.org\/project\/pyxll-pycharm\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/pypi.org\/project\/pyxll-pycharm<\/a>. This requires you to be using PyXLL 5 or later.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is PyCharm? PyCharm is an extremely popular Python IDE by JetBrains. Among it&#8217;s many features is its excellent visual debugger. The debugger lets you step through Python code, line by line, and inspect variables. We can use this to debug our Python code running in Excel. For professional developers getting a development environment set<\/p>\n","protected":false},"author":1,"featured_media":1616,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[658],"tags":[657,6,3,8],"class_list":["post-1618","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-debugging","tag-debug","tag-excel","tag-python","tag-pyxll"],"acf":[],"jetpack_featured_media_url":"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2022\/08\/image-3.png","jetpack_shortlink":"https:\/\/wp.me\/p7l3LP-q6","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/posts\/1618","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/comments?post=1618"}],"version-history":[{"count":1,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/posts\/1618\/revisions"}],"predecessor-version":[{"id":2025,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/posts\/1618\/revisions\/2025"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/media\/1616"}],"wp:attachment":[{"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/media?parent=1618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/categories?post=1618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/tags?post=1618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}