ImportError: No module named kicad.pcbnew.board

When trying to run scripts I got the subject message, so I recently upgraded to KiCad 5.1.6-1. Problem continues.

I am very disappointed that Back Annotation does not exist in KiCad so I’m trying find or write a script to do it.

I found a script that looks OK, but I get the subject error message (beginning of script is shown below).

I can find nowhere the most obvious, simplest information about running scripts. Where do you put them when working with Windows (not Linux please)? Do you have to import them first? How do you execute them? I may have guessed how, but try to find this explained anywhere.

Even the most basic tutorial on scripting quickly jumps too how to write code rather than discuss environment set up. It is so obvious to everyone, there’s no need for documentation?

Script:

-- coding: utf-8 --

import pcbnew
from kicad.pcbnew.board import Board
from kicad.pcbnew.layer import Layer
import re
import glob
import os
import codecs
print(“Starting…”)
b = Board.from_editor()

Backannotation can be found in the nightly builds, so unless you want to do it for 5.1 only, don’t bother.

You should tell more about what you tried and how. How do you try to run the script? When we know it, we can probably tell what went wrong.

This is the cause of the error you’re getting. You probably want something like this:

import pcbnew

b = pcbnew.GetBoard()

I downloaded a nightly build from Aug 3, 2020. It runs. I looked around and did not notice any new Back Annotation feature. Would someone please explain where it is and how to use it.

Can you point me to a good tutorial on scripting? I don’t need help writing code, which all of the tutorials quickly dive into. I need to understand the mechanics of running a script:

  1. Where do I put a .py file?
  2. How do I use it and run it?

Not sure where a good tutorial is, but you can start with a single python file or a python package (folder with init.py in it). Either way, put it in the kicad install directory under something like share\scripts\plugins. You should see other python files in there already. There’s also a place in your user folder structure to put it, but not sure exactly where.

Inside the python file, register the plugin as a KiCAD ActionPlugin. This will make it show up in the External Programs… menu in pcbnew. During development, get real familiar with the Script Console. If you want your program to work in Windows, it will have to be Python2. For Linux and Mac, I think it needs to be Python3. I try to make mine compatible with both. This should go away when Python3 is working on Windows. Review the KiCAD Python documentation to get a feel for the classes and their methods, but note that the documentation may not be for the exact version you’re running, so look in the Script Console object explorer tab )or whatever it’s called). Creating a GUI is a whole thing in itself, using wxWidgets. Avoid that when starting out.

Execute commands in the Script Console to get a feel for the objects in pcbnew. You can access all board objects from methods provided in “import pcbnew”.

This may have predated the external plugin section. I should probably move it and list it in the index.

Tools -> Update Schematic from PCB

image

Read the tooltips, compare with the Update PCB from Schematic dialog and ask if something isn’t clear. Remember that the nightly builds are very unstable at the moment and not suitable for projects unless you are adventurous.

Yes, I already found that.
I tried manually changing a diode’s reference from D2 to D100 and then tried Update Schematic from PCB. I got error messages about not finding D2 or D100.

Also, notice that the Reference designators checkbox is disabled. With little info to go on, I am guessing KiCad is nowhere near having Back Annotation.

Just to be clear, what I wish we had was:

  1. A feature in the PCB editor that lets us renumber references by position.
  2. Review those changes visually in the PCB editor.
  3. A feature that lets us push them back into the schematic.

I wonder if KiCad nightly builds will attempt that in the next week.

My options seem to be:

  1. Wait for a new, robust Back Annotation feature to materialize in a nightly build.
  2. Get scripting to work. Write a script to do BA.
  3. Renumber references the slow, painful manual way.
  4. Examine files and figure out a way to hack them.

Right now, I think #4 might be the best option for me, but I should try #2 more.

BTW, I think I found what might be a new, undesirable behavior in the nightly build. When I click on a symbol in the schematic, the corresponding foot print is shown in the PCB editor, but first it zooms out the PCB editor, which is going to pretty annoying if I attempt to manually do BA.

Thank you for the substantial advice.

I created the following file, test.py:
import pcbnew
b = Board.from_editor()
print(“does this work?”)

I am using Windows and v5.1.6 of KiCad. I put the above file here:
D:\KiCad\share\kicad\scripting\plugins

In the scripting console I entered “import test” and saw this:
import test
Traceback (most recent call last):
File “”, line 1, in
File “D:\Programs\KiCad\share\kicad\scripting\plugins/test.py”, line 2, in
b = Board.from_editor()
NameError: name ‘Board’ is not defined

I am very willing to study documentation, if only I could find it. Can someone help me to reach the point where a 3 line script works? What am I doing wrong?

I made my first successful plugin using the ‘Simple’ plugin from this site
After that, I incrementally added to it…

[EDIT] Forgot, the ‘Hello’ doesn’t work to display outside of the panel. Look at the PlugIn Panel in PCBnew, and checkout the "builtin’s

That’s not “backannotation” per se. Backannotation means moving annotation backwards from the layout to the schematic. You can select refdes (real backannotation) as part of updating the schematic based on the changed layout.

What you want is geographic reannotation. It’s coming soon. See https://gitlab.com/kicad/code/kicad/-/merge_requests/108 and Kicad with Geographic Reannotation Available for Test (the forum thread isn’t up to date).

I totally forgot about this very helpful link. On the right side is a Menu panel with the Scripting quick link.

You can copy & paste the examples into the PCBnew’s scripting panel. It still works - just confirmed it - screenshot below (output shown at bottom cluttered display…

Board is not a top level object. Start with what I posted:

import pcbnew
b = pcbnew.GetBoard()

b is now the object of type pcbnew.Board referring to the board in the GUI/editor.

Then go from there.

At the link I posted, you’ll find an Action Plugin example at the bottom of page.

I copied & pasted it, added an Icon link. Bingo! Results and Python code in a .py file also shown).

Plugin for Adding Info Text To PCB: ~same as for changing the Date…

textChange.mov

2 Likes

Just FYI, geographic reannotation has been merged to the development version and will probably be available in the next nightly builds.

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