How to debug python scripts in Python

Hello,

I’ve been using KiCad for around 5 Years now and strated with python programming recently and I’d like so develop some plugins for kicad that feel like they just have to be a part or the software by default so I’d like to start with net_list editor and reader (manipulating schematic components and netlist) from there move to PCBNew plugins and I guess finish at GUI programming with wx (or QtGUI if possible) so

I’d like to know where is the usual place kicad developers debug their scripts and how to run them on a particular kicad project in terms of dependencies and stuff

It’d also be lovely if I could get some links and suggestions to dated python scripting guides both for schematic, PCB, and the wx GUI programming( seen mmccoo web and it’s outdated other than the youtube videos)

Schematics are not a good place to start at right now. Eeschema has no python API yet and upcoming v6 will switch to a completely new format so whatever you do now will be outdated pretty soon.

I suggest starting with pcbnew scripting. It has an official (albeit not very stable) API and lots of existing high quality plugins to look for inspiration.

In general check out some existing work here https://github.com/xesscorp/kicad-3rd-party-tools
These deserve special mention:
https://github.com/easyw/RF-tools-KiCAD
https://github.com/MitjaNemec/Kicad_action_plugins
Mitja’s plugins also work with schematics and you can see how unwieldy it is currently, but can be done.

The way I debug my plugin is through usual IDE tools. I use both vscode and Intellij IDEA (with pycharm plugin) for python development. I wrote code in such a way that it has 2 entry points, from CLI interface and from pcbnew action plugin interface. When I need to debug I always use CLI interface. Another universally useful technique is “printf debugging”, or simply put adding logging statements to verify code execution paths and state. You can view them in stdout or in scripting console in pcbnew.

In terms of GUI programming, if you are going to write kicad plugins and not external tools, it’s best to stick to wxPython (py bindings for wxWidgets). Because that’s what is guaranteed to be available and the less dependencies you have for your plugin, the better. Luckily wx has a decent GUI designer (wxFormBuilder) and a sane event model so it’s not hard to learn.

3 Likes

So Yeah I’m actually using pycharm for my dev and I’ve found a pretty good starting point with BOM generator scripts which contain all the elements I need (excluding .sch net list GroupField set function) with the library kicad_netlist_reader and my only problem now is how do I see print() debug output and how do I run the script on a schematic to get and set elements there.

Also I’m just curios about when KiCad 6 is about to launch? I’m currently running a nightly version from 3 days ago…

Output should show up in stdout. Pycharm captures it if you run your script from ide.
To “run script on a schematic” you can pass schematic file path as argument to your script for example. Or if you have a gui you can open a standard “open file” dialog and let user choose the file.

There is no set date yet. Wayne (kicad project lead) mentioned few months ago that first release will hopefully be out by January. Official release can be 3-6 months after that (just my guess).

1 Like

How do you pass the schematic file path if you run it from CMD?
running the BOM script just as a start guide reference from within the kicad project directory with
"python my_bom_to_csv_by_value.py “%I” “%O.csv” "
Doesn’t work and gives an error about the %I parameter what do these two translate into?

Also installed the library kicad_netlist_reader on my global python path temporary
but my guess is that this is wrong so how do you run that script from pycharm and include all KiCad’s libraries paths with it?

%I and %O are variables that eeschema substitutes when running the bom script.
%I corresponds to xml file that eeschema generates and %O to the output file name which is same as schematic file name without extension.
So gen an equivalent command line you have to first run eeschema bom once with any script just to get an xml file and then launch your script like this:

python my_bom_script.py <path_to_xml> <output_name>.csv

This depends on platform. On linux Kicad uses system interpreter so what you did should work and also pycharm should see kicad’s libraries (assuming you have the system interpreter chosen for the project).

On Windows and Mac kicad ships it’s own python and you have to setup that python as interpreter for your project and/or module in pycharm.

1 Like

OK thank you very much just somethings that are still not clear for me and that xml file eeschema generates what does it contain? it there a way to view and access it?

Also to setup the kicad interperter in pycharm on windows, How am I suppose to know what dir is the correct one? when I do print(sys.path) in pcbnew terminal it outputs me this:

'C:\\Program Files (x86)\\KiCad\\lib\\python27.zip',

'C:\\Program Files (x86)\\KiCad\\lib\\python2.7', 'C:\\Program Files (x86)\\KiCad\\lib\\python2.7\\plat-win32', 'C:\\Program Files (x86)\\KiCad\\lib\\python2.7\\lib-tk', 'C:\\Program Files (x86)\\KiCad\\lib\\python2.7\\lib-old', 'C:\\Program Files (x86)\\KiCad\\lib\\python2.7\\lib-dynload', 'D:\\msys64\\mingw32', 'C:\\Program Files (x86)\\KiCad\\lib\\python2.7\\site-packages', 'C:\\Program Files (x86)\\KiCad\\lib\\python2.7\\site-packages\\wx-3.0-msw', '.', 'C:\\Program Files (x86)\\KiCad\\share\\kicad\\scripting', 'C:\\Program Files (x86)\\KiCad\\share\\kicad\\scripting\\plugins', u'C:\\Users\\user\\AppData\\Roaming\\kicad\\5.99\\scripting', u'C:\\Users\\user\\AppData\\Roaming\\kicad\\5.99\\scripting\\plugins'

Also do you where can I read more about how the internals of each kicad tool works?

It is created in the project directory, same place where your schematic is.
It’s a plain xml file you can view it with any text editor. It contains netlist and component information.

To setup interpreter in intellij idea (i’m assuming pycharm is similar) go to project settings and in SDKs section create a new one with “Add Python SDK…”
image
Then choose “system interpreter” and provide path to C:\Program Files\KiCad\bin\python.exe or wherever you kicad is installed. Pycharm should pick up the site-packages and all libs automatically.
Then in module settings set interpreter to the newly created one.

image
I have named it kicad nightly to differentiate because I have a few setup this way.

Then when you setup a run configuration make sure that project default interpreter is used.

2 Likes

So that means you can’t run python 3+ libraries with kicad?

I’m having a bunch of issues with running a simple script

from __future__ import print_function

# import os
# import digikey
# from digikey.v3.productinformation import KeywordSearchRequest



# Import the KiCad python helper module and the csv formatter
import kicad_netlist_reader
import csv
import sys

def myEqu(self, other):
    """myEqu is a more advanced equivalence function for components which is
    used by component grouping. Normal operation is to group components based
    on their value and footprint.

    In this example of a custom equivalency operator we compare the
    value, the part name and the footprint.
    """
    result = True
    if self.getValue() != other.getValue():
        result = False
    elif self.getPartName() != other.getPartName():
        result = False
    elif self.getFootprint() != other.getFootprint():
        result = False

    return result

# Override the component equivalence operator - it is important to do this
# before loading the netlist, otherwise all components will have the original
# equivalency operator.
kicad_netlist_reader.comp.__eq__ = myEqu

net = kicad_netlist_reader.netlist('C:/My_Cloud/KiCad/Playground/M600DJIToPixhawlPDB/M600DJIToPixhawlPDB.xml')

print(net)

and it give’s me the following error after running it:

Traceback (most recent call last):
  File "C:/Users/User/AppData/Roaming/kicad/scripting/plugins/Digikey_plugins/Digikey_parser.py", line 11, in <module>
    import kicad_netlist_reader
  File "C:\Program Files (x86)\KiCad\lib\python2.7\site-packages/kicad_netlist_reader/__init__.py", line 5, in <module>
    from .kicad_netlist_reader import netlist, comp, xmlElement, libpart
  File "C:\Program Files (x86)\KiCad\lib\python2.7\site-packages/kicad_netlist_reader/kicad_netlist_reader.py", line 704
    print( __file__, ":", e, file=sys.stderr )
                                 ^
SyntaxError: invalid syntax

Any clue?

If you don’t use any of kicad’s own libraries then you can use your own interpreter and it can be python 3. But if you want to import pcbnew module you have to use kicad’s shipped interpreter which on windows is 2.7 for now.

You get syntax error because kicad_netlist_reader library you are using is written for python3.

If you switch to python3 in your code you will have to give eeschema full path to your own python interpreter in the bom command line as well, otherwise it will try to run it with kicad’s python 2.7.

1 Like

