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

Windows Azure Storage Emulator failed to install

CodeProject Windows Azure Storage Emulator failed to install When attempting to install a new version of the Azure Storage Emulator either as a separate installation package or automatically as part of an Azure SDK update, you may run into an error message which states the storage emulator has failed to install. This can occur using the Web Platform Installer (WebPI), NuGet Package Manager or when performing the install manually. Below is the message received using the WebPI.   Storage Emulator Background  (optional reading) The windows azure storage emulator executable lives under the Microsoft SDKs directory as shown below: Configuration If we take a quick look inside the WAStorageEmulator.exe.config file we can see each of the storage services pointing to local service endpoints. <StorageEmulatorConfig>     <services>       <service name=" Blob " url=""/>       <service

Test connection to remote SQL Server Database from an Application server

While aiming to test whether a SQL connection is succeeding between an Application server and a remote Database Server, it is often not possible to install SQL Server Management Studio (SSMS) or Microsoft Command Line Utilities (MsSqlCmdLnUtils) due to the locked down nature of the targets, particularly in test and production environments. A lightweight approach that worked for me recently, makes use of components that have been a part of windows boxes for a long time, albeit different levels of database driver support as the components have evolved, the Microsoft Data Access Components (MDAC). MDAC provide a Universal Data Link, which can be configured using a common user interface for specifying connection properties as well as testing the connection.  Data Link properties dialog box At Assemblysoft we specialise in  Custom Software Development  tailored to your requirements. We have experience creating Booking solutions, as we did for HappyCamper