For me, using the CMD-line in the Python Console is bothersome so, for non-Kicad-specific App’s, I prefer to build the Python code as an executable Stand-Alone-App and Run it (call it) from a Kicad Plugin.
So, first step is to build the code so it runs by using the Simple Plugin code setup (search for the Documentation). And, place your .py in the Scripting>Plugin folder (add an Icon to it or it gets a default icon). If it does the stuff on the PCB you want, continue…
It should run. If successful, you can build an Executable App by various means - I’ve used several but, prefer PyInstaller (note the Uppercase letters - there’s a bad look-alike pyinstaller (same name but Not the same code).
Once successful making it a Stand-Alone App, you can run it from a plugin by following my post(s) here. If you scroll through the full post, you’ll see link to my Colorizer App that contains a Plugin Folder (it contains the needed code but, you may need to Tweak it per the discussion so it runs on your OP system…)