There is an option to get a windows python3 build. I got one a while back (so the pcbnew python API has probably changed since then). If you need a python3 win build try contacting nick_oe. But be aware that he is a part of the dev team and he might not have time to service your needs.

Well this realy gets complicated yes I want to use this python 3 library but have absolutely no idea how to give eeschema full path to my interperter now I other than the kicad dev I use the default python interperter pycharm uses which is a folder

I don’t mind to start contributing to KiCad in exchange to some available articles that talk more about how KiCad internals work to be actually able to properly and smoothly develop tools and plugins.

You can just download and install official python3 installer from https://www.python.org/downloads/windows/

Then in eeschema instead of
python my_bom_to_csv_by_value.py “%I” “%O.csv”

you would have a command line like
c:\python3\python.exe my_bom_to_csv_by_value.py “%I” “%O.csv”

1 Like

IIRC that build had only scripting support but not plugin support because it doesnt have working wxpython.

Yup, it had/has only scripting support, lacking plugin support. But for me it was an a viable way to test my plugin backend (business logic) with python 3.

If you are on windows 10 you can install kicad in wsl and test python3 build that way.

Thanks for the reply very educating even besides KiCad and I just downloaded the latest Python 3.9(BTW my python installs @ C:\Users\user\AppData\Local\Programs\Python\Python39) and command you said you use does work but now when I use this drawPCB script in the windows 10 CMD with this command:

pcbdraw MIC28515_Voltage_Regulator_Module.kicad_pcb test.jpg

it gives me this error:

 ** On entry to DGEBAL parameter number  3 had an illegal value
 ** On entry to DGEHRD  parameter number  2 had an illegal value
 ** On entry to DORGHR DORGQR parameter number  2 had an illegal value
 ** On entry to DHSEQR parameter number  4 had an illegal value
Traceback (most recent call last):
  File "c:\users\User\appdata\local\programs\python\python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\User\appdata\local\programs\python\python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\Scripts\pcbdraw.exe\__main__.py", line 4, in <module>
  File "c:\users\damn1\appdata\local\programs\python\python39\lib\site-packages\pcbdraw\pcbdraw.py", line 12, in <module>
    import numpy as np
  File "c:\users\User\appdata\local\programs\python\python39\lib\site-packages\numpy\__init__.py", line 305, in <module>
    _win_os_check()
  File "c:\users\User\appdata\local\programs\python\python39\lib\site-packages\numpy\__init__.py", line 302, in _win_os_check
    raise RuntimeError(msg.format(__file__)) from None
RuntimeError: The current Numpy installation ('c:\\users\\User\\appdata\\local\\programs\\python\\python39\\lib\\site-packages\\numpy\\__init__.py') fails to pass a sanity check due to a bug in the windows runtime. See this issue for more information: https://tinyurl.com/y3dm3h86

Is it due to a new version of python used or what?
Also I’d like to learn more about how those cmd command work to be able to understand and troubleshoot command problems say what python interpreter does that pcbdraw command use and how to know what defines the execution of the command python in windows or if similar on Ubuntu as well?

That error message has a reference to windows runtime lib bug.
Workaround is to install specific numpy version

pip uninstall numpy

pip install numpy==1.19.3

I’m not sure I follow. In general python command line is in the form of

<path to python interpreter binary> <script/module to execute> <arguments for script/module>

That’s all there is to it.
Maybe you can be more specific?

1 Like

First thanks that it seems to fix that particular problem

say I write the command:
pcbdraw MIC28515_Voltage_Regulator_Module.kicad_pcb test.jpg

in CMD how what sets how the “pcbdraw” command is being treated by the command line?
for example I know that when I execute:
pcbdraw MIC28515_Voltage_Regulator_Module.kicad_pcb test.jpg

from picking at that error output it looks like it runs with python 3.9 but how would I “debug” it without the printed output? and say I want to run it with another python interperter how’d I do that?

Just would like to educate myself on how cmd command are processed and executed after enter like but no idea how that topic is called sins just “cmd” shows many other unrelated results and no one explains in depth how it works but just on how to use it.

My knowledge of Python is very limited.
I do know there is a:

#import pdb # Python Debugger.

I have not used it myself though and do not know how this works.