Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v6 MacOS functionality and wxwidgets #252

Closed
a7hybnj2 opened this issue Dec 20, 2021 · 71 comments
Closed

v6 MacOS functionality and wxwidgets #252

a7hybnj2 opened this issue Dec 20, 2021 · 71 comments

Comments

@a7hybnj2
Copy link
Contributor

I just updated kicad and kikit. I am running
KiCad Version: (6.0.0-rc2-14-ga17a58203b), release build and
kikit, version 0.99-pre1.0+76.g2d499e0 although I tired this with 0.7 also. On
macOS 12.0.1

The error I get is

An error occurred: dlopen(/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so, 0x0002): Library not loaded: @rpath/libwx_osx_cocoau-3.1.5.dylib
  Referenced from: /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so
  Reason: tried: '/usr/local/lib/libwx_osx_cocoau-3.1.5.dylib' (no such file), '/usr/local/lib/libwx_osx_cocoau-3.1.5.dylib' (no such file), '/usr/local/lib/libwx_osx_cocoau-3.1.5.dylib' (no such file), '/usr/lib/libwx_osx_cocoau-3.1.5.dylib' (no such file)
No output files produced

This was referenced in #21 but this is indeed a different issue.

I was able to get past the initial error that was looking for libwx_osx_cocoau_gl-3.1.5.dylib by installing wxwidgets using brew install wxwidgets but now that I think about it that shouldn't have been necessary. It shouldn't be looking for the local libs...right? Shouldn't all of that be packaged into the kicad environment?

I know yaqwsx doesn't have a mac at their disposal so I am just putting my notes here in case they or anyone else gets a similar error or figures how to progress past this point.

@yaqwsx
Copy link
Owner

yaqwsx commented Dec 21, 2021

