14

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.

Edmund
  • 42,267
  • 3
  • 51
  • 143
  • 1
    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 – b3m2a1 Apr 08 '20 at 07:08
  • @b3m2a1 That is good information. Please add an answer. – Edmund Apr 08 '20 at 11:23

2 Answers2

11

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:

M.R.
  • 31,425
  • 8
  • 90
  • 281
  • 1
    This is cool! How'd you get around this: https://mathematica.stackexchange.com/questions/167605/externallanguage-cell-with-externalsessionobject/167634?noredirect=1#comment595031_167634 – b3m2a1 Dec 11 '20 at 00:29
  • 1
    @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. – M.R. Dec 11 '20 at 01:20
  • 1
    Great find. (+1) – Edmund Dec 11 '20 at 13:26
  • 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. – Kvothe Apr 22 '21 at 13:32
  • @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: https://www.wolframcloud.com/obj/resourcesystem/published/FunctionRepository/resources/SetLanguageCellSession – M.R. May 11 '21 at 23:07
  • I have 12.1.1.0 and get an error when try to execute s = ResourceFunction["SetLanguageCellSession"][] Do I need 12.2 for this function to work? – Jozef Pulko Sep 06 '21 at 14:02
  • Before that works you need to do two things 1. "pip install zmq" in the conda environment 2. register the python executable for that env with RegisterExternalEvaluator – M.R. Sep 06 '21 at 16:42
7

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)
sunt05
  • 4,367
  • 23
  • 34
  • 2
    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) – Edmund Apr 07 '20 at 22:26
  • @Edmund seems like so; but I fully agree with you that this should be more explicitly customisable or documented. – sunt05 Apr 07 '20 at 22:28