Hi all,
I try to code customized Python script for generating position file for PnP machine.
My goal is to filter components with regard to custom “Field” information to get just set of parts for given version of PCB and to get rid of parts which shall not be populated.
So basically I believe I would need to script with both pcbnew and eeschema’s kicad_netlist_reader simultaneously to join information from both.
So far I don’t know how to generate xml “intermediate” netlist programmatically - the only way it works for me is starting my script from Eeschema BOM generator window (it seems when I start my BOM script, the actual xml is generated just before).
Unfortunately the BOM script started from there can’t import pcbnew - returning an error:
import _pcbnew
ImportError: dynamic module does not define init function (init_pcbnew)
some digging around bring me to figure out the Python versions and
surprisingly the Python version in BOM generator environment is
2.7.17 (default, Sep 30 2020, 13:38:04)
[GCC 7.5.0]
while Pcbnew console indicates my default system Pyhton 3
Python 3.6.9 (default, Oct 8 2020, 12:12:24)
[GCC 8.4.0] on linux
Kicad is default Kubuntu package with KICAD_SCRIPTING_PYTHON3=ON
So I do not understand how the Python2 could be still there ?
Anyway it seems pcbnew can’t be imported from “BOM” Python2 environment …
I would appreciate any clue how to overcome this problem. Thanks.
This is getting interesting, I definitelly miss something basic…
I am trying to use pcbnew and kicad_netlist_reader simultaneosly (either in BOM generator or in pcbnew Python console) but I can’t import either one or the other…
When I launch a script from BOM generator (eeschema) it failes at “import pcbnew” while when I tried the same at pcbnew Python console it failed at “import kicad_netlist_reader” like:
import pcbnew
import kicad_netlist_reader
Traceback (most recent call last):
File “/usr/lib/python3.6/code.py”, line 91, in runcode
exec(code, self.locals)
File “”, line 1, in
ModuleNotFoundError: No module named ‘kicad_netlist_reader’
What I didn’t notice before is that the first attempt to “import pcbnew” from BOM generator has ended with different error "No module named ’ … while after I imported pcbnew first time at Pyhton console the error at BOM generator has changed to
Command error. Return code 1
Error messages:
Traceback (most recent call last):
File “/storage/kicad/scripts/gen_pos_RCS_1.py”, line 7, in
import pcbnew
File “/usr/lib/python3/dist-packages/pcbnew.py”, line 38, in
import _pcbnew
ImportError: dynamic module does not define init function (init_pcbnew)
Note that my command was just “import pcbnew” w/o underscore and “import kicad_netlist_reader”.
I think importing pcbnew is pretty basic operation - anybody has a clue what may be wrong ?
OK thanks, got it finally, I missed this obvious reason not using “python3” command … just learning Python from the scratch. To my excuse “python” was there as default command…
Btw. Any idea how to generate intermediate “xml” programatically without GUI of eeschema ?
My goal is to use custom “field” information from “xml” plus position information from pcbnew to generate PnP file.
Depending on your skill the answer ranges from “you can’t” to “you can’t but if you are brave enough you can emulate gui and key presses to programmatically trick the software”.
V6 is supposed to have eeschema api so this might change.
If all you need is custom field info you can extract it from schematic file directly, it has a fairly simple text format.
I can’t … I am affraid the emulation of GUI would be too fragile and hardly maintainable, I did something like this in the past and the experience was mixed.
I will probably stick to GUI generated xml and decoding sch as the next step.
Thanks for this discussion and patience, I needed some feedback to get back on the track.