Message Boards Message Boards

0
|
5021 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Registering Python External Evaluators from Microsoft Store

Posted 5 years ago

With the recent release of Python 2 and 3 in the Microsoft Store coinciding with the Windows 10 1903 update, I decided to give those a crack and see if they are worth ditching my hacky and delicate setup based on installs from python.org.

Overall, they work exactly as expected, and will provide a superior experience for most users over managing and maintaining their own installs, but YMMV as always.

The one issue I encountered is that Mathematica and WolframEngine won't detect them as external evaluators for Python, despite them being in the PATH. The reason for this has to do with how Microsoft Store apps work (the binary in your PATH is actually a symlink/reparse-point that points to the package install location, and Mathematica can't handle following those links), but the short answer is that it is pretty easy to work around.

I've got a one-liner in a GitHub gist. I've reproduced it here, but the gist will be the authoritative version I update as necessary.

RegisterExternalEvaluator["Python",
    StringTrim[#]<>"\\python.exe"]&/@
        StringSplit[RunProcess[{
                "powershell.exe",
                "(Get-AppxPackage | where {$_.name -match 'PythonSoftwareFoundation.Python.[23]'}).InstallLocation"},
            "StandardOutput"],
        "\n"]

The trick is to use Powershell to find all installed Store packages (Get-AppXPackage) that match the package name pattern of the Python packages with a regex, and then look at where they are installed. Once we have the install location, just add python.exe to that location, and register!

Now when we run Python code, we see exactly what we expect:

In[]:= ExternalEvaluate["Python", "import sys;[sys.version,sys.executable]"]

Out[]= {
    "3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:13:57) [MSC v.1916 64 bit (AMD64)]",
    "C:\\Users\\Prima\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\\python.exe"}

If you are like me, and get a bit over-eager with removing old Python installations registered as external evaluators, and find that your external evaluator list is looking a bit cluttered, you can always head over to

C:\Users\${USERNAME}\AppData\Roaming\Wolfram\Objects\Persistence\ExternalEvaluate%60EvaluatorCache

to clear out the cache. Either delete that whole folder and let it start fresh, or just remove the specific entries in put.wl.

2 Replies
Posted 4 years ago

I have uninstalled my Microsoft store accidentally and now I want to reinstall it. Is there any other way to register python external evaluator? I am trying to reinstall windows store with the help of https://windowsclassroom.com/reinstall-windows-store/ but if I could not install it then how can I register it without Windows store?

POSTED BY: Lina hadley

You can always install Python directly from the official downloads. I recommend the newest 3.7 build, as 3.8 introduces changes that in my experience break Mathematica's external evaluation (might be fixed in 12.1, but I haven't tried).

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract