Running kicad python scripts on windows

I’ve quickly gotten lost in several rabbit holes here. I want to use the kicad_picknplace_assistant.py script from this thread:


I don’t know how to use it on my Win10 system. I appologize that I feel the answer to my question isn’t so much about KiCad, rather the proper Python installation that can access the kicad modules.

I’ve tried running it from the python console in kicad with execfile(“kicad_picknplace_assistant.py”) not expecting it to work because I’m not sure how to pass the file argument. But it failed loading numpy instead. I don’t know how to install numpy (and will also eventually need to install matplotilb) for kicad’s python console.

I installed Anaconda v5.2, but don’t know how to link the pcbnew module to where the python accessed via Anaconda Prompt can find it, so I get the following traceback:

Traceback (most recent call last):
  File "kicad_picknplace_assistant.py", line 5, in <module>
    import pcbnew
ModuleNotFoundError: No module named 'pcbnew'

I’ve tried putting the kicad_picknplace_assistant.py script into %AppData%\kicad\scripting and %AppData%\kicad\scripting\plugins. A .pyc (is that compiled?) is created in %AppData%\kicad\scripting but I don’t know how to access the script from within PCBNew or the python console. Even if I could, I suspect it would crap out trying to import numpy and how would I provide the board file?

I didn’t see any of the forks of the script appearing to have been modified as an action script, and again the lack of numpy and matplotlib…

Is there a better way of installing Python on Win10 that can access the pcbnew module to run a script from the command line? Was Anaconda a bad choice?

FWIW, I’m currently running the r10603 rc3-dev of KiCad v5 on Win 10 Pro Version 1803 build 17134.137 Here is the version info for KiCad:

Application: kicad
Version: (5.0.0-rc3-dev-2-g101b68b61), release build
Libraries:
    wxWidgets 3.0.3
    libcurl/7.54.1 OpenSSL/1.0.2l zlib/1.2.11 libssh2/1.8.0 nghttp2/1.23.1 librtmp/2.3
Platform: Windows 8 (build 9200), 64-bit edition, 64 bit, Little endian, wxMSW
Build Info:
    wxWidgets: 3.0.3 (wchar_t,wx containers,compatible with 2.8)
    Boost: 1.60.0
    OpenCASCADE Community Edition: 6.8.0
    Curl: 7.54.1
    Compiler: GCC 7.1.0 with C++ ABI 1011

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

You might consider installing MSYS2 (https://www.msys2.org/, https://github.com/msys2/msys2/wiki). It gives you a bash window on Windows, allows to install Perl or Python and many others, you may set the PATH variable etc. .

I do not use this with KiCad, but for making a gcc version of ngspice.

I haven’t found any method of doing “import new” on Win7 except by running the Python version installed with KiCad (default location is C:\Program Files\KiCad\lib\python2.7). None of my own Python installs can successfully load the pcbnew module.

So you need to run C:\Program Files\KiCad\bin\python.exe to be able to import pcbnew. If you need to install extra modules such as numpy you have do that in the KiCad python installation.

Running KiCad Python standalone means fiddling with the PATH variable. I use a batch file similar to the following :

path C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\

path %path%;C:\Programs\PortableGit\cmd

REM Standard Python 2.7
if /%1/ == // (
  path %path%;c:\python27;c:\python27\scripts;C:\Python27\Lib\site-packages\gtk-2.0\runtime\bin\

  set PKG_CONFIG_PATH=C:\Python27\Lib\pkgconfig\;C:\Python27\Lib\site-packages\gtk-2.0\runtime\lib\pkgconfig\
  set PYTHON_PKGCONFIG=C:\Python27\Lib\pkgconfig\
  set PYTHON_ROOT=C:\Python27\
  set PYTHON_SCRIPTS=C:\Python27\Scripts\
  set RUNTIME_BIN=C:\Python27\Lib\site-packages\gtk-2.0\runtime\bin\
  set RUNTIME_PKGCONFIG=C:\Python27\Lib\site-packages\gtk-2.0\runtime\lib\pkgconfig\
)

REM KiCad Python 2.7
if /%1/ == /kpy/ (
  path %path%;c:\programs\kicad\lib\python2.7;c:\programs\kicad\bin
  echo KiCad Python setup
)

