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

Python can't find KiCad python libs. #119

Closed
CapnKernel opened this issue Sep 29, 2022 · 7 comments
Closed

Python can't find KiCad python libs. #119

CapnKernel opened this issue Sep 29, 2022 · 7 comments

Comments

@CapnKernel
Copy link

CapnKernel commented Sep 29, 2022

Thank you so much for PcbDraw and JlcParts!

I'm running KiCad 6.0.7 in Fedora Linux 36. I have created a virtual env with python -m venv venv, activated the venv, and run pip install PcbDraw. When I run pcbdraw --help, I get the message ModuleNotFoundError: No module named 'pcbnew'. I'm guessing this is because my invocation of python can't find KiCad's Python libraries.

If I start Python in this venv, this is the value of sys.path:

['', '/usr/lib64/python310.zip', '/usr/lib64/python3.10', '/usr/lib64/python3.10/lib-dynload', '/home/mjd/git/ozhelm/venv/lib64/python3.10/site-packages', '/home/mjd/git/ozhelm/venv/lib/python3.10/site-packages']

If I start Python via KiCad (ie, the KiPython shell), I get this value for sys.path:

['', '/usr/lib/python3.10/site-packages', '/usr/lib64/python310.zip', '/usr/lib64/python3.10', '/usr/lib64/python3.10/lib-dynload', '/usr/lib64/python3.10/site-packages', '/usr/share/kicad/scripting', '/usr/share/kicad/scripting/plugins', '/home/mjd/.local/share/kicad/6.0/scripting', '/home/mjd/.local/share/kicad/6.0/scripting/plugins', '/usr/share/kicad/scripting']

I don't know if this is what should be picked up, but there's a pcbnew in /usr/lib/python3.10/site-packages/.

Here's the list of files in this version of Fedora's KiCad:

https://rpmfind.net/linux/RPM/fedora/updates/36/x86_64/Packages/k/kicad-6.0.7-1.fc36.x86_64.html

KiPython correctly picks up /usr/lib/python3.10/site-packages.

How can I run pcbdraw so pcbnewTransition/transition.py finds pcbnew?

@CapnKernel
Copy link
Author

Can I get some help on this please? Thank you.

@CapnKernel
Copy link
Author

Would really love some help

@yaqwsx
Copy link
Owner

yaqwsx commented Oct 9, 2022

Can you import pcbnewTransition from the python REPL inside pcbnew?

@CapnKernel
Copy link
Author

I tried starting a shell in pcbnew using Tools > Scripting Console.

Python 3.10.7 (main, Sep  7 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
Startup script executed: /home/mjd/.config/kicad/6.0/PyShell_pcbnew_startup.py
>>> 
>>> import pcbnewTransition
Traceback (most recent call last):
  File "/usr/lib64/python3.10/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
ModuleNotFoundError: No module named 'pcbnewTransition'
>>> 
>>> import sys
>>> sys.path
['', '/usr/lib/python3.10/site-packages', '/usr/lib64/python310.zip', '/usr/lib64/python3.10', '/usr/lib64/python3.10/lib-dynload', '/usr/lib64/python3.10/site-packages', '/usr/share/kicad/scripting', '/usr/share/kicad/scripting/plugins', '/home/mjd/.local/share/kicad/6.0/scripting', '/home/mjd/.local/share/kicad/6.0/scripting/plugins', '/usr/share/kicad/scripting']
>>> 

pcbnew was started from the KiCad main shell, which was started from my desktop's start menu.
pcbdraw is installed in a venv inside the directory of the project KiCad currently has open. There's a venv/lib/python3.10/site-packages/pcbnewTransition/__init__.py file in that venv.

I don't know how to get the python interpreter I start in the venv outside of KiCad to know about KiCad's libraries, and I don't know how to get the python interpreter I start from pcbnew to know about the pcbdraw libraries.

@yaqwsx
Copy link
Owner

yaqwsx commented Oct 10, 2022

I am actually not sure if that's possible to have venv and KiCAD at the same time (I am not saying it is possible, I just don't know).

@CapnKernel
Copy link
Author

Ahh I see. Your one line message gave me the clue I needed to find other places on the internet that have talked about this problem.

The solution is to create the venv with the --system-site-packages flag, ie:

python -m venv --system-site-packages venv

Then python started in the venv will look at the packages in the system location, which is where KiCad puts its python code.

Hope this helps someone else!

@yaqwsx
Copy link
Owner

yaqwsx commented Oct 24, 2022

Thanks for sharing the solution.

@yaqwsx yaqwsx closed this as completed Oct 24, 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

2 participants