I run some Python code in the external code cell as following:

enter image description here

The above Python code produced the result (as shown with red [1] in the screenshot), which is a list of squart of i.

Then, I want to analyze the result within Mathematica environment. Out of my expectation, the result shown as not defined (as shown with red [2] in the screenshot).

My Question

How can I export the output result of Python to Mathematica computational enviroment? (So that I can further analyze the data in Mathematica)


2 Answers 2


Even if the session was still open External code cell will be open after executing, you can't use the same variable name you'd defined in the external session, inside Mathematica. Let me introduce you to ExternalValue.

(* start a python session *)
session = StartExternalSession["Python"];

(* run your code using the defined session *)
ExternalEvaluate[session, "import math
result=[math.sqrt(i) for i in range(10)]"];

(* get the value from the session *)
ExternalValue[session, "result"]

(*Out: {0., 1., 1.41421, 1.73205, 2., 2.23607, 2.44949, 2.64575, 2.82843, 3.} *)

(* terminate the session *)

Also, you can use this workaround but it has its limitations:

result = ExternalEvaluate["Python", "import math
[math.sqrt(i) for i in range(10)]"];

(*Out: {0., 1., 1.41421, 1.73205, 2., 2.23607, 2.44949, 2.64575, 2.82843, 3.} *)

Note that if you use the print statement in python, this workaround doesn't work. result value will be Null.

Update 1

You can stay in the External code cell:

result = ExternalValue[ExternalEvaluate`GetDefaultExternalSession["Python"], "result"]
  • 3
    $\begingroup$ I think it’s important to note why the the result value would be Null when using a print statement. (Because the output of Print is Null, from my understanding) $\endgroup$ Jun 27, 2021 at 18:11
  • $\begingroup$ Inspired by your solution, I find another workaround with External Code Cell. $\endgroup$
    – PureLine
    Jun 28, 2021 at 2:47
  • $\begingroup$ Note the ExternalValue have two usages, but you just use the second. As my guess, the first usage should be ExternalValue["Python", "result"]. But it's error.... $\endgroup$
    – yode
    Jun 10, 2022 at 17:29
  • $\begingroup$ @yode I agree with you. It's probably because it's still experimental, we see such behaviors. $\endgroup$
    – Ben Izd
    Jun 11, 2022 at 6:43

Inspired by your solution, I find another workaround with External Code Cell:

enter image description here

  • 2
    $\begingroup$ Ideally I would expect Mathematica to have inline external code cell. (We already have Ctrl+= for inline free-form, and Ctrl+4 for inline $\LaTeX$, why not more? ) OK, I know the external evaluation functionality is still EXPERIMENTAL… $\endgroup$
    – xzczd
    Jun 28, 2021 at 5:04
  • $\begingroup$ @xzczd inline $\LaTeX$?.. I get an underscript box instead in Mathematica 12 and earlier... $\endgroup$
    – Ruslan
    Jun 28, 2021 at 12:05
  • $\begingroup$ @Ruslan It's new in 12.2 :) . $\endgroup$
    – xzczd
    Jun 28, 2021 at 13:01

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.