That creates a new PATH with the exact things I want, as well as other Python env vars (you won’t need the gtk stuff probably). It uses a parameter to select which version of Python to put on the path, you could of course make it the default, save the file as “setup_kicad_python.bat” or whatever you like.

So the process becomes

  1. Start a new Command Prompt
  2. Run the batch file above with “kpy” parameter
  3. If you now type python --version it should show the version of the KiCad Python install, 2.7.13, and you can run scripts which import pcbnew.
  4. In this command window, run pip to install modules to the KiCad Python installation. pip -V will show where it is running.

The issue with compiled python modules like pcbnew is that they aren’t really portable between different python interpreter builds. I.e. a module built for python 3.6 using compiler X isn’t guaranteed to work with python 3.5 compiled with Y. Also, Kicad is still stuck with python 2, whereas your Anaconda installation probably uses python 3, which definitely can’t import the Kicad library.

However, I’d assume that Anaconda based on Python 2.7 should be able to import pcbnew when adding the Kicad site-packages to the module search path.

There is anouther problem though. According to a recent post my tool is currently broken due to an API change during the Kicad 5 development. The GetStart() method of the pcb.GetDrawings() objects seems to be gone.

I couldn’t get that method to work with Python 2.7, DLL load error.

pip should be included with the bundled python, so you should be able to cd to the bin folder in your kicad install and run the pip install numpy and then run your script. You may want to prefix the command with dot backslash to make sure it uses the correct binaries.

This is simply done via your windows command prompt, for example cmd.exe.

A relevant thread… Hoping something has changed…

nick, one of the problems is alot of pip packages like numpy requires gcc to complete install. (because they include actual extensions and not just python code).

Ok, I didn’t know that. But it seems like that pip install numpy does not even work inside the MSYS2 mingw shell.

Ah. That explains why I have been failing to pip install numpy. :frowning: I abandoned Anaconda because while it seemed to appear to be helpful, it was only helpful in the small use case of doing everything through Anaconda. I’ve reverted to Python.org’s installs so in theory anything that access the system shell can access python.

I just had a thought… I’ll give it a try this evening. What if I make sure that I install the same python 2.x version that KiCad comes with. Then, what folder should I make sure is in my path (kicad/bin or kicad/lib/python2.7) so the system python install can find pcbnew for import?

Once I get the environment set up (not something I find fun), I can then tackle the actual python script I want to use to see if I can get it updated to the latest API (this is something I would find fun).

Might be due to pip trying to be cross platform and it thinks it should do Unix when tis really Windows but pretending to be Unix via MSYS. Never looked, but it does have ca certificate issues because the msys build doesn’t allow it to use the windows certificate store.

Really its an argument to ditch MSYS and go MSVC for real native capabilities (such as using system python) :confused:

There are a bunch of precompiled Python packages for Python 2 & 3 on Windows here so GCC is not needed. numpy is included.

Have you actually used them with the bundled kicad python? Python built and dependent on MSYS won’t necessarily be compatible with Python built by MSVC. The plugins on those page are built by MSVC.

No, I’ve only used these with the python installed from python.org.

1 Like

Ok, I’m not sure if I’m getting closer or further…
I have python.org’s 2.7.15 (yes, I know KiCad is 2.7.14, I hope this isn’t the source of my current issue) before the KiCad python in my user path environment variable. I was able to install both numpy and matplotlib in python.org’s 2.7.15 w/o any issue (I hope) with no mention of not finding gcc.

Now when I try to run the pick and place python script I get:

Fatal Python error: PyThreadState_Get: no current thread

I currently have the following paths (in this order) at the end of my user path variable:

C:\Python27
C:\Python27\Scripts
C:\Program Files\KiCad\lib\python2.7
C:\Program Files\KiCad\bin
C:\Python37
C:\Python37\Scripts

And have additionally created a user environment variable “PYTHONPATH” that so far only contains:

C:\Program Files\KiCad\lib\python2.7\site-packages

(This is the path that I found the pcbnew.py file among others.)

Any more hints on how to crack this nut?

Or is this the expected error for the known bug (wrong kicad api)?

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