Run python scripts without creating a plugin

Sorry guys, I read several threads and tutorials but I don’t understand how to achieve a very simple thing.

Right now I don’t want to create a plugin, I just wrote few simple scripts and I want to manually execute them when needed. I opened the KiPython console and I loaded my script (placed in my own folder, I don’t want to place it inside the Kicad directories).

But I cannot find the “run” button! How to run the loaded file?

1 Like

If you loaded (imported?) it than you already “ran” it.
If there is specific function that you need to call then call it from the console. Without showing your code and what you did in the console it’s hard to give more specific help.

But a short python primer that can give you a push in the right direction:

Say you have a python file with following contents:

import pcbnew


def myfunction():


When you do import myscript in python console that file runs (assuming it’s on the classpath and python can find it, it will yell at you if it can’t). All of the statements in the file execute, including defining a function myfunction and printing startup to the console.
If after that you want to actually run myfunction then you need to type myscript.myfunction() to call it, you will see myfunction printed in the console.

I did the following:

  1. run pcbnew
  2. Tools > Scripting Console
  3. File > Open

now I’m in this situation:

I’m not able to run this file! I feel dumb, this window reminds me the Python IDLE but here I don’t see any button or menu items to run the file I loaded!

If it’s not possible to run here I don’t get the purpose of this GUI. If it was just for typing some commands why the Open menu item?

Anyway, I also tried to place my script inside ~/.local/share/kicad/6.0/scripting/plugins as stated here.

But even after issuing a Refresh plugin my script does not show under Tools > External plugins.

On your screenshot you simply opened the file in simple built in editor. It’s not supposed to do much beyond simple text editing and highlight, it’s not an IDE.

To run the script you need to import it in the *Shell* tab which is the actual console.

Only actual plugins will show up in the menu. The link you posted skips a lot of important stuff, see the official kicad docs PcbNew Plugins | Developer Documentation | KiCad if you want to write actual plugin.


If you have a simple function or several simple functions which you want to run only every now and then, you can put the function definitions in the startup file. In the Shell tab you should be able to see where the startup script is located. Just open it with a text editor and add the function definitions there. Then you can call them in the Shell tab as normal python functions.

That’s the answer. I didn’t understand that :frowning:

Yeah but the first tab, the shell/console is fairly powerful. It gives you live inspection tools for all variables/modules etc. You can see source code and comments. Play with it.

It’s a good idea, indeed.
Anyway, I don’t like to mess with the installation directories or files because after updates or os reinstalling it’s hard to remember all the changes I made. I prefer to keep all my files (like libraries, 3d models, etc…) in my own directories.

All installation files which shouldn’t be touched are in the locations which are meant to be writable for the system admin only, the startup file is in your personal config and isn’t overridden or touched by sw updates.


One more note. When you import a file (a python “module”), its content is evaluated and the functions can be used afterwards, but the parts which aren’t inside class or function definition but are directly in the top level are run immediately. So, you shouldn’t put the content like you did in your first screenshot. Instead, use a function definition and don’t call it in that file (in top level; it can of course be called by other functions). Call it only from the Shell.

Yes now it makes sense. Before I wrote the file in that way intentionally, because I thought the console like an IDE (i.e. IDLE) so the code would run when I click the (non-existent) run button!

I always run my python scripts outside of the pcbnew GUI in my IDE of choice (i.e. vscode at the moment).

Interesting… how do you refer to the current file then?

I just pass the filename as a command-line argument to the python script. Perhaps I am misunderstanding your question? :thinking:

Ah ok. It’s just annoying :slight_smile: I thought I could use the same GetBoard() function to retrieve the current board without specify the whole path.

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