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

Paths seem ok.

In the KiCad/bin/python.exe:

print sys.path
['', 'C:\\Program Files\\KiCad\\lib\\python27.zip', 'C:\\Program Files\\KiCad\\lib\\python2.7', 'C:\\Program Files\\KiCad\\lib\\python2.7\\plat-win32', 'C:\\Program Files\\KiCad\\lib\\python2.7\\lib-tk', 'C:\\Program Files\\KiCad\\lib\\python2.7\\lib-old', 'C:\\Program Files\\KiCad\\lib\\python2.7\\lib-dynload', 'C:\\building\\msys64\\mingw64', 'C:\\Program Files\\KiCad\\lib\\python2.7\\site-packages', 'C:\\Program Files\\KiCad\\lib\\python2.7\\site-packages\\wx-3.0-msw']

In the scripting console:

print sys.path
['', 'C:\\Program Files\\KiCad\\lib\\python27.zip', 'C:\\Program Files\\KiCad\\lib\\python2.7', 'C:\\Program Files\\KiCad\\lib\\python2.7\\plat-win32', 'C:\\Program Files\\KiCad\\lib\\python2.7\\lib-tk', 'C:\\Program Files\\KiCad\\lib\\python2.7\\lib-old', 'C:\\Program Files\\KiCad\\lib\\python2.7\\lib-dynload', 'C:\\building\\msys64\\mingw64', 'C:\\Program Files\\KiCad\\lib\\python2.7\\site-packages', 'C:\\Program Files\\KiCad\\lib\\python2.7\\site-packages\\wx-3.0-msw', '.', 'C:\\Program Files\\KiCad\\share\\kicad\\scripting', 'C:\\Program Files\\KiCad\\share\\kicad\\scripting\\plugins']

The scripting console has these additional paths appended to sys.path:
'.', 'C:\\Program Files\\KiCad\\share\\kicad\\scripting', 'C:\\Program Files\\KiCad\\share\\kicad\\scripting\\plugins'

but adding those manually before import pcbnew does not make a difference.

Also for the PATH environment variable:

In the KiCad/bin/python.exe:

print os.environ['PATH']
C:\Program Files\KiCad\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Intel\Shared Files\cpp\bin\Intel64;C:\Program Files (x86)\Intel\iCLS Client;C:\Program Files\Intel\iCLS Client;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Python27\Lib\site-packages\PyQt4;C:\Program Files (x86)\AMD APP\bin\x86_64; (snip)

In the scripting console:

print os.environ['PATH']
C:\Program Files\KiCad\bin;C:\Program Files\KiCad\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Intel\Shared Files\cpp\bin\Intel64;C:\Program Files (x86)\Intel\iCLS Client;C:\Program Files\Intel\iCLS Client;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Python27\Lib\site-packages\PyQt4;C:\Program Files (x86)\AMD APP\bin\x86_64; (snip)

All the other environment variables seem OK too.

I checked by dumping all of the environment variables to a python dictionary in the scripting console and then setting all of those environment variables from the dictionary in the command-line session. Still no change.

I am out of ideas now.

You could head over to the bugtracker and report this. Or maybe wait till somebody more knowledgeable comes along here.

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!