PCBNEW: Using Python scripting under Windows

So I am running BZR 6086 under Windows 7. I have a working scripting console which I can type commands into and test things out. However, I can’t seem to figure out where to add my own scripts so that I can import them into the console’s interpreter.

For example, I have a KiCad project (.pro) with a PCBNEW (.kicad_pcb) file in my home directory. I open the KiCad project viewer and then from there open the PCBNEW file. Here is the python load path right after the scripting console is opened:

Welcome To PyCrust 0.9.8 - KiCAD Python Shell
Python 2.7.10 (default, Jul  8 2015, 15:10:39) 
[GCC 5.1.0] on win32
Type "help", "copyright", "credits" or "license" for more information.
import sys
sys.path
['C:\\Program Files\\KiCad\\lib\\python27.zip', 'C:\\Program Files\\KiCad\\lib\\python2.7', 'C:\\Program Files\\KiCad\\lib\\python2.7\\plat-win32', 'C:\\Program Files\\KiCad\\lib\\python2.7\\lib-tk', 'C:\\Program Files\\KiCad\\lib\\python2.7\\lib-old', 'C:\\Program Files\\KiCad\\lib\\python2.7\\lib-dynload', 'C:\\building\\msys32\\mingw64', 'C:\\Program Files\\KiCad\\lib\\python2.7\\site-packages', 'C:\\Program Files\\KiCad\\lib\\python2.7\\site-packages\\wx-3.0-msw', '.']
import os
os.getcwd()
'C:\\Program Files\\KiCad'

As you can see, none of the provided directories are in user-accessible areas. Even the current directory is set oddly in that I would expect it to be the directory of the PCBNEW (*.kicad_pcb) file. Thus in order to use my own scripts, I am forced to manually append a user directory to the python load path every time I want to use the scripting console.

Does anyone have any advice on how to improve this situation short of going into the source code and making my own version of PCBNEW?

FYI, since the scripting console can not be extended with my own scripts in this state AFAIK, I’ve mainly been executing my PCBNEW scripts outside of the scripting console via:

“C:/Program Files/KiCad/bin/python.exe” my_script.py

The script (my_script.py) will have a structure like this:

import pcbnew
board = pcbnew.LoadBoard("test.kicad_pcb")

# Do some operations on the board.

board.Save(board.GetFileName())

And then closing PCBNEW and reopening to see the effect. However, this is somewhat awkward, especially considering there is this nice scripting console that is going unused.

Can’t you put a script into …/KiCAD/bin/scripting/plugins that would add your easier accessible script folder?

For starting your scripts you have to type in ‘import script_xy’ anyway…
So having a script like ‘t.py’ which would append your folder to the path you’d just have to type ‘import t’ and it should then allow you to import your own scripts, no?

t.py:
sys.path.append(‘your accessible python script folder’)

Question for me would be if that can be automated?

I did try putting a script in KiCAD/bin/scripting/plugins but I still couldn’t import it which makes sense since KiCAD/bin/scripting/plugins is not on the load path. However, what does work is to put the script in ‘C:\Program Files\KiCad\lib\python2.7\site-packages’ but then I’m not sure what happens when you reinstall KiCad? However, for now it is a hacky solution.

Yeah, I had to restart KiCAD after I made that folder from scratch, they hadn’t been there for me.
Might even had to restart the pc, can’t remember.
Once I did that and opened the scripting console and put in ‘sys.path’ the newly created folder was added to that path last by itself… I didn’t do anything.
And then I did what I wrote above… a script in there that adds another path to sys.path with a test script in there…
which worked when I imported it.

@Joan_Sparky
is there a way to starting the scripts when pcbnew is launched?
I mean I would like to have already ‘import script_xy’ executed at pcbnew start, so I could launch e.g. a my_function() directly in pcbnew python console without having to type:

import script_xy

before

my_function()

thank you
Maurice

Hello,

I am using a typpical python installation on c:\python27 and a IDE to use it.

I know that pcbnew can be imported when you call a python script using the python within kicad installation.

Is it possible to import pcbnew as well in a normal python installation installing pcbnew as a normal library/module or so?

Thanks!

1 Like