Stepping through Python code called from Excel

Setting up your editor to allow Python code debugging for functions called from Excel can be tricky, depending on the details of your editor and linking software, but using the Visual Studio Code Editor and Pyxll, this works for me:

  1. Open the Python code in the Visual Studio Code editor.
  2. Make sure the Python code Extension is installed (check under File-Preferences-Extensions).
  3. Open the folder containing the code file.  Either use File-Open Folder …, or click on the Explorer icon (top icon on the left hand icon bar).
  4. Go to the Excel File that will be calling the Python functions, and link to the editor from the Add-ins – Pyxll – Attach to VS Code menu:
  5. Return to Visual Studio Code and click the Run icon in the left hand tool bar …
  6. … then click the green arrow between Run and Python Attach.  The blue bar at the bottom of the screen should turn orange:
  7. Finally click in the left hand margin on a line where you like to insert a break point …
  8. … and return to Excel and call the routine to be de-bugged.

The code will stop at the selected break point.  You can then step through the code using the Run menu, use the function key short-cuts, or right click for debug options.

This entry was posted in Computing - general, Excel, Link to Python, PyXLL, UDFs and tagged , , , , . Bookmark the permalink.

4 Responses to Stepping through Python code called from Excel

  1. pyxll says:

    See for full instructions, including how to add the “Attach to VS Code” menu item in Excel (it is not included by default).


  2. Suelio Josileissu says:

    Are you changing your main language to Python or will be improvements vba codes??


    • dougaj4 says:

      I am working on moving the most generally useful applications to Python, but there will still be VBA related posts (there were two last month for instance).


      • Suelio Josileissu says:

        maybe we can integrated with freeCAD your codes and build an complete software for the community?
        I will spend my time to follow your blog with new codes in python so now. I just has a few UDFs in VBA can help me, like import dxf points and frames….


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.