Python script - keep terminal console open (solved)

Kicad 5.1.9 on Windows 10
I am trying to write a python script to convert the netlist to an unsupported format (Intergraph)
Something doe not work right and I need to debug it.
The problem is that when running the script from within Kicad the terminal screen “blips” and closes immediately.
Is there a way to keep it open?

Another thing - what exactly does Kicad pass to the script as the “%I” and “%o” arguments?

I haven’t used Python in KiCad yet, but what happens if you just add a line in your Python script to wait for a key press, or add a delay of a few seconds before it exits?
When you’re finished debugging you can delete that again of course.

I think you are talking about eeschema bom dialog. It’s not a real terminal, it’s not designed to stay open. You can run exact same command from normal terminal though. %I is path to the xml file that eeschema generates and %o is output file name, i.e. project name.

Unfortunately this is not the case - even if you put an input() command, the screen disappears.

Yes, this is the eeschema bom dialog.
The thing is that my script works ok if I run it from a terminal and provide the arguments manually, but does not work if I place the same command in the dialog with the %I and %O as arguments

Use the universal debug method: print statements. Add some logging to your script to figure out where things go wrong.

As said -does not help to print anything since the screen never stays open.

Print to a text file.

Well, this is actually the only thing that this script does - reads the XML file and writes to a text file.

If inside a windows command terminal I go to the Kicad project directory and run
python path-to-python-script.py project.xml project.out
then everything works - the project XML file is read and the netlist output file written
But if inside the Kicad netlist dialog I put
python “path-to-paython-script.py” “%I” “%O”
I get the dialog for selecting the output file, but then it does not work - the output file is not generated

This is what I am trying to debug and can not figure how

Maybe write debug data to a second text file, or push it to a socket or serial port?
You can use netcat telnet or putty to listen to a port and then send debug data to it.

When you run it from kicad, do you get a file out without an extension? i.e. a file called project with no file extension?

No. No file at all.
According to the timestamp I see that Kicad creates the XML file but other than that, no indication that something happened

Another file is an option I think I will try

Wait, why did it ask for an output file when you provided it on the command line?

Check whether you are supposed to quote arguments.

First thing in your script open another file and echo the arguments to it.

seems to be something “built in” the dialog - it asks you for a file name (the output file name) whether you put the %0 argument or not. I assume that it you pu the %O argument it puts the name of the file that you selected into this argument to pass it to the script

Good idea - did it and now it starts making sense.

  1. I though that the argument passed is the file name only and I added the path to it in my code - but the arguments appear as full path, so my addition of the path corrupted everything
  2. Odd, but the input file path is given in Windows format () while the output file path is given in Linux format (/), so I guess my script is able to open the XML file for reading but tries to create an invalid file path for the output file.
    I’ll continue from this point to try and solve the problem

Windows understands both forward and backslash as path separator.

One more thing. python run from eeschema launches kicad’s bundled pyton binary that is in kicad’s bin directory. python run from terminal is most likely your system python which may have different sets of libs or be a different version altogether (py3 vs py2).

Thanks, was not aware of this
Indeed if I go into kicad\bin and execute the bundled python I see that it is version 2.7 and I get an error for my script.

You can solve that quickly by just specifying full path to python.exe in eeschema launch command.

Obviously if you want other kicad users to use your script then it’s better to fix the script to be py2 compatible.

Thank you and all that gave their advice.
I just changed it to work with 2.7 that comes with Kicad - script now runs perfectly.

1 Like