Changing Python Default Version in the External Language Cell
Changing Python Default Version in the External Language Cell
1. Find all the external evaluators
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[]=
The code below shows that the external language cell has Python 3.8.5 as default system
In[]:=
import sys
sys.executable
sys.executable
Out[]=
C:\Program Files\Python38\python.exe
2. Unregistering Python 3.8.5
2. Unregistering Python 3.8.5
Creating an object with the path for Python 3.8.5 executable
In[]:=
FindExternalEvaluators["Python"]
In[]:=
python=Normal[%]
Out[]=
7dcbb793-dffe-4ae7-b0a9-cfa363693741SystemPython,Version3.8.5,ExecutableC:\Program Files\Python38\python.exe,RegisteredTrue,0d025c0d-ad59-4cf0-beac-932ee1e08f60SystemPython,Version3.6.8,ExecutableC:\Users\jordonez\AppData\Local\Programs\Python\Python36\python.exe,RegisteredTrue
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[]=
However, without quitting the kernel, the external language cells continue having Python 3.8.5 as default system
In[]:=
import sys
sys.executable
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[]=
SystemPython,Version3.6.8,ExecutableC:\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
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[]=
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[]=
The external evaluation cells still runs Python 3.8.5 because the kernel has not been re-started
In[]:=
import sys
sys.executable
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.
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
sys.executable
Out[]=
C:\Users\jordonez\AppData\Local\Programs\Python\Python36\python.exe