Group Abstract Group Abstract

Message Boards Message Boards

Has anyone gotten ExternalEvaluate to work with Anaconda Python on OS X?

Posted 8 years ago

Has anyone gotten ExternalEvaluate to work with Anaconda Python on OS X? It used to work for me in the prerelease, but it's not working in the final release.

In[2]:= py = StartExternalSession["Python"]
Out[2]= ExternalSessionObject["3dac9d41-1400-48be-83ea-9b6012316ff4"]

In[3]:= ExternalEvaluate[py, "1+1"]

During evaluation of In[3]:= Import::nopythonevals: No Python external evaluator found. Use RegisterExternalEvaluator to register an external evaluator.
During evaluation of In[3]:= ExternalEvaluate::interpFail: The result 2 failed to be interpreted as a WL expression. Use "ReturnType"->"String" instead.

Out[3]= $Failed

enter image description here

Notice that Python did return the result 2. What fails is only the translation of this result to Mathematica.

I would like to know if this is broken for everyone or if my system is misconfigured.


After this problem is solved, I would also like to know if it is possible to use different virtualenvs. Can I have multiple external evaluators registered, each corresponding to a specific virtualenv? Update: It seems that virtualenv creates a copy of the python interpreter, thus passing this to RegisterExternalEvaluator should work. But I can't try until ExternalEvaluate starts working on my system ...

POSTED BY: Szabolcs Horvát
5 Replies

I know this is an old post, but a lot has changed with ExternalEvaluate and its support of Python. In the current releases ExternalEvaluate will create virtual environments on the fly.

For example, this now creates a Python virtual environment that includes pandas:

session = StartExternalSession[<|
   "System" -> "Python",
   "Evaluator" -> <|"Dependencies" -> {"pandas"}|>,
   "SessionProlog" -> "import pandas as pd"|>]

And then this is some sample code that shows how you can use it. The following will return a Dataset:

ExternalEvaluate[session, "
data = {'col1': [1, 2, 3], 'col2': ['a', 'b', 'c']}
pd.DataFrame(data)"]

The attached notebook shows the code with the output included.

Attachments:
POSTED BY: Arnoud Buzing

I didn't comment on virtualenv further because when I tried it, it worked for me. It is necessary to have pyzmq installed in that virtualenv though. Did you make sure it's installed?

POSTED BY: Szabolcs Horvát
Posted 8 years ago

I tried to register a Python executable installed in a virtualenv with RegisterExternalEvaluator but it didn't seem to work:

In[162]:= virtualenvPython="/Users/meng/python_virtualenvs/virtualenv-test/bin/python2.7";
RunProcess[{virtualenvPython,"--version"},All]
Out[162]= <|"ExitCode" -> 0, "StandardOutput" -> "", 
 "StandardError" -> "Python 2.7.13
  "|>
In[161]:= RegisterExternalEvaluator["Python",virtualenvPython]
During evaluation of In[161]:= RegisterExternalEvaluator::invalid: -- Message text not found -- (ExternalEvaluate`Private`reason)
Out[161]= $Failed
POSTED BY: Meng Lu
POSTED BY: Szabolcs Horvát
POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard