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.
 
Optimisation
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.


Conclusion

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 blazor.net and azure blog here carlrandall.net


Links






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="http://127.0.0.1:10000/"/>       <service

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