Changing Python Default Version in the External Language Cell

1. Find all the external evaluators

• The order in which a System is listed in the External Evaluator table takes precedence.• In the example below, Python 3.8.5 is the default System taken by the external language cells.• To make Python 3.6.8 the default System in the external language cells the following is needed: 1. Unregister Python 3.8.5 using the function UnregisterExternalEvaluator[]. 2. Quit the local Kernel. These changes will not be reflected without quitting the local Kernel. 3. Register Python 3.8.5, if needed, using the function RegisterExternalEvaluator[]. 4. Python 3.6.8 will be moved above and will take precedence over Python 3.8.5.
In[]:=
FindExternalEvaluators[]
Out[]=
System
Version
Executable
Registered
Jupyter
7832e041-9f01-41b2-8d14-200b3c16918b
Jupyter
python3
C:\Users\jordonez\AppData\Local\Continuum\Anaconda3\share\jupyter\kernels\python3\kernel.json
Automatic
Python
7dcbb793-dffe-4ae7-b0a9-cfa363693741
Python
3.8.5
C:\Program Files\Python38\python.exe
True
0d025c0d-ad59-4cf0-beac-932ee1e08f60
Python
3.6.8
C:\Users\jordonez\AppData\Local\Programs\Python\Python36\python.exe
True
Python-NumPy
35941322-a92a-4c30-ad20-caaf2bc7670d
Python-NumPy
3.8.5
C:\Program Files\Python38\python.exe
Automatic
13563fca-c1f7-41b1-afd0-b3ac7961c668
Python-NumPy
3.6.8
C:\Users\jordonez\AppData\Local\Programs\Python\Python36\python.exe
Automatic
Python-PIL
99a5a9a6-4938-4e7c-b92c-119749d9f8d3
Python-PIL
3.8.5
C:\Program Files\Python38\python.exe
Automatic
e5f40ca5-d269-4b38-ac46-3eb820619fab
Python-PIL
3.6.8
C:\Users\jordonez\AppData\Local\Programs\Python\Python36\python.exe
Automatic
R
5d64bcf0-b466-4049-9349-eb53d069a7f9
R
C:\Program Files\Wolfram Research\Mathematica\12.1\SystemFiles\Links\RLink\Java\REngine.jar
Automatic
WebDriver-Chrome
e43f4a3b-cd44-44b5-86b5-12f3a4ec5fb4
WebDriver-Chrome
2.43.600210
C:\Program Files\Wolfram Research\Mathematica\12.1\SystemFiles\Components\WebUnit\Resources\DriverBinaries\ChromeDriver\Windows-x86-64\chromedriver.exe
Automatic
WebDriver-Chrome-Headless
08837101-230a-498b-84bc-016ef78b1318
WebDriver-Chrome-Headless
2.43.600210
C:\Program Files\Wolfram Research\Mathematica\12.1\SystemFiles\Components\WebUnit\Resources\DriverBinaries\ChromeDriver\Windows-x86-64\chromedriver.exe
Automatic
WebDriver-Firefox
017237fa-b788-4dee-a647-dd34db79e0b1
WebDriver-Firefox
0.23.0 ( 2018-10-04)
C:\Program Files\Wolfram Research\Mathematica\12.1\SystemFiles\Components\WebUnit\Resources\DriverBinaries\GeckoDriver\Windows-x86-64\geckodriver.exe
Automatic
WebDriver-Firefox-Headless
90a616bd-ffb4-494b-8289-50e910dae123
WebDriver-Firefox-Headless
0.23.0 ( 2018-10-04)
C:\Program Files\Wolfram Research\Mathematica\12.1\SystemFiles\Components\WebUnit\Resources\DriverBinaries\GeckoDriver\Windows-x86-64\geckodriver.exe
Automatic
The code below shows that the external language cell has Python 3.8.5 as default system
〉
In[]:=
import sys
sys.executable
Out[]=
C:\Program Files\Python38\python.exe

2. Unregistering Python 3.8.5

Creating an object with the path for Python 3.8.5 executable
In[]:=
FindExternalEvaluators["Python"]
System
Version
Executable
Registered
7dcbb793-dffe-4ae7-b0a9-cfa363693741
Python
3.8.5
C:\Program Files\Python38\python.exe
True
0d025c0d-ad59-4cf0-beac-932ee1e08f60
Python
3.6.8
C:\Users\jordonez\AppData\Local\Programs\Python\Python36\python.exe
True
In[]:=
python=Normal[%]
Out[]=
7dcbb793-dffe-4ae7-b0a9-cfa363693741SystemPython,Version3.8.5,ExecutableC:\Program Files\Python38\python.exe,RegisteredTrue,0d025c0d-ad59-4cf0-beac-932ee1e08f60SystemPython,Version3.6.8,ExecutableC:\Users\jordonez\AppData\Local\Programs\Python\Python36\python.exe,RegisteredTrue
In[]:=
python[[1,3]]
Out[]=
C:\Program Files\Python38\python.exe
Unregistering Python 3.8.5
In[]:=
UnregisterExternalEvaluator["Python",python[[1,3]]]
Out[]=
7dcbb793-dffe-4ae7-b0a9-cfa363693741
The table below shows False for Python 3.8.5. Therefore this version of Python has been unregistered
In[]:=
FindExternalEvaluators["Python"]
Out[]=
System
Version
Executable
Registered
7dcbb793-dffe-4ae7-b0a9-cfa363693741
Python
3.8.5
C:\Program Files\Python38\python.exe
False
0d025c0d-ad59-4cf0-beac-932ee1e08f60
Python
3.6.8
C:\Users\jordonez\AppData\Local\Programs\Python\Python36\python.exe
True
However, without quitting the kernel, the external language cells continue having Python 3.8.5 as default system
〉
In[]:=
import sys
sys.executable
Out[]=
C:\Program Files\Python38\python.exe
The functions StartExternalSession and ExternalEvaluate can be used to run different versions of Python. In the example below Python 3.6.8 is run. But it is not ideal. The external language cells are more practical.
In[]:=
python[[2,1;;3]]
Out[]=
SystemPython,Version3.6.8,ExecutableC:\Users\jordonez\AppData\Local\Programs\Python\Python36\python.exe
In[]:=
ExternalEvaluate[StartExternalSession[python[[2,1;;3]]],"import syssys.executable"]
Out[]=
C:\Users\jordonez\AppData\Local\Programs\Python\Python36\python.exe
In[]:=
ExternalEvaluate["Python","import syssys.executable"]
Out[]=
C:\Users\jordonez\AppData\Local\Programs\Python\Python36\python.exe

3. Registering Python 3.8.5

Now we register Python 3.8.5 again to make it available for use. But it will be proceeded by Python 3.6.8 which will be now priority in the external language cells.
In[]:=
FindExternalEvaluators["Python"]
Out[]=
System
Version
Executable
Registered
7dcbb793-dffe-4ae7-b0a9-cfa363693741
Python
3.8.5
C:\Program Files\Python38\python.exe
False
0d025c0d-ad59-4cf0-beac-932ee1e08f60
Python
3.6.8
C:\Users\jordonez\AppData\Local\Programs\Python\Python36\python.exe
True
In[]:=
Map[RegisterExternalEvaluator["Python",#]&,Normal[Values[FindExternalEvaluators["Python"]][All,"Executable"]]]
Out[]=
{7dcbb793-dffe-4ae7-b0a9-cfa363693741,0d025c0d-ad59-4cf0-beac-932ee1e08f60}
Note after registering Python 3.8.5 it is placed by Mathematica in the second row.
In[]:=
FindExternalEvaluators["Python"]
Out[]=
System
Version
Executable
Registered
0d025c0d-ad59-4cf0-beac-932ee1e08f60
Python
3.6.8
C:\Users\jordonez\AppData\Local\Programs\Python\Python36\python.exe
True
7dcbb793-dffe-4ae7-b0a9-cfa363693741
Python
3.8.5
C:\Program Files\Python38\python.exe
True
The external evaluation cells still runs Python 3.8.5 because the kernel has not been re-started
〉
In[]:=
import sys
sys.executable
Out[]=
C:\Program Files\Python38\python.exe
After quitting the local kernel, the external evaluation cell does run Python 3.6.8 as a priority.
The same steps need to be followed to reverse the changes if Python 3.8.5 is needed as the priority system.
〉
In[]:=
import sys
sys.executable
Out[]=
C:\Users\jordonez\AppData\Local\Programs\Python\Python36\python.exe