Of course, I can use the this code:

session = StartExternalSession["Python"];
mmaVar1 = ExternalEvaluate[session, "a=[3,6,4];a"]

{3, 6, 4}

Then I can get a MMA variable mmaVar1, its name is assigned by Python. But I have to say it hard to write python code in a string line. So I like typing a > key into DefaultPythonSession:

enter image description here

But how to get the value of a in the DefaultPythonSession and assign to a MMA variable? This is my current method:

  • Find the order of the DefaultPythonSession by ExternalSessions[]: enter image description here

  • Then

    mmaVar2 = ExternalEvaluate[ExternalSessions[][[2]], "a"]

Dirty solution...

  • $\begingroup$ Maybe SelectFirst[ExternalSessions["Python"], #["Name"] === "DefaultPythonSession" &] make it less dirty ;) $\endgroup$
    – Ben Izd
    Jun 10, 2022 at 9:01
  • 1
    $\begingroup$ Even better ExternalEvaluate`GetDefaultExternalSession["Python"] $\endgroup$
    – Ben Izd
    Jun 10, 2022 at 9:03
  • $\begingroup$ @BenIzd Magic!!!! $\endgroup$
    – yode
    Jun 10, 2022 at 9:26
  • $\begingroup$ @BenIzd Do you mind to add an answer? I think it can help to read for the following user $\endgroup$
    – yode
    Jun 10, 2022 at 13:22
  • $\begingroup$ Strongly related, if not duplicate: mathematica.stackexchange.com/q/250259/1871 $\endgroup$
    – xzczd
    Jun 10, 2022 at 13:47

1 Answer 1


You could get DefaultPythonSession directly by:


or if you prefer using ExternalSessions by:

SelectFirst[ExternalSessions["Python"], #["Name"] === "DefaultPythonSession" &]

You should also note, that starting fresh Mathematica, will not start a python session automatically, so if you didn't start one, the first method return unevaluated command while in the second method, you have the option to return whatever you want (default is Missing["NotFound"]).


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.