14
$\begingroup$

This example is for Python but I would like to set this option for any external language.

I have a couple of versions of Python installed.

FindExternalEvaluators["Python"][All, "Version"]

enter image description here

When I create an external language code cell and evaluate code the Python version is not the one I need.

Shift + >

<Python> 1+1

Then

Through@Select[ExternalSessions[], #["System"] == "Python" &]@"Version"
{"3.6.3"}

However, I would like the Python external code cells to use version 3.8.2. Where is this option set? I have not noticed anything it the Edit | Preferences dialogue.

$\endgroup$
2
  • 1
    $\begingroup$ If you look at ExternalEvaluate`FE`ExternalCellEvaluate which is how this is implemented, you see that it starts a session via StartExternalSession[lang] so you could try to mess with that or you can specify a specific session to use by setting the appropriate key in ExternalEvaluate`FE`$CellSessions which is an Association mapping language names to sessions $\endgroup$
    – b3m2a1
    Commented Apr 8, 2020 at 7:08
  • $\begingroup$ @b3m2a1 That is good information. Please add an answer. $\endgroup$
    – Edmund
    Commented Apr 8, 2020 at 11:23

2 Answers 2

7
$\begingroup$

This may be realised by manipulating the registered external bundles.

Based on the OP's scenario, one can simply do the following:

RegisterExternalEvaluator["Python", "path/to/python3.8.2"]

Then Python3.8.2 would appear at the top of the external evaluators:

FindExternalEvaluators["Python"][All, "Version"]

Once the kernel is restarted, the notebook external language cell will default to the 3.8.2 kernel:

import sys;(sys.executable, sys.version)
$\endgroup$
2
  • 2
    $\begingroup$ So it is purely the order in which the external evaluators appear in FindExternalEvaluators? I hope WRI adds some settings to Edit | Preferences to control this. (+1) $\endgroup$
    – Edmund
    Commented Apr 7, 2020 at 22:26
  • $\begingroup$ @Edmund seems like so; but I fully agree with you that this should be more explicitly customisable or documented. $\endgroup$
    – sunt05
    Commented Apr 7, 2020 at 22:28
11
$\begingroup$

A ResourceFunction called SetLanguageCellSession gives a complete solution for managing external language sessions in Python, Julia, R, etc.

Basic example:

enter image description here

This solution works in 12.1+ and so answers these posts as well:

$\endgroup$
7
  • 1
    $\begingroup$ This is cool! How'd you get around this: mathematica.stackexchange.com/questions/167605/… $\endgroup$
    – b3m2a1
    Commented Dec 11, 2020 at 0:29
  • 1
    $\begingroup$ @b3m2a1 I went spelunking through "ExternalEvaluate" private context. Behind the scenes, the external cell will look for the first ExternalSession object with the name "DefaultXXXSession" :) Thanks for the reminder, I'll mention this in that question as well. $\endgroup$
    – M.R.
    Commented Dec 11, 2020 at 1:20
  • 1
    $\begingroup$ Great find. (+1) $\endgroup$
    – Edmund
    Commented Dec 11, 2020 at 13:26
  • $\begingroup$ This does not seem to currently work on "12.1.1 for Linux x86 (64-bit) (June 19, 2020)". In a new notebook I run: s = StartExternalSession["Python"] and then ResourceFunction[ "https://www.wolframcloud.com/obj/mikesollami/DeployedResources/\ Function/SetLanguageCellSession"][s] as in the example in the documentation. This results in the error Part 1 of {} does not exist. followed by more errors. $\endgroup$
    – Kvothe
    Commented Apr 22, 2021 at 13:32
  • $\begingroup$ @Kvothe that is an old resource, Make sure your have configured a valid python target by doing pip install pyzmq and then it should work: wolframcloud.com/obj/resourcesystem/published/… $\endgroup$
    – M.R.
    Commented May 11, 2021 at 23:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.