I am not a mac user but I was wondering if the binary `pcbnew-nightly isn't just a wrapper script that properly set all library paths - just like in #246. The paths, of course, could be different.

@yaqwsx yaqwsx closed this as completed Dec 21, 2021
@a7hybnj2
Copy link
Contributor Author

So I set LD_LIBRARY_PATH to /Applications/KiCad/KiCad.app/Contents/Frameworks/ which is where

libwx_osx_cocoau_gl-3.1.5.0.0.dylib
libwx_osx_cocoau_gl-3.1.5.dylib
libwx_osx_cocoau-3.1.5.0.0.dylib
libwx_osx_cocoau-3.1.5.dylib

all live. But, I still got the same error as above. Is there another path I should be setting?

@yaqwsx
Copy link
Owner

yaqwsx commented Dec 21, 2021

Quick googling told me that mac uses DYLD_LIBRARY_PATH instead of LD_LIBRARY_PATH. Try that. But I am shooting blindly as I have no experience with mac...

@a7hybnj2
Copy link
Contributor Author

a7hybnj2 commented Dec 21, 2021

Okay I set my DYLD_LIBRARY_PATH to /Applications/KiCad/KiCad.app/Contents/Frameworks because that is where the libwx_osx_cocoau files live. That appears to have resolved that problem but now it cannot locate "Python" itself.

An error occurred: dlopen(/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so, 0x0002): Library not loaded: @rpath/Versions/3.8/Python
  Referenced from: /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so
  Reason: tried: '/Applications/KiCad/KiCad.app/Contents/Frameworks/Python' (no such file), '/usr/local/lib/Versions/3.8/Python' (no such file), '/usr/local/lib/Versions/3.8/Python' (no such file), '/usr/local/lib/Python' (no such file), '/usr/lib/Python' (no such file)

There are 3 Python files

/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Python
/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/Python
/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/Resources/Python.app/Contents/MacOS/Python

@yaqwsx
Copy link
Owner

yaqwsx commented Dec 22, 2021

Could you:

  • check content DYLD_LIBRARY_PATH before setting a value?
  • check what does the pcbnew-nighlty binary does. I guess it will be a shell script

@yaqwsx yaqwsx reopened this Dec 22, 2021
@yaqwsx
Copy link
Owner

yaqwsx commented Dec 22, 2021

BTW: I am sorry; I probably closed this issue yesterday by mistake when posting an answer.

@cdwilson
Copy link

@yaqwsx I think the issue here is that the Python bundled with KiCad is ultimately calling ctypes.util.find_library() which probably can't find any *.dylib installed by homebrew. Homebrew patches Python to fix this (see https://github.com/Homebrew/homebrew-core/blob/15e7f288e4b97c75b32c75748df0daa3b60d1c77/Formula/python%403.9.rb#L185), but I'm guessing the KiCad devs aren't patching the Python included with KiCad in the same way.

@a7hybnj2 Can you try adding something like the following to your ~/Library/Preferences/kicad/6.0/PyShell_pcbnew_startup.py:

import sys
if sys.platform == "darwin":
    import os
    os.environ["DYLD_FALLBACK_LIBRARY_PATH"] = "/opt/homebrew/lib/" <-- put your path to homebrew here

This should allow the KiCad python to find any dylibs installed by homebrew.

image

image

@cdwilson
Copy link

I was able to get past the initial error that was looking for libwx_osx_cocoau_gl-3.1.5.dylib by installing wxwidgets using brew install wxwidgets but now that I think about it that shouldn't have been necessary. It shouldn't be looking for the local libs...right? Shouldn't all of that be packaged into the kicad environment?

Sorry, I misread this earlier, and now that I think about this, I think @a7hybnj2 is right, it seems like these should be included with KiCad in /Applications/KiCad/KiCad.app/Contents/Frameworks, not something installed by homebrew.

Okay I set my DYLD_LIBRARY_PATH to /Applications/KiCad/KiCad.app/Contents/Frameworks because that is where the libwx_osx_cocoau files live. That appears to have resolved that problem but now it cannot locate "Python" itself.

Slight edit to what I proposed earlier, this should allow KiCad Python to find libwx_osx_cocoau_gl-3.1.5.dylib:

# ~/Library/Preferences/kicad/6.0/PyShell_pcbnew_startup.py
import sys
if sys.platform == "darwin":
    import os
    os.environ["DYLD_FALLBACK_LIBRARY_PATH"] = "/Applications/KiCad/KiCad.app/Contents/Frameworks"

image

@a7hybnj2
Copy link
Contributor Author

I tested with your above fix and was still getting the same errors so just in console I

export PYTHONPATH=/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ kikit
export DYLD_LIBRARY_PATH=/Applications/KiCad/KiCad.app/Contents/Frameworks
export DYLD_FALLBACK_LIBRARY_PATH=/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8

all of the previous errors went away and were replaced by: Segmentation fault: 11

What is @rpath? after setting just PYTHONPATH and the FALLBACK it is trying to work but @rpath/Versions/3.8/Python doesn't expand correctly. An error occurred: dlopen(/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so, 0x0002): Library not loaded: @rpath/Versions/3.8/Python if @rpath=Python.framework then it would be looking for python in the right spot...

@cdwilson
Copy link

I'm brand new to KiKit, and I didn't realize earlier that it wasn't just using the Python bundled with KiCad, so the PyShell_pcbnew_startup.py change I mentioned above won't make any difference running kikit externally. I tried going through the install instructions last night, but I wasn't able to get it working on my system either.

@a7hybnj2 are you seeing any difference when setting DYLD_LIBRARY_PATH or DYLD_FALLBACK_LIBRARY_PATH on the command line? Passing those on the command line doesn't seem to be working for me (it appears that SIP might be sanitizing DYLD_* environment variables, see https://briandfoy.github.io/macos-s-system-integrity-protection-sanitizes-your-environment/):

$ PYTHONPATH=/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ \
DYLD_FALLBACK_LIBRARY_PATH=/Applications/KiCad/KiCad.app/Contents/Frameworks \
kikit panelize --layout 'grid; rows: 2; cols: 2;' --tabs full --cuts vcuts doc/resources/conn.kicad_pcb panel.kicad_pcb
An error occurred: dlopen(/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so, 0x0002): Library not loaded: @rpath/libwx_osx_cocoau_gl-3.1.5.dylib
  Referenced from: /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so
  Reason: tried: '/usr/local/lib/libwx_osx_cocoau_gl-3.1.5.dylib' (no such file), '/usr/lib/libwx_osx_cocoau_gl-3.1.5.dylib' (no such file)
No output files produced

What is @rpath?

It appears to be a macro for the dynamic loader that lets you specify dependency paths that are resolved at runtime. (see https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/RunpathDependentLibraries.html#//apple_ref/doc/uid/TP40008306-SW1)

When the dynamic loader (dyld) loads the executable, it looks for run-path dependent libraries in the run-path search paths in the order in which they were specified at link time

You can inspect these rpaths using otool:

$ $ otool -L /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so
/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so:
        /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
        /System/Library/Frameworks/Carbon.framework/Versions/A/Carbon (compatibility version 2.0.0, current version 164.0.0)
        /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 23.0.0)
        /System/Library/Frameworks/AudioToolbox.framework/Versions/A/AudioToolbox (compatibility version 1.0.0, current version 1000.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.60.1)
        /System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
        @rpath/libwx_osx_cocoau_gl-3.1.5.dylib (compatibility version 1.0.0, current version 1.0.0)
        @rpath/libwx_osx_cocoau-3.1.5.dylib (compatibility version 1.0.0, current version 1.0.0)
        @rpath/Versions/3.8/Python (compatibility version 3.8.0, current version 3.8.0)
        @rpath/libkicad_3dsg.2.0.0.dylib (compatibility version 2.0.0, current version 0.0.0)
        /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 2022.20.117)
        /System/Library/Frameworks/CoreData.framework/Versions/A/CoreData (compatibility version 1.0.0, current version 1044.3.0)
        /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1770.255.0)
        /usr/lib/libcurl.4.dylib (compatibility version 7.0.0, current version 9.0.0)
        @rpath/libGLEW.2.2.dylib (compatibility version 2.2.0, current version 2.2.0)
        @rpath/libcairo.2.dylib (compatibility version 11603.0.0, current version 11603.0.0)
        @rpath/libpixman-1.0.dylib (compatibility version 41.0.0, current version 41.0.0)
        /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 904.4.0)
        /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1770.255.0)
        /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)

You can actually experiment with changing the rpaths using install_name_tool:

install_name_tool -change @rpath/Versions/3.8/Python /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/Python /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so
install_name_tool -change @rpath/libwx_osx_cocoau_gl-3.1.5.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/libwx_osx_cocoau_gl-3.1.5.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so
install_name_tool -change @rpath/libwx_osx_cocoau-3.1.5.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/libwx_osx_cocoau-3.1.5.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so
install_name_tool -change @rpath/libkicad_3dsg.2.0.0.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/libkicad_3dsg.2.0.0.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so
install_name_tool -change @rpath/libGLEW.2.2.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/libGLEW.2.2.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so
install_name_tool -change @rpath/libcairo.2.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/libcairo.2.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so
install_name_tool -change @rpath/libpixman-1.0.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/libpixman-1.0.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so
install_name_tool -change @rpath/libwx_osx_cocoau_gl-3.1.5.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/libwx_osx_cocoau_gl-3.1.5.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/libwx_osx_cocoau_gl-3.1.5.0.0.dylib
install_name_tool -change @rpath/libwx_osx_cocoau-3.1.5.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/libwx_osx_cocoau-3.1.5.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/libwx_osx_cocoau_gl-3.1.5.0.0.dylib
install_name_tool -change @rpath/libwx_osx_cocoau-3.1.5.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/libwx_osx_cocoau-3.1.5.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/libkicad_3dsg.2.0.0.dylib
install_name_tool -change @rpath/libwx_osx_cocoau_gl-3.1.5.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/libwx_osx_cocoau_gl-3.1.5.dylib /Applications/KiCad/KiCad.app/Contents/Frameworks/libkicad_3dsg.2.0.0.dylib

This results in absolute paths that are already resolved:

$ otool -L /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pcbnew.so:
        /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
        /System/Library/Frameworks/Carbon.framework/Versions/A/Carbon (compatibility version 2.0.0, current version 164.0.0)
        /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 23.0.0)
        /System/Library/Frameworks/AudioToolbox.framework/Versions/A/AudioToolbox (compatibility version 1.0.0, current version 1000.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.60.1)
        /System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
        /Applications/KiCad/KiCad.app/Contents/Frameworks/libwx_osx_cocoau_gl-3.1.5.dylib (compatibility version 1.0.0, current version 1.0.0)
        /Applications/KiCad/KiCad.app/Contents/Frameworks/libwx_osx_cocoau-3.1.5.dylib (compatibility version 1.0.0, current version 1.0.0)
        /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/Python (compatibility version 3.8.0, current version 3.8.0)
        /Applications/KiCad/KiCad.app/Contents/Frameworks/libkicad_3dsg.2.0.0.dylib (compatibility version 2.0.0, current version 0.0.0)
        /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 2022.20.117)
        /System/Library/Frameworks/CoreData.framework/Versions/A/CoreData (compatibility version 1.0.0, current version 1044.3.0)
        /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1770.255.0)
        /usr/lib/libcurl.4.dylib (compatibility version 7.0.0, current version 9.0.0)
        /Applications/KiCad/KiCad.app/Contents/Frameworks/libGLEW.2.2.dylib (compatibility version 2.2.0, current version 2.2.0)
        /Applications/KiCad/KiCad.app/Contents/Frameworks/libcairo.2.dylib (compatibility version 11603.0.0, current version 11603.0.0)
        /Applications/KiCad/KiCad.app/Contents/Frameworks/libpixman-1.0.dylib (compatibility version 41.0.0, current version 41.0.0)
        /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 904.4.0)
        /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1770.255.0)
        /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)

However, even after replacing the rpaths with absolute paths, I'm still getting the same Segmentation fault: 11 as @a7hybnj2:

$ PYTHONPATH=/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ \
kikit panelize --layout 'grid; rows: 2; cols: 2;' --tabs full --cuts vcuts doc/resources/conn.kicad_pcb panel.kicad_pcb
Segmentation fault: 11

At this point, I'm out of ideas... 🤷

@a7hybnj2
Copy link
Contributor Author

Now that KiCad 6 is officially out I suspect it is only a matter of time before someone figures it out. I too have done all I know how to do.

@yaqwsx
Copy link
Owner

yaqwsx commented Dec 24, 2021

I am still unsure whether it is safe to release v1, if I cannot confirm it works on Mac. Or if it is better to release v1, find the Mac users and then release v1.0.1 that is patched for Mac.

@cdwilson
Copy link

Given that Mac users can run KiKit in Docker, I'd say just release v1 and make a note that macOS isn't supported yet (who knows how long until somebody figures out how to get this working on Mac...)

@tywtyw2002
Copy link
Contributor

It seems like the MacOS Version KiCAD compile a standalone python version. The python library did not share w/ system or brew version python.

The easy way is to use kicad python to install kikit. And use that kikit to do panelize.

Install
/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/pip3 install kikit

Panelize
/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/kikit panelize --layout 'grid; rows: 2; cols: 1;' --tabs full --cuts vcuts ./example.kicad_pcb panel.kicad_pcb

@a7hybnj2
Copy link
Contributor Author

a7hybnj2 commented Dec 29, 2021

Well, I just tried the above and it seems promising although I still ran into some errors. When I installed kikit I get a
ERROR: wxpython 4.1.1 requires pillow, which is not installed.
so I just did a
/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/pip3 install pillow
but then when I ran the panelize command I got

An error occurred: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.8 from "/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/python3.8"
  * The NumPy version is: "1.21.5"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: dlopen(/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so, 0x0002): tried: '/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so' (code signature in <D6DEC3D7-AF4F-3AA2-B2DD-D6A87AC638BA> '/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so' not valid for use in process: mapped file has no Team ID and is not a platform binary (signed with custom identity or adhoc?)), '/usr/lib/_multiarray_umath.cpython-38-darwin.so' (no such file)

seems like this might be a pythonpath dyld thing again. I will try to locate those packages and point to them correctly and update here.

EDIT: Looks like it is looking in the correct places but something with a signature or ¯\(ツ)/¯. I am again past my knowledge of the issue.

EDIT 2: I had just installed STABLE this morning upgrading/replacing the nightly build. I am under the impression removing kicad will also remove its version of python and associated packages so nothing local should be messing it up.

@tywtyw2002
Copy link
Contributor

tywtyw2002 commented Dec 29, 2021

You got this error ERROR: wxpython 4.1.1 requires pillow, which is not installed. That fine. Skip it.

Make sure you use new SHELL without any PATHONPATH environments.

T:/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin|
⇒  ./pip3 -V
pip 19.2.3 from /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
T:/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin|
⇒  ./pip3 install kikit
Collecting kikit
  Using cached https://files.pythonhosted.org/packages/e2/6a/b67074cc94d4373c7e82b661c59a227fa51fb5251e96a721d375cb91e49a/KiKit-1.0.0-py3-none-any.whl
Collecting commentjson (from kikit)
  Using cached https://files.pythonhosted.org/packages/c0/76/c4aa9e408dbacee3f4de8e6c5417e5f55de7e62fb5a50300e1233a2c9cb5/commentjson-0.9.0.tar.gz
Collecting solidpython (from kikit)
  Using cached https://files.pythonhosted.org/packages/b6/1e/3a065797bd8fe8e2e0ced73e6baf07fdb921fecc04fa18b2b8ffb34f3c87/solidpython-1.1.1-py3-none-any.whl
Collecting shapely (from kikit)
  Downloading https://files.pythonhosted.org/packages/80/4f/d96da54dadc06b3202df977f3721cc72f9f322f0ec5f15dd0baabd5ab365/Shapely-1.8.0-cp38-cp38-macosx_10_9_x86_64.whl (1.2MB)
     |████████████████████████████████| 1.2MB 3.3MB/s
Collecting pybars3 (from kikit)
  Using cached https://files.pythonhosted.org/packages/ec/1a/2fb847db017f9f89ab8519d96e35fb3dacb6170a0643fddba3b366af0af1/pybars3-0.9.7.tar.gz
Collecting numpy (from kikit)
  Downloading https://files.pythonhosted.org/packages/e8/b2/be38f1036c7e7f6af79201f09de86615599beeb232f37dd288d9631d9b29/numpy-1.21.5-cp38-cp38-macosx_10_9_x86_64.whl (16.9MB)
     |████████████████████████████████| 16.9MB 25.9MB/s
Collecting pcbnewTransition==0.2.0 (from kikit)
  Using cached https://files.pythonhosted.org/packages/98/cc/2210a2dca06d89c4f87e1c2b6e10d20e8a4d32b9fbe1590e9d188b3123bd/pcbnewTransition-0.2.0-py3-none-any.whl
Collecting markdown2 (from kikit)
  Downloading https://files.pythonhosted.org/packages/ab/36/101b2a0fb8880ae7ea2bbcee5a2f91e74bb58e5f23d0d692020fb07623c6/markdown2-2.4.2-py2.py3-none-any.whl
Collecting click (from kikit)
  Using cached https://files.pythonhosted.org/packages/48/58/c8aa6a8e62cc75f39fee1092c45d6b6ba684122697d7ce7d53f64f98a129/click-8.0.3-py3-none-any.whl
Collecting lark-parser<0.8.0,>=0.7.1 (from commentjson->kikit)
  Using cached https://files.pythonhosted.org/packages/34/b8/aa7d6cf2d5efdd2fcd85cf39b33584fe12a0f7086ed451176ceb7fb510eb/lark-parser-0.7.8.tar.gz
Collecting pypng<0.0.20,>=0.0.19 (from solidpython->kikit)
  Using cached https://files.pythonhosted.org/packages/0e/39/993a5feea8ed9c2eebd70c6e7c20cb4b0823588f5ab0afab4b0be95ebc23/pypng-0.0.19.tar.gz
Collecting regex<2020.0,>=2019.4 (from solidpython->kikit)
  Using cached https://files.pythonhosted.org/packages/fc/a6/2c153ced229ba51f941d15554a66293c9f79e2d5b9a18c1d1b0c52a31a1f/regex-2019.12.20.tar.gz
Collecting euclid3<0.2.0,>=0.1.0 (from solidpython->kikit)
  Using cached https://files.pythonhosted.org/packages/fc/d2/80730bee6b51f2a0faacaec51abb919f144c8b1fff5907fe019ec0e95698/euclid3-0.01.tar.gz
Collecting PrettyTable==0.7.2 (from solidpython->kikit)
  Downloading https://files.pythonhosted.org/packages/ef/30/4b0746848746ed5941f052479e7c23d2b56d174b82f4fd34a25e389831f5/prettytable-0.7.2.tar.bz2
Collecting PyMeta3>=0.5.1 (from pybars3->kikit)
  Using cached https://files.pythonhosted.org/packages/ce/af/409edba35fc597f1e386e3860303791ab5a28d6cc9a8aecbc567051b19a9/PyMeta3-0.5.1.tar.gz
Building wheels for collected packages: commentjson, pybars3, lark-parser, pypng, regex, euclid3, PrettyTable, PyMeta3
  Building wheel for commentjson (setup.py) ... done
  Created wheel for commentjson: filename=commentjson-0.9.0-cp38-none-any.whl size=12085 sha256=65706d92abcc5535e1204e64031edbac4eb971eb84984c23fb41d0325f37565d
  Stored in directory: /Users/tyw/Library/Caches/pip/wheels/0d/18/46/8da734185b844fc754de60da2c162fc15f7acdacfd4621144e
  Building wheel for pybars3 (setup.py) ... done
  Created wheel for pybars3: filename=pybars3-0.9.7-cp38-none-any.whl size=14094 sha256=6eb74205c106ca25a0070dd8af839c5936344287c9fc96257df74f3b9e7fdae8
  Stored in directory: /Users/tyw/Library/Caches/pip/wheels/36/34/84/eb0e44383aa4b6e27bf4b32274284b1909b117c1bf8ca74e79
  Building wheel for lark-parser (setup.py) ... done
  Created wheel for lark-parser: filename=lark_parser-0.7.8-py2.py3-none-any.whl size=62514 sha256=b7d392c624f8343a79adcb6407230854e23406a6d38327d46eac3d52891b8919
  Stored in directory: /Users/tyw/Library/Caches/pip/wheels/01/a2/30/ebae6ffa73cf3aa1c972a24d4c78388afd910f91e43bf554aa
  Building wheel for pypng (setup.py) ... done
  Created wheel for pypng: filename=pypng-0.0.19-cp38-none-any.whl size=53308 sha256=b96c51ca25e23df35aebb036b2f500e9e845e756276dc13acadc02e2b4404500
  Stored in directory: /Users/tyw/Library/Caches/pip/wheels/a4/cd/54/c1d0803dfed5adf2bba530eda161581d9096d0154aee082cac
  Building wheel for regex (setup.py) ... done
  Created wheel for regex: filename=regex-2019.12.20-cp38-cp38-macosx_10_9_x86_64.whl size=281812 sha256=da53096d87df58901c3cf8fb944ba821e32b370f179b192bd8239b1c7cd7bbdf
  Stored in directory: /Users/tyw/Library/Caches/pip/wheels/98/16/eb/136427a471a706ea4847904c4b37c27a21bc3c90763092b67c
  Building wheel for euclid3 (setup.py) ... done
  Created wheel for euclid3: filename=euclid3-0.1-cp38-none-any.whl size=13256 sha256=efc920ec241ac0f694f13c868d7a76be9181c00f0c757d5ac5ffe900c117aef1
  Stored in directory: /Users/tyw/Library/Caches/pip/wheels/15/52/ea/f0bf957951bd8b43c99461ef483bdd799d0e896a4bb99857ac
  Building wheel for PrettyTable (setup.py) ... done
  Created wheel for PrettyTable: filename=prettytable-0.7.2-cp38-none-any.whl size=13701 sha256=38c4e2ba9d00d28aed7efb7bc80c8817851273dc9cd66d6b10369e74c2ec74b7
  Stored in directory: /Users/tyw/Library/Caches/pip/wheels/80/34/1c/3967380d9676d162cb59513bd9dc862d0584e045a162095606
  Building wheel for PyMeta3 (setup.py) ... done
  Created wheel for PyMeta3: filename=PyMeta3-0.5.1-cp38-none-any.whl size=16451 sha256=8e5c85f48c6e3fa8aa68f164b75cbc862a82032b6fe8f53b604288dac0803b9a
  Stored in directory: /Users/tyw/Library/Caches/pip/wheels/ca/39/08/80a225ffde8b15eea306b6d603e3ed4aa44a616f7c2d485c6b
Successfully built commentjson pybars3 lark-parser pypng regex euclid3 PrettyTable PyMeta3
ERROR: wxpython 4.1.1 requires pillow, which is not installed.
Installing collected packages: lark-parser, commentjson, pypng, regex, euclid3, PrettyTable, solidpython, shapely, PyMeta3, pybars3, numpy, pcbnewTransition, markdown2, click, kikit
Successfully installed PrettyTable-0.7.2 PyMeta3-0.5.1 click-8.0.3 commentjson-0.9.0 euclid3-0.1 kikit-1.0.0 lark-parser-0.7.8 markdown2-2.4.2 numpy-1.21.5 pcbnewTransition-0.2.0 pybars3-0.9.7 pypng-0.0.19 regex-2019.12.20 shapely-1.8.0 solidpython-1.1.1
WARNING: You are using pip version 19.2.3, however version 21.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

@a7hybnj2
Copy link
Contributor Author

/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/pip3 -V pip 19.2.3 from /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)

echo $PYTHONPATH

(The above is a blank line)
I removed my kicad install including everything in ~/Library/Preferences and ~/Library/Caches and tried again without installing pillow this time:

/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/pip3 install kikit
Collecting kikit
  Using cached https://files.pythonhosted.org/packages/e2/6a/b67074cc94d4373c7e82b661c59a227fa51fb5251e96a721d375cb91e49a/KiKit-1.0.0-py3-none-any.whl
Collecting shapely (from kikit)
  Using cached https://files.pythonhosted.org/packages/80/4f/d96da54dadc06b3202df977f3721cc72f9f322f0ec5f15dd0baabd5ab365/Shapely-1.8.0-cp38-cp38-macosx_10_9_x86_64.whl
Collecting numpy (from kikit)
  Using cached https://files.pythonhosted.org/packages/e8/b2/be38f1036c7e7f6af79201f09de86615599beeb232f37dd288d9631d9b29/numpy-1.21.5-cp38-cp38-macosx_10_9_x86_64.whl
Collecting click (from kikit)
  Using cached https://files.pythonhosted.org/packages/48/58/c8aa6a8e62cc75f39fee1092c45d6b6ba684122697d7ce7d53f64f98a129/click-8.0.3-py3-none-any.whl
Collecting solidpython (from kikit)
  Using cached https://files.pythonhosted.org/packages/b6/1e/3a065797bd8fe8e2e0ced73e6baf07fdb921fecc04fa18b2b8ffb34f3c87/solidpython-1.1.1-py3-none-any.whl
Collecting markdown2 (from kikit)
  Using cached https://files.pythonhosted.org/packages/ab/36/101b2a0fb8880ae7ea2bbcee5a2f91e74bb58e5f23d0d692020fb07623c6/markdown2-2.4.2-py2.py3-none-any.whl
Collecting commentjson (from kikit)
Collecting pybars3 (from kikit)
Collecting pcbnewTransition==0.2.0 (from kikit)
  Using cached https://files.pythonhosted.org/packages/98/cc/2210a2dca06d89c4f87e1c2b6e10d20e8a4d32b9fbe1590e9d188b3123bd/pcbnewTransition-0.2.0-py3-none-any.whl
Collecting euclid3<0.2.0,>=0.1.0 (from solidpython->kikit)
Collecting pypng<0.0.20,>=0.0.19 (from solidpython->kikit)
Collecting regex<2020.0,>=2019.4 (from solidpython->kikit)
Collecting PrettyTable==0.7.2 (from solidpython->kikit)
Collecting lark-parser<0.8.0,>=0.7.1 (from commentjson->kikit)
Collecting PyMeta3>=0.5.1 (from pybars3->kikit)
ERROR: wxpython 4.1.1 requires pillow, which is not installed.
Installing collected packages: shapely, numpy, click, euclid3, pypng, regex, PrettyTable, solidpython, markdown2, lark-parser, commentjson, PyMeta3, pybars3, pcbnewTransition, kikit
Successfully installed PrettyTable-0.7.2 PyMeta3-0.5.1 click-8.0.3 commentjson-0.9.0 euclid3-0.1 kikit-1.0.0 lark-parser-0.7.8 markdown2-2.4.2 numpy-1.21.5 pcbnewTransition-0.2.0 pybars3-0.9.7 pypng-0.0.19 regex-2019.12.20 shapely-1.8.0 solidpython-1.1.1
WARNING: You are using pip version 19.2.3, however version 21.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

looks just like yours
then

/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/kikit panelize --layout 'grid; rows: 2; cols: 1;' --tabs full --cuts vcuts conn.kicad_pcb panel.kicad_pcb
An error occurred: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.8 from "/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/python3.8"
  * The NumPy version is: "1.21.5"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: dlopen(/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so, 0x0002): tried: '/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so' (code signature in <D6DEC3D7-AF4F-3AA2-B2DD-D6A87AC638BA> '/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so' not valid for use in process: mapped file has no Team ID and is not a platform binary (signed with custom identity or adhoc?)), '/usr/lib/_multiarray_umath.cpython-38-darwin.so' (no such file)

No output files produced

I am using the example board provided by kikit in the doc/resources folder. Could it be the example file? Do you have a file you want to share to rule that out?

@yaqwsx
Copy link
Owner

yaqwsx commented Dec 29, 2021

I am using the example board provided by kikit in the doc/resources folder. Could it be the example file? Do you have a file you want to share to rule that out?

Once you panelize, numpy is imported. So it won't be just this case, but every case. Actually, help imports only click and nothing else (not even the pcbnew module - as it takes quite a long time to load).

@tywtyw2002
Copy link
Contributor

This is MacOS Standard security issue..... signature error...

I do not know how to resolve it. (Long long time ago, I disabled lots of macos security options.)

Hits: search google mapped file has no Team ID and is not a platform binary

Original error was: 

dlopen(/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so, 0x0002): 

tried: '/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so' 

(code signature in <D6DEC3D7-AF4F-3AA2-B2DD-D6A87AC638BA> 

'/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so'

 not valid for use in process: mapped file has no Team ID and is not a platform binary (signed with custom identity or adhoc?)),

 '/usr/lib/_multiarray_umath.cpython-38-darwin.so' (no such file)

@conradopoole
Copy link
Contributor

conradopoole commented Dec 30, 2021

I managed to get the SIP (System Integrity Protection) Mac issue solved, but I had to sign again the Python.app embedded with Kicad.

If you don't have a Apple Developer code signing certificate you can simply create a Self Signed using Keychain like this...

image

Then run

codesign -fs "my-certificate" /Applications/Kicad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/Resources/Python.app

I was then able to run using the embedded python the example panelization command...

/Applications/Kicad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/kikit panelize --layout 'grid; rows: 2; cols: 1;' --tabs full --cuts vcuts <PATH_TO_PCB>/conn.kicad_pcb <OUTPUT_PATH>/panel.kicad_pcb

Technically I believe this should be solved by the Kicad Mac builder, they should probably add the "com.apple.security.cs.disable-library-validation=TRUE" entitlement the the Python.app. The problem is that the Kicad.app is code signed but the minute we run the embedded pip3, files are added that were not part of the original bundle so they cannot be loaded. The issue is solved by resigning the Python.app but ideally we'd like that validation to be disabled.

Summarizing, install KiKit, code sign and then run kikit.

/Applications/Kicad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/pip3 install kikit

codesign -fs "my-certificate" /Applications/Kicad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/Resources/Python.app

/Applications/Kicad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/kikit panelize --layout 'grid; rows: 2; cols: 1;' --tabs full --cuts vcuts <PATH_TO_PCB>/conn.kicad_pcb <OUTPUT_PATH>/panel.kicad_pcb

@conradopoole
Copy link
Contributor

I forgot to mention that even though the panelize command runs and generates the panel, it also outputs some assertion issues:

./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this

@tywtyw2002
Copy link
Contributor

I got same outputs too, but it seems not affect the panelize. It only the message for wxApp.

I forgot to mention that even though the panelize command runs and generates the panel, it also outputs some assertion issues:

./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this

@a7hybnj2
Copy link
Contributor Author

a7hybnj2 commented Dec 30, 2021

@conradopoole sweet! I will try this out this afternoon.

Would this be mitigated if this package were installed through the new kicad plugin/package/theme manager? I know that isn't really how this tool works but would be cool and much easier for non-cl people.

@cdwilson
Copy link

cdwilson commented Dec 30, 2021

When I installed kikit I get a ERROR: wxpython 4.1.1 requires pillow, which is not installed.

@a7hybnj2 I think you can prevent the error from showing up by passing all the required packages to pip on one line (this installs for me without any errors):

/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/python3 -m pip install --upgrade pip numpy pillow kikit

@conradopoole the code signing works for me too. I'm also getting the assertion errors, but it looks like the panelizer is working correctly.

$ /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/kikit panelize --layout 'grid; rows: 2; cols: 2;' --tabs full --cuts vcuts doc/resources/conn.kicad_pcb panel.kicad_pcb
./src/common/stdpbase.cpp(59): assert ""traits"" failed in Get(): create wxApp before calling this

@yaqwsx FYI, when I open pcbnew after installing the KiKit front end from PCM, I get the following error. What is it looking for to determine if there is a backend installed?

image

@tywtyw2002
Copy link
Contributor

@cdwilson On my side, the kicad kikit plugin work perfect.

@yaqwsx For macos, when you enable the plugin, the script should in /Users/xxx/Documents/kicad/6.0/scripting/plugins/ instead of /Users/xxxx/.local/share/kicad/6.0/scripting/.

Following steps

  • enable plugin by kikit-plugin
/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/kikit-plugin enable panelize hideReferences
File '/Users/xxxx/.local/share/kicad/6.0/scripting/kikit_plugin.py' was created
  • Mv kikit_plugin to kicad plugin folder.
mv /Users/xxxx/.local/share/kicad/6.0/scripting/kikit_plugin.py /Users/xxx/Documents/kicad/6.0/scripting/plugins/kikit_plugin.py

PS: Fingers crossed and good luck

@conradopoole
Copy link
Contributor

conradopoole commented Dec 31, 2021

@tywtyw2002 Did you do any additional code signs? For me the Panelize plugin does not work either from the GUI (it does not load with same message as @cdwilson ) unless a fully code sign the full Kicad.app (with --deep), then it loads

codesign --deep -fs "my-certificate" /Applications/Kicad/KiCad.app

I figured out it was a signing issue again when from the Script console in Pcbnew I tried to run the same import the kikit_plugin.py does. Doing a :

from kikit.plugin import panelize

Resulted in the same signing error about the Team ID blah blah blah .... Only way around it for me was to fully code sign the whole app like explained above. @cdwilson let me know if this works for you as well

One side effect is that if I fully code sign Kicad.app then the kikit does not work for me from the command line.... But I rather have the GUI plugin working on my Mac and then if I want to run the CLI I just then a docker image instead. (@yaqwsx started pushing the M1 docker image as well to docker hub, if using an Intel Mac then the standard x64 image works).

@tywtyw2002
Copy link
Contributor

tywtyw2002 commented Dec 31, 2021

NO, I disabled SIP and other security features since day 1.

I think that is called Disable Gatekeeper.

If you know what software you've running, disable security features make life easier.

@Robomaticii
Copy link

Oh! and there it is. Guess you can see my user name is "chimney". Forgot to replace it with a generic in one step.

Hope the above helps.

Robert AKA Robomaticii

@Gasman2014
Copy link
Contributor

Gasman2014 commented Feb 2, 2022 via email

@themaninthesuitcase
Copy link

Last step is to sign the KiCad.app.
codesign -fs my-certificate /Applications/Kicad/KiCad.app

This was the missing secret sauce for me.

@yaqwsx
Copy link
Owner

yaqwsx commented Feb 9, 2022

Hi! I see that you made a progress with this issue. Would you mind writing a step-by-step guide and submitting it as a pull request to the documentation?

PS: Is there anything we could ask from the KiCAD developers to make the whole process easier? I find the current situation on Mac really bad, however, since I am not a Mac use I am not sure if I fully understand what needs to change.

@Robomaticii
Copy link

Robomaticii commented Feb 9, 2022 via email

sslupsky added a commit to sslupsky/KiKit that referenced this issue Feb 22, 2022
Install the KiKit backend into a virtual environment.
For example:
python3 -m venv <path_to_kikit_PCM_directory>

This installs a virtual environment in the KiKit plugin directory.
Activate the environement and install
the kikit backend.

This commit tells the plugin to recognize the virtual environment.
@sslupsky
Copy link

sslupsky commented Feb 22, 2022

I think it Is possible to solve this issue using a virtual environment. I did some investigation and it is possible to install the KiKit backend into a virtual environment that is created from the KiCad python. For example:

/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/python3 -m venv /Users/stevenslupsky/Documents/kicad/6.0/3rdparty/plugins/com_github_yaqwsx_kikit 

This installed a virtual environment in the KiKit plugin directory. I can now open a terminal and activate that python environment and run KiKit:

source /Users/stevenslupsky/Documents/kicad/6.0/3rdparty/plugins/com_github_yaqwsx_kikit/bin/activate

And I can confirm that KiKit is running properly:

(com_github_yaqwsx_kikit) barbie-en0:~ stevenslupsky$ which python3
/Users/stevenslupsky/Documents/kicad/6.0/3rdparty/plugins/com_github_yaqwsx_kikit/bin/python3
(com_github_yaqwsx_kikit) barbie-en0:~ stevenslupsky$ which kikit
/Users/stevenslupsky/Documents/kicad/6.0/3rdparty/plugins/com_github_yaqwsx_kikit/bin/kikit
(com_github_yaqwsx_kikit) barbie-en0:~ stevenslupsky$ kikit --version
kikit, version 1.0.2

The only remaining issue is to get the plugin to recognize the virtual environment. I did this by adding the path to the site directory in the plugin (kikit/plugins/init.py):
(https://github.com/sslupsky/KiKit/blob/0e9d539789b57712be53f4cbf2a91552bea0bbff/pcm/kikit/plugins/__init__.py#L41-L47)

And this appears to work without having to code sign the KiCad app in macOS. Of course, this is specific to my particular installation so the proposed solution here requires a little more work to extract the proper path from the virtual environment and add it to the site package search list.

@sslupsky
Copy link

There is another way to accomplish the same thing without the change described above. Create a file "kicad.pth" with the following:

# kicad 3rdparty package configuration

/Users/stevenslupsky/Documents/kicad/6.0/3rdparty/plugins/com_github_yaqwsx_kikit/lib/python3.8/site-packages

Use the path to the directory you created the virtual environment you installed KiKit to.

Place the file in the following directory:

/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages

Viola.

@conradopoole
Copy link
Contributor

There is another way to accomplish the same thing without the change described above. Create a file "kicad.pth" with the following:

# kicad 3rdparty package configuration

/Users/stevenslupsky/Documents/kicad/6.0/3rdparty/plugins/com_github_yaqwsx_kikit/lib/python3.8/site-packages

Use the path to the directory you created the virtual environment you installed KiKit to.

Place the file in the following directory:

/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages

Viola.

I repeated your steps, both with the fix for init.py or the kicad.pth file, and in my case it still does not work unless I resign the Kicad.app . It does install kikit in the virtual environment but the minute the app tries to load dependencies that load SO objects that were not in the original signature, it fails. Same issue as before, only that it now complains about paths in the virtual environment. An easy easy way to reproduce it is to launch the KyPython shell and run

from kikit.plugin import importAllPlugins

It fails with the same error as always due to signatures... See below...

image

Did you make any changes to your Notarization settings?

@sslupsky
Copy link

sslupsky commented Mar 10, 2022

@conradopoole What machine are you running? I found things a little different if you are running on Apple Silicon.

The root cause is the numpy package. The numpy package has a few binary libraries that cause the problem as a result of gatekeeper Library Validation. Library Validation fails for any binaries that are not signed with the same Team ID unless Library Validation is disabled. This is strictly enforced on Apple Silicon macs but apparently not on Intel macs. This is why I noticed a difference in behaviour between the two architectures.

Regarding recreating the problem it is even simpler to do, just attempt to import numpy.

The solution to this problem requires that @yaqwsx include with KiKit the packages used by KiKit that are not included with the KiCad app bundle site-packages. So at a minimum, @yaqwsx will need to include numpy because numpy is not included in the KiCad python site-packages. Then, the KiKit plugin package, including the numpy package used by KiKit, needs to be signed by KiCad. Thus, when the package is installed, it will pass library validation since both the plugin libraries and the python binaries are signed by KiCad. I posted an issue on the KiCad gitlab repo and briefly discussed it with the KiCad team. I think the KiCad developers are likely reluctant to disable library validation. So, I think signing the plugin is the most appropriate solution.

It is possible to work around this for the moment but it requires the manual steps generally described here. So, regarding your specific issue, are you running on Intel or Apple Silicon? I have it working on both. If running on Apple Silicon, you do need to re-sign the python binaries and the numpy libraries using your own certificate.

I also found that the KiCad app bundle python creates cache files inside the app bundle. From the work I did, I found that using PYTHONPATH and PYTHONPYCACHEPREFIX environment settings configured the python environment properly so python could find the site-packages directory for kikit and not pollute the KiCad app bundle with the cache files. I installed kikit and the kikit dependencies in my own directory outside the KiCad app bundle. When I used these environment variables, I did not have to make the change to init.py and I did not require a .pth file.

I will see if I can put together a step by step set of instructions of how to do all of this.

@sslupsky
Copy link

Here is the script I use to sign the proper items in the KiCad app bundle:

codesign --verbose=4 --force --sign "<put your certificate here>" /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Resources/Python.app
codesign --verbose=4 --force --sign "<put your certificate here>" /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/python3.8
codesign --verbose=4 --force --sign "<put your certificate here>" /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/Python
codesign --verbose=4 --force --sign "<put your certificate here>" /Applications/KiCad/KiCad.app

I set the following environment variables in the "Preferences->Configure Paths" dialog:

PYTHONPATH /Users/stevenslupsky/Documents/KiCad/6.0/3rdparty/plugins/com_github_yaqwsx_kikit/lib/python3.8/site-packages
PYTHONPYCACHEPREFIX /Users/stevenslupsky/Documents/KiCad/6.0/3rdparty/cache
PYTHONUSERBASE /Users/stevenslupsky/Documents/KiCad/6.0/3rdparty/plugins/com_github_yaqwsx_kikit

Since the numpy libraries are signed ad-hoc they will run if the python binaries are signed by you. I don't think the PYTHONUSERBASE setting is required since I added the PYTHONPATH setting.

@secolive
Copy link

secolive commented Mar 15, 2022

Hi there, I have been experiencing the same difficulties while trying to install another tool (pcbdraw), which also uses numpy. Following the advice in this thread, I came up with the following plan (for reference).

  1. create a new virtualenv based on KiCad's python
  2. "link" KiCad's python to this new virtualenv (technically: include the virtualenv's site-packages into KiCad's)
  3. resign KiCad
  4. install whatever is needed in this virtualenv

I created the following script to perform steps 1-3:

#!/bin/zsh

set -e -u

SCRIPT_DIR="$( dirname "$0" )"
SCRIPT_DIR="$( cd "$SCRIPT_DIR" && /bin/pwd )"

head1() {
  printf "\n"
  printf "%s\n" "$*" | sed 'h;s/./=/g;H;G;' 1>&2
}
notice() {
  printf "%s\n" "$*" 1>&2
}

V=3.8
KICAD_PYTHON="/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/$V"
CERTIFICATE="user@hostname CodeSigning"
PYENV_DIR="$SCRIPT_DIR/PyEnv"

head1 "Parameters"
echo "Kicad Python dir: $KICAD_PYTHON" 1>&2
echo "Python version: $V" 1>&2
echo "VirtualEnv dir: $PYENV_DIR" 1>&2
echo "Signinf certificate: $CERTIFICATE" 1>&2


head1 "Creation of new virtualenv"

notice "+ Create virtualenv"
"$KICAD_PYTHON/bin/python3" -m venv "$PYENV_DIR"

notice "+ Add KiCad's site-packages to virtualenv"
ls -d1 "$KICAD_PYTHON/lib/python$V/site-packages" > "$PYENV_DIR/lib/python$V/site-packages/KiCad.pth"


head1 "Update of KiCad"
notice "+ Add virtualenv's site-packages to KiCad's"
ls -d1 "$PYENV_DIR/lib/python$V/site-packages/" > "$KICAD_PYTHON/lib/python$V/site-packages/KiCad_plugins.pth"

notice "+ Resign KiCad"
codesign -fs "$CERTIFICATE" "$KICAD_PYTHON/Resources/Python.app"
codesign -fs "$CERTIFICATE" "/Applications/KiCad/KiCad.app"

One of the tricks is that each python environment references the other's site-packages to make things easier.

Now I can use this virtualenv to install additional software:

. PyEnv/bin/activate
pip3 install -U pip pillow numpy pcbdraw kikit

And running python I can import everything successfully:

$ python3
Python 3.8.2 (v3.8.2:7b3ab5921f, Feb 24 2020, 17:52:18)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pcbnew
>>> import numpy
>>>

It remains to be seen if this strategy works in the medium run (e.g. facing KiCad updates).

@yaqwsx
Copy link
Owner

yaqwsx commented Mar 15, 2022

Just for reference - the related KiCAD issue: https://gitlab.com/kicad/code/kicad/-/issues/10943

@sslupsky
Copy link

sslupsky commented Mar 15, 2022

@secolive I believe you are missing one of the python binaries:

codesign --verbose=4 --force --sign "<put your certificate here>" /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/python3.8

Using a virtual environment is the proper way to ensure you can run kikit from the command line properly. One thing to remember though is that the python bundled with KiCad does not use the virtual environment when run from within KiCad. So I recommend using the environment variables I describe in my earlier post. These variables ensure that when the kikit plugin is run from pcbnew, it has the correct environment configuration.

Fortunately, using the virtual environment will ensure the site-packages are retained across KiCad updates. However, in order to use the site packages not included with KiCad, the KiCad app and python binaries have to be resigned each time KiCad is updated.

@yaqwsx
Copy link
Owner

yaqwsx commented Mar 21, 2022

@secolive @sslupsky: Is the script fully automatic? If so, could I make it part of KiKit's repository and link to in the KiKit's instructions. I don't mind if KiKit would have to be installed after every KiCAD's update.

At the moment, with the setup provided by KiCAD 6, I see this as the only viable solution.

@sslupsky
Copy link

@yaqwsx I think it Is close. The user will have to set a couple environment variables in their Kicad configuration.

@secolive
Copy link

@secolive @sslupsky: Is the script fully automatic? If so, could I make it part of KiKit's repository and link to in the KiKit's instructions. I don't mind if KiKit would have to be installed after every KiCAD's update.

At the moment, with the setup provided by KiCAD 6, I see this as the only viable solution.

Kind of - the variables V and KICAD_PYTHON have to match the details of the KiCad installation. CERTIFICATE has to be changed to the name of the certificate you'll use for codesigning. The python virtual env will be created as a subdirectory of where the script is located, meaning you have to decide on a proper location - I went for ~/Documents/KiCad/6.0, to be seen in practice if this is the right thing to do.

Also, note that the actions being carried out are rather simple and could be part of a simple documentation. Still, I see this as a kind of workaround, and am not 100% sure it's the proper way to go.

@yaqwsx
Copy link
Owner

yaqwsx commented Mar 22, 2022

@sslupsky: If you need to override some variable, you can do so in the file KiCAD_CONFIG_HOME/6.0/kicad_common.json.
@secolive: I agree that it is more-less a work-around. But there is no possibility to change this in v6. I prefer to have at least a step-by-step guide for the Mac users or preferably a script so they can use KiKit. Would you be interested in providing one of them?

@ebuswell
Copy link

I just want to confirm that the instructions from secolive work on Intel Big Sur. I didn't need any extra environment variables or to re-sign the python binary separately from Python.app. Here's what I did. This is pretty much exactly what secolive wrote, including a few more steps that might not be obvious to a non-MacOS-developer.

(1) Create a code signing key if you don't have one
(1a) Open Keychain Access
(1b) Keychain Access->Certificate Assistant->Create a Certificate
Name->Anything, e.g. "kkworkaroundssc"
Identity Type->Self-Signed Root
Certificate Type->Code Signing
(2) Create a virtual environment sharing packages with KiCad in some directory, e.g. ~/kkvenv
(2a) Open a shell
(2b) /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/bin/python3 -m venv ~/kkvenv
(2c) ls -d1 /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python/3.8/site-packages > ~/kkenv/lib/python/3.8/site-packages/KiCad.pth
(2d) ls -d1 ~/kkenv/lib/python/3.8/site-packages > /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/lib/python/3.8/site-packages/KiKitVenv.pth
(3) Re-sign Python + KiCad & co.
(3a) codesign -fs kkworkaroundssc /Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/3.8/Resources/Python.app
(3b) codesign -fs kkworkaroundssc /Applications/KiCad/KiCad.app
(3c) codesign -fs kkworkaroundssc /Applications/KiCad/Pcbnew.app (only necessary if you want to use Pcbnew standalone)
(4) Install the KiKit command line
(4a) Enter the venv: . ~/kkenv/bin/activate
(4b) pip3 install -U pip pillow numpy pcbdraw kikit
(5) Open the KiCad gui and install the KiKit plugin

To use kikit via the command line, you always need to first activate the virtual environment with:
. ~/kkenv/bin/activate

Since there's a whole lot of other stuff in this thread that's like "you should maybe do this" or "this worked for me"—just to be clear, I didn't do any of that other stuff. This is with a clean KiCad install and more or less the MacOS default set of environment variables. Of course, very possibly some of it is necessary with slightly different setups, ¯\_(ツ)_/¯.

@sslupsky
Copy link

@yaqwsx 6.0.4 fixed one of the python problems and the PYTHONPYCACHEPREFIX variable is no longer required.

@yaqwsx
Copy link
Owner

yaqwsx commented May 19, 2022

I finally got access to a Mac (thanks, @JakubAndrysek!) and I made an installation guide: https://github.com/yaqwsx/KiKit/blob/master/doc/macosInstallation.md

Please, give it a try.

@ebuswell
Copy link

Just installed 6.0.5 and used the guide. Works for KiCad.app, but you need to also sign at least pcbnew.app for standalone use.

@yaqwsx
Copy link
Owner

yaqwsx commented May 23, 2022

If you can submit a PR for signing pcbnew, I will be happy to merge it. At the moment, I have no way to validate the changes at the moment as I don't have access to Mac.

@a7hybnj2
Copy link
Contributor Author

a7hybnj2 commented Jun 1, 2022

Hey @yaqwsx we just spoke in #kicad and the path is /Applications/KiCad/KiCad.app/Contents/Applications/pcbnew.app but I don't know exactly what to do with that or how to PR.

@a7hybnj2
Copy link
Contributor Author

a7hybnj2 commented Jun 1, 2022

Works on 6.0.5 with the extra codesign of /Applications/KiCad/KiCad.app/Contents/Applications/pcbnew.app THANKS!

#364

@yaqwsx
Copy link
Owner

yaqwsx commented Jun 1, 2022

Does any of the Mac users here have any idea what might be wrong in #363?

@yaqwsx
Copy link
Owner

yaqwsx commented Aug 17, 2022

We have at least five successful installations on macOS using the new instructions. I consider this issue resolved. If you find new problems on mac, please, feel free to open a new issue.

@yaqwsx yaqwsx closed this as completed Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests