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