Can’t Import pcbnew in eeschema BOM script

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 … :frowning:
I would appreciate any clue how to overcome this problem. Thanks.

Btw. KiCad Version: 5.1.9-73d0e3b20d~88~ubuntu18.04.1

What does your command in eeschema look like? Likely simply switching binary to python3 will fix it but also import pcbnew instead of _pcbnew.

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, I have probably sorted out what is going on…
For other poor users who may be interested in kicad Python2/3 mess in Linux (kubuntu 18.04):

Kicad “BOM generator” launches Python 2
print(sys.version)
2.7.17 (default, Sep 30 2020, 13:38:04) [GCC 7.5.0]
print(sys.executable)
/usr/bin/python

While Kicad Python console launches Python3
‘3.6.9 (default, Oct 8 2020, 12:12:24) \n[GCC 8.4.0]’
/usr/bin/python3

The Kicad pcbnew is automagically stored in python3 folders
print(pcbnew.file)
/usr/lib/python3/dist-packages/pcbnew.py

and so it is unaccesible from “BOM generator” which doesn’t have these places accesible

print(sys.path)
[’/storage/kicad/scripts’, ‘/usr/lib/python3/dist-packages’, ‘/usr/lib/python2.7’, ‘/usr/lib/python2.7/plat-x86_64-linux-gnu’, ‘/usr/lib/python2.7/lib-tk’, ‘/usr/lib/python2.7/lib-old’, ‘/usr/lib/python2.7/lib-dynload’, ‘/home/user/.local/lib/python2.7/site-packages’, ‘/usr/local/lib/python2.7/dist-packages’, ‘/usr/lib/python2.7/dist-packages’, ‘/usr/lib/python2.7/dist-packages/gtk-2.0’]

Kicad “BOM generator” launches whatever you ask it to launch.
Change your command executable to python3 and it will launch python 3.

If only you had read my first message

OK thanks, got it finally, I missed this obvious reason not using “python3” command :wink: … 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 :smiley: … 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.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.