Skip to main content

Debugging Python and Iron Python using Visual Studio

Now Python is a first class citizen since the release of Visual Studio 2017 and can be configured directly from the Installation IDE, below are a few settings worth bookmarking for your next python integration project.

Debugging Python

One of the first things you are going to want to do is step through your code when using Visual Studio, particularly as the language is dynamic and inspection of local and global scope soon becomes necessary. One thing to note is that if you start from a native python project, this is all wired up for you but if you are using .Net to call python modules or want to support an older python version, such as 2.7, you will soon see that breakpoints are not being hit due to symbols not being loaded.


Enable Just My Code

To distinguish user code from non-user code in .net, Just My Code looks at two things: PDB (Program Database) files, and Optimization
Program Database
A .pdb file, otherwise known as a symbol file, maps the identifiers that you create in source code for classes, methods, and other code to the identifiers that are used in the compiled executables of your project. The .pdb file also maps the statements in the source code to the execution instructions in the executables. The debugger uses this information to determine two key pieces of information:
·         Name of the source file and line number to be displayed in the Visual Studio IDE
·         Location in the executable to stop at when you set a breakpoint
A symbol file also contains the original location of the source files, and optionally, the location of a source server where the source files can be retrieved from.
A symbol file is created when you build with /debug
Note - Symbol files used to be referred as DBG files or debug files but have now been superseded by PDB files.
When optimization is turned off (the default setting for Debug builds) that also contributes to the code being considered "mine".  This enables or disables optimizations performed by the compiler to make your output file smaller, faster, and more efficient".  This ranges from the size of the assembly to the speed it can execute.  This is ideal for a release version which is shipped but not ideal for the version in active development or being post release debugged.
 For a C# project, this can be found in the properties on the Build tab as shown below

So in short, if you have the .pdb file and ‘optimisation’ is turned OFF, the code is classed as yours as you have all the mappings required to step through and stop at breakpoints alongside the actual code. This is precisely what you want when debugging code you have written and under normal circumstances you would want this option checked.

--However, with Iron Python where  but when being executed by

Debug -> Options -> Debugging -> Under the General Tab, Deselect the checkbox 'Enable Just My Code' as shown below

2) Set the PYTHONDEBUG Preprocessor Directive
Project -> Properties -> 

Debugging Python Libraries

If you want to step into the python standard library, there is a python debugging menu under python tools is Visual Studio as shown below

Debugging Iron Python

All the above apply also to Iron Python with the addition of another parameter that can be passed into the Iron Python constructor

Code Editor Indentation Setup

As Python functions have no explicit begin or end, it relies on the colon (:) and the indentation of the code itself. The indentation aspect of the language soon becomes fairly prominent.
One of the common issues with a python code editor is how indentation is configured. Generally, out of the box, python indentation is setup to use spaces. The majority of commercial projects I have been exposed lean towards tabs as the indentation marker of choice.
The net effect of having this setup incorrectly can be quite subtle, resulting in a runtime error. Visual Studio does highlight mismatched indentation of code with a squiggly line but it is easy to get caught out.

This can be modified by selecting the python editor settings, as shown below, selecting  'use tabs'
At the time of writing, this is not the default option.


Hopefully there is something here that will save you scratching your head and enable you to get on with solving your application logic rather than wasting effort on stuff that should just work

If you would like some hands on expertise for your business feel free to reach via my company assemblysoft or checkout some other musings via my and azure blog here


Popular posts from this blog

Azure Devops - Pull Request Merge Conflicts

Before a Git pull request can complete, any conflicts with the target branch must be resolved. Out of the box, at the time of writing this article, Azure DevOps requires this to be resolved locally. Following best practices to not allow direct commits to our release/master branches further exasperates the problem as we need to effectively clone the branch or go with a rebase approach, both of which break the natural flow of resolving the conflicts as part of the pull request. With this extension, from the Microsoft DevLabs team via the Marketplace, you can resolve these conflicts online, as part of the pull request process, instead of being forced to break flow and resolve locally. Online Experience After adding the extension the new conflicts tab is visible which enables conflict resolution in the familiar side by side review page as shown below: Really nice extension, which should make resolving merge conflicts a much more straightforward part of the DevOps

Azure DevOps Authorisation

Managing whether an identity has access to a given  service, feature, function, object, or method in Azure DevOps comes down to authorisation. Fortunately, by default, the DevOps permissions are set in such a way to enable you to focus on the job at hand, DevOps. Loosely translated this means 'don't get in my way'. My experience is that the Azure DevOps team have done a good job at this, enabling you to crack on developing, building, testing and releasing without much hindrance. Working with relaxed permissions is great when you are the owner and possibly either a one man band or small team but as soon as we need to consider larger teams, varying roles with approvals and degrees of access, authorisation becomes a real concern. I was recently involved in a project utilising offshore developers where trust was a concern and a number of specific teams handling specific roles needed to come together to approve a set of pipelines.  This article is a pick of findings a

Adding mobile support for MVC4 Web Applications

A few years ago, mobile web sites were more of an after-thought for many developers unless you were involved in developing apps specifically targeted for mobile devices. I was surprised to read on wiki that it was only March 2010 when Apple began taking pre-orders for the iPad. What a different landscape we have seen since. Desktops, Tablet and Mobile support are now a major design goal for any site that needs a broad reach and this is only set to increase.   This article will demonstrate how MVC4 , almost out of the box allows us to deliver views that cater for specific mobile devices and keep the code base shared as much as possible, creating mobile friendly web applications. Read complete article... Also checkout some other musings via my and azure blog here