Cannot "import pcbnew" when running KiCad/bin/python.exe on the command-line

Thanks Rene! I appreciate your suggestions so far.

BTW, I wonder if you can call pcbnew.exe with command-line options to have it execute a python script and exit?

No, KiCad uses command line options for very few purposes.

I found that I can get this working if I do the following 2 things:

  1. Copy _pcbnew.pyd from the sites-packages folder to the KiCad/bin folder.
  2. In my python session, changing the working directory to KiCad/bin before trying to import pcbnew, like this:
os.chdir("C:\\Program Files\\KiCad\\bin")
import pcbnew

I don’t understand why this works because I would think that having C:\Program Files\KiCad\bin on the PATH would achieve the same thing but it does not. My partial theory is that there are DLLs in the KiCad\bin folder that the import process is not finding.

1 Like

Program Files in latest windows can do some smoke and mirrors tricks. I would bet if you install KiCad to another folder those problems would go away.

I bet the code near following line was hard coded the path of loading _pcbnew.pyd DLL to current dir.
“C:\Program Files\KiCad\lib\python2.7\site-packages/pcbnew.py”, line 38

I checked but there is no hard-coded path information in pcbnew.py. That file is auto-generated by SWIG.

Line 38 of pcbnew.py looks like this:

import _pcbnew

That mean the error seem to be deeper inside the _pcbnew.pyd. I think is it a bug may need to be reported.
What if you don’t copy _pcbnew.pyd file into Kicad\bin, but do a chdir to it only?..

I did try that first but no, you definitely need to copy it.

Weird… I have the same version you have, and it work find on my computer!

I did revert back to v5.0.2 since this morning. I wonder if that is a difference with the nightly build? So many variables! :cry:

Just to confirm, what exactly worked on your computer?

I’m have
4.0.7 installed into C:\Program Files\KiCad_4.0.7
5.0.2 installed into C:\Program Files\KiCad_5.0.2
nightly (6.0.0-rc1-dev-1455-g4b7ef22ec) installed into C:\Program Files\KiCad_r12070

All of them are working with “import pcbnew”, and sys.path are seem to point to the correct folder that I installed them!

Weirder and weirder!

Just to confirm, you are NOT using the scripting console in PCBNEW right?
Also, what is your Windows version?

Would it be you have a mix-match version of _pcbnew.pyd ? Or more than 1 _pcbnew.pyd in your installation folder?

I try both from pcbnew console, and CMD… They both work!

It seem to me, this may mean the _pcbnew.pyd version are mix match with other DLL install in KiCad folder…

I didn’t find any extra _pcbnew.pyd copies. Also, when I installed v5.0.2, I uninstalled KiCad first and made sure C:/Program Files/KiCad was empty.

That seem weird! I have no more idea for now!

Thank you very much for the ideas and confirmation that it works on your PC though!

Oh, my is Windows 7, x64

Version: (6.0.0-rc1-dev-1455-g4b7ef22ec), release build
Libraries:
    wxWidgets 3.0.4
    libcurl/7.61.1 OpenSSL/1.1.1 (WinSSL) zlib/1.2.11 brotli/1.0.6 libidn2/2.0.5 libpsl/0.20.2 (+libidn2/2.0.5) nghttp2/1.34.0
Platform: Windows 7 (build 7601, Service Pack 1), 64-bit edition, 64 bit, Little endian, wxMSW
Build Info:
    wxWidgets: 3.0.4 (wchar_t,wx containers,compatible with 2.8)
    Boost: 1.68.0
    OpenCASCADE Community Edition: 6.9.1
    Curl: 7.61.1
    Compiler: GCC 8.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
1 Like

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