Consider the following command.

CellPrint[Cell["[1,2,3]", "ExternalLanguage"]]

It creates a python cell with command [1,2,3].

If evaluated then the result is {1,2,3}

Is there a way to Evaluate the cell expression programmatically and get the result in a variable. Like following,

result=CellEvaluate[Cell["[1,2,3]", "ExternalLanguage"]];

So now the result variable contains {1,2,3}.

  • $\begingroup$ Why not just use ExternalEvalute? $\endgroup$
    – b3m2a1
    Feb 4, 2019 at 22:17
  • $\begingroup$ Because it is currently not connected with ExternalLanguage Cells. If you define some function in ExternalLanguage Cells and use ExternalEvaluate["Python","..."] then this does not have access to those definitions since they both use different kernel session of python. This is a bug which is confirmed to be fixed in 12.0 but i want some workaround now. $\endgroup$
    – user13892
    Feb 4, 2019 at 22:23
  • $\begingroup$ Ah just look into the CellEvaluationFunction then. I'll post an answer. $\endgroup$
    – b3m2a1
    Feb 4, 2019 at 22:24
  • $\begingroup$ See also WFR SetLanguageCellSession as used in 236298/61736 $\endgroup$
    – Sterling
    May 19, 2021 at 14:05

1 Answer 1


You really want to be able to use the same evaluator as the .nb does, so we'll figure out what the .nb does:

 EvaluationNotebook[], {StyleDefinitions, "ExternalLanguage", 


Then look at the DownValue:


And we see it just calls this:


Which we dig into and get that it's just working off of this Association:


And then we use that like so:

ExternalEvaluate[ExternalEvaluate`FE`$CellSessions["Python"], "1"]

And now you can programmatically access .nb definitions.

  • $\begingroup$ Beautiful ... is there a way to look at the definitions of all builtin functions using GeneralUtilities? $\endgroup$
    – user13892
    Feb 4, 2019 at 22:40
  • $\begingroup$ Not sure what you mean. But you can look at many of them using that function. Some don’t expose their definitions. $\endgroup$
    – b3m2a1
    Feb 4, 2019 at 22:41
  • $\begingroup$ If i apply this function to say Sin I see that it says <<kernel function>>. Does it mean that it is not written in the wolfram language so is part of the kernel written in C? $\endgroup$
    – user13892
    Feb 4, 2019 at 22:56
  • $\begingroup$ @user13892 yeah a huge amount of the functionality in Mathematica is actually implemented in C++ $\endgroup$
    – b3m2a1
    Feb 4, 2019 at 22:57
  • $\begingroup$ Also if i create a package in which i give the function the attribute Locked, Protected and ReadProtected and then use this GeneralUtilities`PrintDefinitionsLocal on it. Will i be able to see the definitions? $\endgroup$
    – user13892
    Feb 4, 2019 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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