Problem using python to load demo board

Can anyone run a python command for me? I’m getting an error:

I can load the demo board fine through pcbnew, but it doesn’t load when using python scripting.

import pcbnew
pcbnew.LoadBoard(‘share\kicad\demos\complex_hierarchy\complex_hierarchy.kicad_pcb’)

Error:

Traceback (most recent call last):
File “”, line 1, in
File “C:\Program Files\KiCad\lib\python2.7\site-packages/pcbnew.py”, line 6860, in LoadBoard
return _pcbnew.LoadBoard(*args)
Traceback (most recent call last):
File “C:\Program Files\KiCad\lib\python2.7\site-packages\wx-3.0-msw/wx/py/shell.py”, line 562, in OnKeyDown
self.processLine()
File “C:\Program Files\KiCad\lib\python2.7\site-packages\wx-3.0-msw/wx/py/shell.py”, line 886, in processLine
self.push(command)
File “C:\Program Files\KiCad\lib\python2.7\site-packages\wx-3.0-msw/wx/py/shell.py”, line 976, in push
self.more = self.interp.push(command)
File “C:\Program Files\KiCad\lib\python2.7\site-packages\wx-3.0-msw/wx/py/interpreter.py”, line 80, in push
more = self.more = self.runsource(source)
File “C:\Program Files\KiCad\lib\python2.7\site-packages\wx-3.0-msw/wx/py/interpreter.py”, line 90, in runsource
more = InteractiveInterpreter.runsource(self, source)
File “C:\Program Files\KiCad\lib\python2.7/code.py”, line 87, in runsource
self.runcode(code)
File “C:\Program Files\KiCad\lib\python2.7/code.py”, line 107, in runcode
self.showtraceback()
File “C:\Program Files\KiCad\lib\python2.7/code.py”, line 162, in showtraceback
map(self.write, list)
File “C:\Program Files\KiCad\lib\python2.7/code.py”, line 171, in write
sys.stderr.write(data)
File “C:\Program Files\KiCad\lib\python2.7\site-packages\wx-3.0-msw/wx/py/shell.py”, line 1247, in writeErr
self.write(text)
File “C:\Program Files\KiCad\lib\python2.7\site-packages\wx-3.0-msw/wx/py/shell.py”, line 1001, in write
self.AddText(text)
File “C:\Program Files\KiCad\lib\python2.7\site-packages\wx-3.0-msw/wx/stc.py”, line 2041, in AddText
return _stc.StyledTextCtrl_AddText(*args, **kwargs)
File “C:\Program Files\KiCad\lib\python2.7/encodings/cp1252.py”, line 15, in decode
return codecs.charmap_decode(input,errors,decoding_table)
UnicodeDecodeError: ‘charmap’ codec can’t decode byte 0x9d in position 101: character maps to

Application: Pcbnew
Version: (5.1.5)-3, release build
Libraries:
wxWidgets 3.0.4
libcurl/7.66.0 OpenSSL/1.1.1d (Schannel) zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 
libpsl/0.21.0 (+libidn2/2.1.1) nghttp2/1.39.2
Platform: Windows 8 (build 9200), 64-bit edition, 64 bit, Little endian, wxMSW
Build Info:
wxWidgets: 3.0.4 (wchar_t,wx containers,compatible with 2.8)
Boost: 1.71.0
OpenCASCADE Community Edition: 6.9.1
Curl: 7.66.0
Compiler: GCC 9.2.0 with C++ ABI 1013

Build settings:
USE_WX_GRAPHICS_CONTEXT=OFF
USE_WX_OVERLAY=OFF
KICAD_SCRIPTING=ON
KICAD_SCRIPTING_MODULES=ON
KICAD_SCRIPTING_PYTHON3=OFF
KICAD_SCRIPTING_WXPYTHON=ON
KICAD_SCRIPTING_WXPYTHON_PHOENIX=OFF
KICAD_SCRIPTING_ACTION_MENU=ON
BUILD_GITHUB_PLUGIN=ON
KICAD_USE_OCE=ON
KICAD_USE_OCC=OFF
KICAD_SPICE=ON

What windows version are you using? I see mentions of cp1252 in stack trace, there is no reason to use that encoding nowadays.

Windows 10. Not sure how or why it’s picking the cp1252 encoding. Loading KiCad, starting pcbnew, then starting the Script Console. Then typing in those two commands right from the beginning.

I’ve done it on Win10 ENglish and I had to correct the single quotes for the path (but this might be a forum copy-paste issue) and it works:

