|
| 1 | +## Debugging code with ExcelPython |
| 2 | + |
| 3 | +ExcelPython does not have any special provision for debugging Python code because it is fully compatible with existing Python debugging tools. Here we will see how to set up a couple of the most widely-used ones, [Python Tools for Visual Studio](#debugging-with-python-tools-for-visual-studio) and [PyDev](#debugging-with-eclipse-and-pydev). |
| 4 | + |
| 5 | +## Debugging with Python Tools for Visual Studio |
| 6 | + |
| 7 | +On Windows it is possible to debug Python scripts in Microsoft Visual Studio using an add-in called [Python Tools for Visual Studio](http://pytools.codeplex.com/) (PTVS). To get set up (full instructions can be found on the PTVS website) you need to |
| 8 | + |
| 9 | +1. Install a recent version of Visual Studio, either Express or a commercial license. |
| 10 | +2. Download and install the relevant distribution of the PTVS add-in |
| 11 | +3. Install the PTVS debug package into your Python distribution |
| 12 | +4. Prepare your Python script to enable debugging in Visual Studio. |
| 13 | + |
| 14 | +Assuming you've done steps 1 and 2, step 3 can be achieved using [PIP](https://pypi.python.org/pypi/pip). |
| 15 | + |
| 16 | +``` |
| 17 | +C:\>pip install ptvsd |
| 18 | +Downloading/unpacking ptvsd |
| 19 | + Running setup.py (path:c:\docume~1\user\locals~1\temp\pip_build_user\ptvsd\setup.py) egg_info for package ptvsd |
| 20 | +
|
| 21 | +Installing collected packages: ptvsd |
| 22 | + Running setup.py install for ptvsd |
| 23 | +
|
| 24 | +Successfully installed ptvsd |
| 25 | +Cleaning up... |
| 26 | +``` |
| 27 | + |
| 28 | +Now you should be able to `import ptvsd` in your Python interpreter. |
| 29 | + |
| 30 | +Let's see how to prepare a [basic script](./Addin01.md) for debugging with PTVS |
| 31 | + |
| 32 | +```python |
| 33 | +# Book1.py |
| 34 | +from xlpython import * |
| 35 | + |
| 36 | +# Add these two lines |
| 37 | +import ptvsd |
| 38 | +ptvsd.enable_attach(secret='cows') |
| 39 | + |
| 40 | +@xlfunc |
| 41 | +def DoubleSum(x, y): |
| 42 | + return 2 * (x + y) |
| 43 | +``` |
| 44 | + |
| 45 | +These to lines enable us to attach the Visual Studio debugger to the Python script, so that we can debug it. Note that the Python process itself must be running for this to work, and ExcelPython does not launch it until it is needed - so just to make sure that it is running and that the `Book1.py` script is loaded, click 'Import Python UDFs'. |
| 46 | + |
| 47 | +At this point you may need to unblock a port on the Windows firewall: |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +Having done this you should now be in a position to attach the Visual Studio debugger to the Python script. To do this: |
| 52 | +* open the Debug > Attach to Process dialog |
| 53 | +* select 'Python remote debugging' as the transport |
| 54 | +* specify the relevant qualifier in the 'Qualifier' box, for example `cows@localhost` |
| 55 | +* click refresh and select the Python process |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +Now you can set breakpoints in the code and the next time your function is invoked from Excel, the execution will be interrupted when the breakpoint is hit. |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +## Debugging with Eclipse and PyDev |
| 64 | + |
| 65 | +Coming soon! |
0 commit comments