{"id":1560,"date":"2019-05-05T09:08:21","date_gmt":"2019-05-05T03:38:21","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=1560"},"modified":"2019-05-05T09:20:43","modified_gmt":"2019-05-05T03:50:43","slug":"python-integration-in-mathematica-12","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2019\/05\/05\/python-integration-in-mathematica-12\/","title":{"rendered":"Python Integration in Mathematica 12"},"content":{"rendered":"<p><em><strong>Mathematica<\/strong><\/em> has had <em><strong>Python<\/strong><\/em> support since ver <em><strong>11.2<\/strong><\/em> through <em><strong>ExternalEvaluate[]<\/strong><\/em>. In ver <em><strong>11.3<\/strong><\/em> it was possible to input <em><strong>Python<\/strong><\/em> expression in a cell by beginning with <em><strong>\u201c&gt;\u201d<\/strong><\/em> character. The good news is that <em><strong>Mathematica 12<\/strong><\/em> has significantly enhanced this integration.<\/p>\n<h3><b>Python Cells<\/b><\/h3>\n<p>Let us start with the simplest way to use <em><strong>Python<\/strong><\/em> code in <em><strong>Mathematica<\/strong><\/em>. When you type <em><strong>\u201c&gt;\u201d<\/strong><\/em> as the first character in a cell, <em><strong>Mathematica<\/strong><\/em> automatically converts that cell into a <em><strong>Python<\/strong> <strong>\u201clistener\u201d<\/strong><\/em> (although <em><strong>Python<\/strong><\/em> is the default, you can also switch to <em><strong>NodeJS<\/strong><\/em> mode). See the following figure.<\/p>\n<figure id=\"attachment_1561\" aria-describedby=\"caption-attachment-1561\" style=\"width: 587px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/Python-Cell.png?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"1561\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/05\/05\/python-integration-in-mathematica-12\/python-cell\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/Python-Cell.png\" data-orig-size=\"587,424\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Python Cell\" data-image-description=\"&lt;p&gt;Python Cell&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Python Cell&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/Python-Cell.png\" class=\"wp-image-1561 size-full\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/Python-Cell.png?resize=587%2C424&#038;ssl=1\" alt=\"Python Cell\" width=\"587\" height=\"424\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/Python-Cell.png?w=587&amp;ssl=1 587w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/Python-Cell.png?resize=300%2C217&amp;ssl=1 300w\" sizes=\"(max-width: 587px) 100vw, 587px\" \/><\/a><figcaption id=\"caption-attachment-1561\" class=\"wp-caption-text\"><strong>Python Cell<\/strong><\/figcaption><\/figure>\n<p>I hope you are able to see that the state is preserved across these cells. That is nice.<\/p>\n<p>Working with <em><strong>Python<\/strong><\/em> this way is interesting, but probably not that useful because you are not able to mix <em><strong>Python<\/strong><\/em> with <em><strong>Mathematica<\/strong><\/em>.<\/p>\n<h3><b>Using ExternalEvaluate[]<\/b><\/h3>\n<p>In the simplest case, <em><strong>ExternalEvaluate[]<\/strong><\/em> lets us execute arbitrary <em><strong>Python<\/strong><\/em> code embedded in a string.<\/p>\n<figure id=\"attachment_1562\" aria-describedby=\"caption-attachment-1562\" style=\"width: 586px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-1.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"1562\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/05\/05\/python-integration-in-mathematica-12\/externalevaluate-1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-1.png\" data-orig-size=\"586,198\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ExternalEvaluate Function\" data-image-description=\"&lt;p&gt;ExternalEvaluate Function&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;ExternalEvaluate Function&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-1.png\" class=\"size-full wp-image-1562\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-1.png?resize=586%2C198&#038;ssl=1\" alt=\"ExternalEvaluate Function\" width=\"586\" height=\"198\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-1.png?w=586&amp;ssl=1 586w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-1.png?resize=300%2C101&amp;ssl=1 300w\" sizes=\"(max-width: 586px) 100vw, 586px\" \/><\/a><figcaption id=\"caption-attachment-1562\" class=\"wp-caption-text\"><strong>ExternalEvaluate Function<\/strong><\/figcaption><\/figure>\n<p>The first argument is the language string, in this case, <em><strong>\u201cPython\u201d<\/strong><\/em>, and the second argument is a string containing the external language expression to be evaluated.<\/p>\n<p>Notice that in this case, each call to <b>ExternalEvaluate[] <\/b>executes in its own <em><strong>\u201csession\u201d<\/strong><\/em>, so there is no sharing of state across different cells (hence the error in the third expression). This mode is useful only if we are interested in <em><strong>&#8220;independent&#8221;<\/strong><\/em> evaluation of expressions, something that is not quite common.<\/p>\n<h3><b>Identifying External Evaluators<\/b><\/h3>\n<p>Before we proceed further, let us discuss the function <strong><em>FindExternalEvaluators[]<\/em><\/strong>.<span class=\"Apple-converted-space\">\u00a0 <\/span>This function returns data about what external evaluators are available on the current platform. <em><strong>\u201cPython\u201d<\/strong><\/em> happens to be just one external system supported. <em><strong>&#8220;<\/strong><strong>NodeJS&#8221;<\/strong><\/em> is another system that is integrated into <em><strong>Mathematica 12<\/strong><\/em>. When I execute this function on my <em><strong>iMac<\/strong><\/em> (macOS 10.14.4), I get the following result:<\/p>\n<figure id=\"attachment_1564\" aria-describedby=\"caption-attachment-1564\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/FindExternalEvaluators.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"1564\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/05\/05\/python-integration-in-mathematica-12\/findexternalevaluators\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/FindExternalEvaluators.png\" data-orig-size=\"973,744\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Finding External Evaluators\" data-image-description=\"&lt;p&gt;Finding External Evaluators&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Finding External Evaluators&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/FindExternalEvaluators.png\" class=\"wp-image-1564\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/FindExternalEvaluators.png?resize=650%2C497&#038;ssl=1\" alt=\"Finding External Evaluators\" width=\"650\" height=\"497\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/FindExternalEvaluators.png?w=973&amp;ssl=1 973w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/FindExternalEvaluators.png?resize=300%2C229&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/FindExternalEvaluators.png?resize=768%2C587&amp;ssl=1 768w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-1564\" class=\"wp-caption-text\"><strong>Finding External Evaluators<\/strong><\/figcaption><\/figure>\n<p>By the way, for the <em><strong>Python<\/strong><\/em> integration to work properly, <em><strong>\u201cpyzmq\u201d<\/strong><\/em> package for <em><strong>Python<\/strong><\/em> must be installed on the current platform. See <a href=\"https:\/\/reference.wolfram.com\/language\/workflow\/ConfigurePythonForExternalEvaluate.html\" target=\"_blank\" rel=\"noopener\"><em><strong>this<\/strong><\/em><\/a>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<h3><b>Working with Explicit Python Session<\/b><\/h3>\n<p>If we wish to create a <em><strong>Python<\/strong><\/em> session whose lifetime we want to control, then <em><strong>StartExternalSession[]<\/strong><\/em> function is what we need. This function sets up a session for the given external evaluator, and as you can guess, the <em><strong>ExternalEvaluate[]<\/strong><\/em> function we saw earlier takes this session as its first parameter. That means our evaluation is always in the context of this session object.<\/p>\n<figure id=\"attachment_1567\" aria-describedby=\"caption-attachment-1567\" style=\"width: 664px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/StartSession.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1567\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/05\/05\/python-integration-in-mathematica-12\/startsession\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/StartSession.png\" data-orig-size=\"664,98\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Starting a Python Session\" data-image-description=\"&lt;p&gt;Starting a Python Session&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Starting a Python Session&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/StartSession.png\" class=\"size-full wp-image-1567\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/StartSession.png?resize=664%2C98&#038;ssl=1\" alt=\"Starting a Python Session\" width=\"664\" height=\"98\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/StartSession.png?w=664&amp;ssl=1 664w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/StartSession.png?resize=300%2C44&amp;ssl=1 300w\" sizes=\"(max-width: 664px) 100vw, 664px\" \/><\/a><figcaption id=\"caption-attachment-1567\" class=\"wp-caption-text\"><strong>Starting a Python Session<\/strong><\/figcaption><\/figure>\n<p>Here is how we can use this session:<\/p>\n<figure id=\"attachment_1568\" aria-describedby=\"caption-attachment-1568\" style=\"width: 658px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-2.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1568\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/05\/05\/python-integration-in-mathematica-12\/externalevaluate-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-2.png\" data-orig-size=\"658,191\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Evaluation in a Session\" data-image-description=\"&lt;p&gt;Evaluation in a Session&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Evaluation in a Session&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-2.png\" class=\"size-full wp-image-1568\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-2.png?resize=658%2C191&#038;ssl=1\" alt=\"Evaluation in a Session\" width=\"658\" height=\"191\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-2.png?w=658&amp;ssl=1 658w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-2.png?resize=300%2C87&amp;ssl=1 300w\" sizes=\"(max-width: 658px) 100vw, 658px\" \/><\/a><figcaption id=\"caption-attachment-1568\" class=\"wp-caption-text\"><strong>Evaluation in a Session<\/strong><\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>The third expression above shows how we can invoke a specific function and pass arguments to it. You can also see that state is preserved across expressions.<\/p>\n<p>The following is an example of a <em><strong>Python<\/strong><\/em> function taking two arguments:<\/p>\n<figure id=\"attachment_1569\" aria-describedby=\"caption-attachment-1569\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-3.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1569\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/05\/05\/python-integration-in-mathematica-12\/externalevaluate-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-3.png\" data-orig-size=\"660,200\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Function with 2 Arguments\" data-image-description=\"&lt;p&gt;Function with 2 Arguments&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Function with 2 Arguments&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-3.png\" class=\"size-full wp-image-1569\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-3.png?resize=660%2C200&#038;ssl=1\" alt=\"Function with 2 Arguments\" width=\"660\" height=\"200\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-3.png?w=660&amp;ssl=1 660w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalEvaluate-3.png?resize=300%2C91&amp;ssl=1 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-1569\" class=\"wp-caption-text\"><strong>Function with 2 Arguments<\/strong><\/figcaption><\/figure>\n<p>Note the two ways of passing arguments to a function. Referencing specific functions like the above will be even more useful when we load a <em><strong>Python<\/strong><\/em> code file containing many definitions.<\/p>\n<figure id=\"attachment_1570\" aria-describedby=\"caption-attachment-1570\" style=\"width: 658px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/External-Evaluate-4.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1570\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/05\/05\/python-integration-in-mathematica-12\/external-evaluate-4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/External-Evaluate-4.png\" data-orig-size=\"658,300\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Function References\" data-image-description=\"&lt;p&gt;Function References&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Function References&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/External-Evaluate-4.png\" class=\"size-full wp-image-1570\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/External-Evaluate-4.png?resize=658%2C300&#038;ssl=1\" alt=\"Function References\" width=\"658\" height=\"300\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/External-Evaluate-4.png?w=658&amp;ssl=1 658w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/External-Evaluate-4.png?resize=300%2C137&amp;ssl=1 300w\" sizes=\"(max-width: 658px) 100vw, 658px\" \/><\/a><figcaption id=\"caption-attachment-1570\" class=\"wp-caption-text\"><strong>Function Reference<\/strong><\/figcaption><\/figure>\n<p>In the above example, I create a reference to the <em><strong>Python<\/strong><\/em> function <em><strong>\u201cdecr\u201d<\/strong><\/em> and then use it as if it is a <em><strong>Mathematica<\/strong><\/em> function! Quite powerful indeed.<\/p>\n<p>Another related function is <em><strong>\u201cExternalValue[]\u201d<\/strong><\/em>. This function takes an external session and a symbol name, and returns the value of that symbol in that session.<\/p>\n<figure id=\"attachment_1571\" aria-describedby=\"caption-attachment-1571\" style=\"width: 658px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalValue.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1571\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/05\/05\/python-integration-in-mathematica-12\/externalvalue\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalValue.png\" data-orig-size=\"658,69\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Referencing a Value\" data-image-description=\"&lt;p&gt;Referencing a Value&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Referencing a Value&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalValue.png\" class=\"size-full wp-image-1571\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalValue.png?resize=658%2C69&#038;ssl=1\" alt=\"Referencing a Value\" width=\"658\" height=\"69\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalValue.png?w=658&amp;ssl=1 658w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/ExternalValue.png?resize=300%2C31&amp;ssl=1 300w\" sizes=\"(max-width: 658px) 100vw, 658px\" \/><\/a><figcaption id=\"caption-attachment-1571\" class=\"wp-caption-text\"><em><strong>Referencing a Value<\/strong><\/em><\/figcaption><\/figure>\n<p>Finally, when we are done with an external session, we have to free the session:<\/p>\n<figure id=\"attachment_1572\" aria-describedby=\"caption-attachment-1572\" style=\"width: 662px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/DeleteObject.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1572\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/05\/05\/python-integration-in-mathematica-12\/deleteobject\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/DeleteObject.png\" data-orig-size=\"662,42\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Closing a Session\" data-image-description=\"&lt;p&gt;Closing a Session&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Closing a Session&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/DeleteObject.png\" class=\"size-full wp-image-1572\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/DeleteObject.png?resize=662%2C42&#038;ssl=1\" alt=\"Closing a Session\" width=\"662\" height=\"42\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/DeleteObject.png?w=662&amp;ssl=1 662w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/DeleteObject.png?resize=300%2C19&amp;ssl=1 300w\" sizes=\"(max-width: 662px) 100vw, 662px\" \/><\/a><figcaption id=\"caption-attachment-1572\" class=\"wp-caption-text\"><strong>Cleaning up a Session<\/strong><\/figcaption><\/figure>\n<p>In summary, I feel that <em><strong>Mathematica 12<\/strong><\/em> has done a great job in enhancing the integration with external systems such as <em><strong>Python<\/strong><\/em> (<em><strong>NodeJS<\/strong><\/em> is also supported). I expect this external integration to include other languages and environments in the future.<\/p>\n<p>You can download my <em><strong>Mathematica 12<\/strong> <a href=\"http:\/\/www.rangakrish.com\/downloads\/PythonIntegration.nb\" target=\"_blank\" rel=\"noopener\"><strong>notebook<\/strong><\/a><\/em> and the sample <em><strong>Python<\/strong> <a href=\"http:\/\/www.rangakrish.com\/downloads\/PythonFunctions.py\" target=\"_blank\" rel=\"noopener\"><strong>code<\/strong><\/a><\/em> file.<\/p>\n<p>Have a nice weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mathematica has had Python support since ver 11.2 through ExternalEvaluate[]. In ver 11.3 it was possible to input Python expression in a cell by beginning with \u201c&gt;\u201d character. The good news is that Mathematica 12 has significantly enhanced this integration. Python Cells Let us start with the simplest way to use Python code in Mathematica. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[72,17,103],"tags":[206,208],"class_list":["post-1560","post","type-post","status-publish","format-standard","hentry","category-mathematica","category-programming","category-python","tag-mathematica-12","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-pa","jetpack-related-posts":[{"id":1594,"url":"https:\/\/www.rangakrish.com\/index.php\/2019\/06\/08\/using-nodejs-in-mathematica-12\/","url_meta":{"origin":1560,"position":0},"title":"Using NodeJS in Mathematica 12","author":"admin","date":"June 8, 2019","format":false,"excerpt":"In an earlier article, I had described Python integration in Mathematica 12. In addition to Python, NodeJS is also supported as a default \u201cexternal\u201d language. In today\u2019s article, I will focus on NodeJS integration. By the way, NodeJS support was introduced in Mathematica 11.2. Before using NodeJS with Mathematica 12,\u2026","rel":"","context":"In &quot;Mathematica&quot;","block_context":{"text":"Mathematica","link":"https:\/\/www.rangakrish.com\/index.php\/category\/mathematica\/"},"img":{"alt_text":"NodeJS Session Continued","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/06\/Session2.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/06\/Session2.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/06\/Session2.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/06\/Session2.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2947,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/11\/24\/using-julia-from-mathematica\/","url_meta":{"origin":1560,"position":1},"title":"Using Julia from Mathematica","author":"admin","date":"November 24, 2022","format":false,"excerpt":"In an earlier article, I had shown how it is possible to interact with Mathematica from Julia. In today\u2019s article, I will share the details of how to interact with Julia from within Mathematica. Why would somebody want to execute Julia code inside Mathematica? Although Mathematica is a great symbolic\u2026","rel":"","context":"In &quot;Julia&quot;","block_context":{"text":"Julia","link":"https:\/\/www.rangakrish.com\/index.php\/category\/julia\/"},"img":{"alt_text":"Installing Julia","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/11\/Julia-Shell.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/11\/Julia-Shell.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/11\/Julia-Shell.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3381,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/05\/20\/using-openai-from-mathematica\/","url_meta":{"origin":1560,"position":2},"title":"Using OpenAI from Mathematica","author":"admin","date":"May 20, 2024","format":false,"excerpt":"Mathematica was among the first to integrate with OpenAI. The functionality is nicely exposed in terms of a few pre-defined functions. Let us explore some of the functionality in today\u2019s article. The simplest way to get started is to use LLMSynthesize\u00a0function: It can take a few seconds before you get\u2026","rel":"","context":"In &quot;Mathematica&quot;","block_context":{"text":"Mathematica","link":"https:\/\/www.rangakrish.com\/index.php\/category\/mathematica\/"},"img":{"alt_text":"LLMSynthesize Function","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/05\/Example1-300x27.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/05\/Example1-300x27.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/05\/Example1-300x27.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":328,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/09\/11\/natural-language-processing-in-mathematica\/","url_meta":{"origin":1560,"position":3},"title":"Natural Language Processing in Mathematica","author":"admin","date":"September 11, 2016","format":false,"excerpt":"Welcome back. Today I am going to share with you some of the nice capabilities of Mathematica in the area of Natural Language Processing (NLP). Let us start with words. What if we wish to know\u00a0the various definitions of the word image?\u00a0Here is the answer. Mathematica gives the various senses\u2026","rel":"","context":"In &quot;Mathematica&quot;","block_context":{"text":"Mathematica","link":"https:\/\/www.rangakrish.com\/index.php\/category\/mathematica\/"},"img":{"alt_text":"Word Definition","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/09\/word-data1-1024x238.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/09\/word-data1-1024x238.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/09\/word-data1-1024x238.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/09\/word-data1-1024x238.png?resize=700%2C400 2x"},"classes":[]},{"id":285,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/07\/22\/using-julia-to-interact-with-mathematica\/","url_meta":{"origin":1560,"position":4},"title":"Using Julia to Interact with Mathematica","author":"admin","date":"July 22, 2016","format":false,"excerpt":"Mathematica is a powerful environment for symbolic and numerical computation. I have been using it for many years now. In this post\u00a0I had explained how we can use Mathematica bundled with Raspberry distribution to control littleBits devices. When I saw that there is support in Julia for interacting with Mathematica,\u2026","rel":"","context":"In &quot;Julia&quot;","block_context":{"text":"Julia","link":"https:\/\/www.rangakrish.com\/index.php\/category\/julia\/"},"img":{"alt_text":"Julia Session","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/07\/Julia-1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/07\/Julia-1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/07\/Julia-1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":3514,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/10\/02\/using-openai-from-mathematica-part-2\/","url_meta":{"origin":1560,"position":5},"title":"Using OpenAI from Mathematica: Part-2","author":"admin","date":"October 2, 2024","format":false,"excerpt":"I had written an earlier article\u00a0showing how to use OpenAI models from Mathematica ver 14.0.\u00a0 Wolfram Mathematica ver 14.1 was released recently, with several improvements in the area of LLMs. Of course, there are many other core additions as well, but our focus is on LLMs in this article. This\u2026","rel":"","context":"In &quot;Mathematica&quot;","block_context":{"text":"Mathematica","link":"https:\/\/www.rangakrish.com\/index.php\/category\/mathematica\/"},"img":{"alt_text":"LLMSynthesize Function","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/10\/image1-300x71.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/10\/image1-300x71.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/10\/image1-300x71.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=1560"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1560\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=1560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=1560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=1560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}