Part 5: Get started with Python: Debugging in PTVS


This is a tutorial series which will teach you how to code in Python. We will start with the absolute basics of installation of Python in Windows and Python Tools in Visual Studio. We will then go through basic constructs in Python and write a couple of programs to summarize what we have learned. We will end with an Object Oriented Approach using Python and a specific feature of Python Tools in Visual Studio: Mixed mode C/C++/Python Debugging

Part 1: Get Started with Python summarized the steps involved to setup Python and Visual Studio for Python Development. We essentially learned how to install Python in Windows, Visual Studio and Python Tools for Visual Studio.

Part 2: Get Started with Python took you through the basics in programming constructs such as output, input, variables and control flow statements including conditional statements and loops including while and for. Using these tools was enough to get started with coding basic applications in Python.

Part 3: Get Started with Python: Functions and File Handling, as the topic suggests, dealt with functions and file handling in Python. We went through the ‘def’ construct used in function definition and file handling constructs for open. read and write. Essentially, by this topic we covered the basics of programming in Python.

Part 4: Get Started with Python: Object Oriented Approach gives an insight into the object oriented approach to programming in Python, primarily discussing objects and classes.

Part 6: Get started with Python: Build your first Django Application in PTVS

In Part 5, after having covered basic programming constructs in Python, let us look into a couple of new features of PTVS: Mixed Mode Debugging and Remote Debugging.

In most cases Python debuggers support only Python code and this was true even till PTVS 2.0. However, Python is often used in conjunction with C/C++ code which calls for the use of an integrated debugging experience. This also enables higher performance and the ability to call platform APIs. So let us see how to enable this debugging mode.

Step 1: First let us check for the dependencies:

  • You need to download Python Tools for Visual Studio 2.0 or above
  • You also need any Visual Studio version of 2012 or 2013.
  • Lastly not all versions of Python are supported – only CPython 2.7 and 3.3+ are supported

Step 2: Enable Native Code Debugging:

To use mixed mode debugging instead of pure Python, you need to right click your project and select your Project Properties. Once the project properties tab opens up, you need to click on “Debug” tab and click the check box “Enable native code debugging”.

image

Step 3: Now if you press F5 to debug/execute the code, you might get a pop up “Python Symbols required”. Download the symbols for your interpreter and extract them to your Python directory > Symbols folder. To register the above mentioned symbols, click on Tools > Options > Debugging > Symbols and add in the path to the list. More instructions related to this can be found here.

image

 

Another interesting debugging option available is Remote Debugging in Windows, Linux or OS X.

In order to Remote Debug, you will need the package called ‘ptsvd’. This is located in the Python tools installation folder. It has to be located over to a place where your script can find it (basically inside your project directory).

You can import it via the following snippet of code:

 1: import ptvsd
 2: ptvsd.enable_attach(None)

You can then transfer the files to the remote machine/location.

Now you need to attach to it via Visual Studio. To do so, go to Debug > Attach to a Process and change the Transport to “Python remote debugging (unsecured)”. Type in the host name of the machine you want to debug it on and press Enter. You will see the process list and you can click attach. As you can see debugging has started and you can run the script till you hit the breakpoint. The debugging experience is the exact same one.

image

Cross-Platform Remote Debugging explains this in greater detail.

That covers two debugging capabilities of Python Tools in Visual Studio. With that, we covered basics of programming using PTVS. In the next and final of the series, I will summarize all that we learned and demo a simple Django project in PTVS. Stay tuned!

Comments (4)

  1. TamerAfify says:

    Thanks for the detailed blog. It is of great help.
    what is the “the Python tools installation folder” ? PTVS msi doesn’t reveal that during installation,

    1. TamerAfify says:

      Found it. The PTVSD folder is here
      C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.2

  2. TamerAfify says:

    When remote debugging,
    Is there a ptvsd package which I can install in my project virtual env.? or do I have to include the code with my project?
    Also is that requirement an addition to having msvsmon.exe running on remote machine? for VS to connect to application host machine

    you have a typo in “In order to Remote Debug, you will need the package called ‘ptsvd’”

  3. TamerAfify says:

    After Importing this package PTVSD, and just running the project locally, I wasn’t able to break in python code. Is that by design? That method (of application code intervention) is not suitable for my usage as I create – and publish – an executable from my python project and I need to create one exe to attach locally to process (and debug python) or attach remotely.
    Is there another way to establish remote debugging?

Skip to main content