Skip to content

Instantly share code, notes, and snippets.

@Riebart
Last active September 2, 2023 04:16
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Riebart/7622c17237a6ddb2516fd09ea67307ff to your computer and use it in GitHub Desktop.
Find all installed Python packages from the Microsoft store, determine install location, and register them as external evalautors.
(* Run this every time the Python from the Store updates, since the binary location encodes the store full version *)
(* Note that Python 3.9 is supported in Mathematica 12.3.1, so the below patches are not relevant on modern versions, but are retained to aid those running older versions from perpetual licenses *)
(* Update to support Mathematica 12, which requires that Python >=3.8 at least reference python3.8.exe, not python.exe. *)
(* Note, python >3.7 is not officially supported in Mathematica 12.0.0, and other notebook interfaces.
Attempts to use it result in a "required field 'type_ignores' missing from module" error
Source: https://mathematica.stackexchange.com/questions/211984/solved-externalevaluatepysys-pycmd-evaluates-to-failure *)
(*
A patch to C:\Program Files\Wolfram Research\Mathematica\12.0\SystemFiles\Links\WolframClientForPython\wolframclient\utils\externalevaluate.py to enable 3.8:
66c66
< exec(compile(ast.Module(expressions, []), '', 'exec'), current)
---
> exec(compile(ast.Module(expressions), '', 'exec'), current)
*)
RegisterExternalEvaluator["Python",
StringTrim[#] <>
StringCases[#, {___ ~~ "PythonSoftwareFoundation.Python." ~~ M_ ~~
"." ~~ m : RegularExpression["[^_]*"] -> {"\\python", M, ".",
m, ".exe"}}]] & /@
StringSplit[
RunProcess[{"powershell.exe",
"(Get-AppxPackage | where {$_.name -match \
'PythonSoftwareFoundation.Python.[23]'}).InstallLocation"},
"StandardOutput"], "\n"]
@Riebart
Copy link
Author

Riebart commented Sep 2, 2023

Updated to support Python 3.10+, as the script assumed the minor version was a single character.

You can find the list of ExternalEvaluators at %APPDATA%\Roaming\Wolfram\Objects\Persistence\ExternalEvaluate%60EvaluatorCache%60Python\put.wl and you can delete that file before running this to remove the old one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment