18
$\begingroup$

As Mathematica has the interoperability with Python since M11.2, with major improvements in M12.0, is it possible to connect to already running interactive Python sessions? For example, can we connect to a Python kernel started by Jupyter?

Can someone provide a pathway for doing that?

This question came to my mind since I feel certain operations are handy at one side and vice versa now and then.

$\endgroup$
  • $\begingroup$ Are you trying to access the same Python workspace both from a Jupyter notebook and from Mathematica? $\endgroup$ – Szabolcs Feb 22 '18 at 17:12
  • 1
    $\begingroup$ @Szabolcs yes, that’s what I was thinking. $\endgroup$ – sunt05 Feb 22 '18 at 17:12
  • 2
    $\begingroup$ I suspect that the ExternalEvaluate stuff was not designed to allow this ... Being able to do this would be extremely useful though. I am putting a bounty on it, hoping that someone will at least investigate the limitations of ExternalEvaluate. $\endgroup$ – Szabolcs Jun 7 at 7:51
  • $\begingroup$ @Szabolcs do you specifically want this to work through ExternalEvaluate? Obviously you can do this if you can control Mathematica from python or python from Mathematica at the C++ level (both of which are of course very much so doable). Is it okay to have to type commands on both sides to get this to work? (i.e. start a LinkObject on the Mathematica side and connect to it from python). $\endgroup$ – b3m2a1 Jun 7 at 8:25
  • $\begingroup$ "do you specifically want this to work through ExternalEvaluate" No, but that framework grew big enough that it would be hard to compete with at this point. "Is it okay to have to type commands on both sides to get this to work?" Yes, completely okay. That would be hard to avoid. $\endgroup$ – Szabolcs Jun 7 at 8:29
-2
$\begingroup$

From Mathematica 12.0.0.0 Documentation Center entry for ExternalEvaluate: ExternalEvaluate[session,cmd] sends the command to a running session and does not exit the session. For example:

session = StartExternalSession["Python"]
ExternalEvaluate[session, "x=1; x"]
ExternalEvaluate[session, "x=x+1; x"]
ExternalEvaluate[session, "x=x+1; x"]
DeleteObject[session]

will produce an ExternalSessionObject that you can expand to show the process details. Successive outputs are 1, 2 and 3.

See https://reference.wolfram.com/language/ref/ExternalEvaluate.html

$\endgroup$
  • 1
    $\begingroup$ This does not answer the question at all ... $\endgroup$ – Szabolcs Jun 15 at 7:34
  • $\begingroup$ way to make new contributors feel welcome :( $\endgroup$ – soundslikeorange Jun 15 at 19:05
  • $\begingroup$ I cast the first downvote to prevent the expiring bounty from being auto-awarded to an answer which does not answer the posed question at all. $\endgroup$ – Szabolcs Jun 15 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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