Py 0.9.8
Python 2.7.16 (default, Aug 15 2019, 17:51:40)  [GCC 9.2.0 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Startup script executed: C:\Users\MitjaN\AppData\Roaming\kicad\PyShell_pcbnew_startup.py
import pcbnew
board = pcbnew.LoadBoard('share\kicad\demos\complex_hierarchy\complex_hierarchy.kicad_pcb')
modules = board.GetModules()
for mod in modules:
    print mod.GetReference()
    
P5
P6
P3
P4
P2
P1
D9
D4
D6
D7
D3
D2
D8
D5
D1
C3
C4
C14
C12
C5
C6
C7
C8
U3
U1
U4
R20
R22
R23
R24
R25
R26
R27
R28
R5
R4
R3
R21
R17
R7
R8
R9
R10
R11
R12
R13
R14
R15
R16
R18
R6
R19
C1
C2
C9
C10
C11
Q1
Q2
Q3
Q4
Q5
Q6
Q7
Q8
RV1
RV2
U2

I’ve tried this with recent 5.1.5 nightly and 5.99 nigthly. I’get the same results in both cases
Version: (5.1.5-68-g1cbee4c50)-1
Version: (5.99.0-804-g88e0ef548)

Looks like there may be some kind of error in wxWidgets? Not sure why that’s in my error message. Do you have the full version/help text of the version that works for you? (I’m using the stable release).

Application: KiCad
Version: (5.1.5-68-g1cbee4c50)-1, release build
Libraries:
wxWidgets 3.0.4
libcurl/7.66.0 OpenSSL/1.1.1d (Schannel) zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 libpsl/0.21.0 (+libidn2/2.1.1) nghttp2/1.39.2
Platform: Windows 8 (build 9200), 64-bit edition, 64 bit, Little endian, wxMSW
Build Info:
wxWidgets: 3.0.4 (wchar_t,wx containers,compatible with 2.8)
Boost: 1.71.0
OpenCASCADE Community Edition: 6.9.1
Curl: 7.66.0
Compiler: GCC 9.2.0 with C++ ABI 1013

Build settings:
USE_WX_GRAPHICS_CONTEXT=OFF
USE_WX_OVERLAY=OFF
KICAD_SCRIPTING=ON
KICAD_SCRIPTING_MODULES=ON
KICAD_SCRIPTING_PYTHON3=OFF
KICAD_SCRIPTING_WXPYTHON=ON
KICAD_SCRIPTING_WXPYTHON_PHOENIX=OFF
KICAD_SCRIPTING_ACTION_MENU=ON
BUILD_GITHUB_PLUGIN=ON
KICAD_USE_OCE=ON
KICAD_USE_OCC=OFF
KICAD_SPICE=ON

and

Application: KiCad
Version: (5.99.0-804-g88e0ef548), release build
Libraries:
wxWidgets 3.0.4
libcurl/7.66.0 OpenSSL/1.1.1d (Schannel) zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 libpsl/0.21.0 (+libidn2/2.1.1) nghttp2/1.39.2
Platform: Windows 8 (build 9200), 64-bit edition, 64 bit, Little endian, wxMSW
Build Info:
Build date: Jan 26 2020 21:59:39
wxWidgets: 3.0.4 (wchar_t,wx containers,compatible with 2.8)
Boost: 1.71.0
OpenCASCADE Community Edition: 6.9.1
Curl: 7.66.0
Compiler: GCC 9.2.0 with C++ ABI 1013

Build settings:
KICAD_SCRIPTING=ON
KICAD_SCRIPTING_MODULES=ON
KICAD_SCRIPTING_PYTHON3=OFF
KICAD_SCRIPTING_WXPYTHON=ON
KICAD_SCRIPTING_WXPYTHON_PHOENIX=OFF
KICAD_SCRIPTING_ACTION_MENU=ON
BUILD_GITHUB_PLUGIN=ON
KICAD_USE_OCE=ON
KICAD_USE_OCC=OFF
KICAD_SPICE=ON

Tested in Win10 also with stable release

Py 0.9.8
Python 2.7.16 (default, Aug 15 2019, 17:51:40)  [GCC 9.2.0 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Startup script executed: C:\Users\Uniterra\AppData\Roaming\kicad\PyShell_pcbnew_startup.py
import pcbnew
board = pcbnew.LoadBoard('share\kicad\demos\complex_hierarchy\complex_hierarchy.kicad_pcb')
modules = board.GetModules()
for mod in modules:
    print mod.GetReference()
    
P5
P6
P3
P4
P2
P1
D9
D4
D6
D7
D3
D2
D8
D5
D1
C3
C4
C14
C12
C5
C6
C7
C8
U3
U1
U4
R20
R22
R23
R24
R25
R26
R27
R28
R5
R4
R3
R21
R17
R7
R8
R9
R10
R11
R12
R13
R14
R15
R16
R18
R6
R19
C1
C2
C9
C10
C11
Q1
Q2
Q3
Q4
Q5
Q6
Q7
Q8
RV1
RV2
U2
Application: Pcbnew
Version: (5.1.5)-3, release build
Libraries:
    wxWidgets 3.0.4
    libcurl/7.66.0 OpenSSL/1.1.1d (Schannel) zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 libpsl/0.21.0 (+libidn2/2.1.1) nghttp2/1.39.2
Platform: Windows 8 (build 9200), 64-bit edition, 64 bit, Little endian, wxMSW
Build Info:
    wxWidgets: 3.0.4 (wchar_t,wx containers,compatible with 2.8)
    Boost: 1.71.0
    OpenCASCADE Community Edition: 6.9.1
    Curl: 7.66.0
    Compiler: GCC 9.2.0 with C++ ABI 1013

Build settings:
    USE_WX_GRAPHICS_CONTEXT=OFF
    USE_WX_OVERLAY=OFF
    KICAD_SCRIPTING=ON
    KICAD_SCRIPTING_MODULES=ON
    KICAD_SCRIPTING_PYTHON3=OFF
    KICAD_SCRIPTING_WXPYTHON=ON
    KICAD_SCRIPTING_WXPYTHON_PHOENIX=OFF
    KICAD_SCRIPTING_ACTION_MENU=ON
    BUILD_GITHUB_PLUGIN=ON
    KICAD_USE_OCE=ON
    KICAD_USE_OCC=OFF
    KICAD_SPICE=ON

It worked also direct in the command line:

Microsoft Windows [Version 10.0.18363.592]
(c) 2019 Microsoft Corporation. Alle Rechte vorbehalten.

c:\Program Files\KiCad_v5_1_5\bin>python.exe
Python 2.7.16 (default, Aug 15 2019, 17:51:40)  [GCC 9.2.0 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pcbnew
>>> board = pcbnew.LoadBoard('..\share\kicad\demos\complex_hierarchy\complex_hierarchy.kicad_pcb')
>>> modules = board.GetModules()
>>> for mod in modules:
...   print mod.GetReference()
...
P5
P6
P3
P4
P2
P1
D9
D4
D6
D7
D3
D2
D8
D5
D1
C3
C4
C14
C12
C5
C6
C7
C8
U3
U1
U4
R20
R22
R23
R24
R25
R26
R27
R28
R5
R4
R3
R21
R17
R7
R8
R9
R10
R11
R12
R13
R14
R15
R16
R18
R6
R19
C1
C2
C9
C10
C11
Q1
Q2
Q3
Q4
Q5
Q6
Q7
Q8
RV1
RV2
U2

What does the getdefaultencoding function return?

import sys
sys.getdefaultencoding()
> 'ascii'

I think the error boils down to “can’t find file”. It’d be great if this error message is improved.

When I put the full file path and name in quotes, the LoadBoard function returns properly and appears to load the board. I’m not sure what path is prepended to the LoadBoard filename, but when I check the getcwd, the full path would not be found.

import os
os.getcwd()

Thank you!!

Did you by any chance disable the instalation of Demo files? I’ve checked the location of C:\Program Files\KiCad\share\kicad\demos\complex_hierarchy and I have a project there.

On (5.1.5-68-g1cbee4c50)-1

import sys
sys.getdefaultencoding()
'ascii'

import os
os.getcwd()
'C:\\Program Files\\KiCad'

on (5.99.0-804-g88e0ef548)

import sys
sys.getdefaultencoding()
'ascii'

import os
os.getcwd()
'C:\\Program Files\\KiCad_nightly'

The demo files are installed. Somehow the cwd is being modified by a plug-in.

Do you know which plugin it is?

The directory is KiCommand, the plug-in I wrote. I’m trying to find out if it’s in the source of the plug-in or somehow due to KiCAD doing something. It might be in code that saves local information.

Edit: turns out there is an explicit os.chdir() in KiCommand startup. I want to rework that to remove it.

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