I am trying to import a Python function or execute a Python script from inside Mathematica, but the ExternalEvaluate command drives me nuts.
I proceed cautiously with the following Python test module, named "test.py":

def test():

I checked that the module can be found and tried it successfully from the CMD command line:

from test import test

Using Import in Mathematica also works:

Import["!python g:\\p\\python37\\test.py", "String"]

But then with ExternalEvaluate I first made sure that the Python connection works as follows, which works OK:

ExternalEvaluate["Python", "2+3"]

However, the following gives Exception: Import Error. EvaluationFailure:

ExternalEvaluate["Python", "from test import test"]

The same happens if I use a session:

ses = StartExternalSession["Python"]
ExternalEvaluate[ses, "from test import test"]

I would be glad for a hint what I am doing wrong

  • $\begingroup$ Check os.getcwd() and sys.path. ExternalEvaluate might not have those right to find it. Also check sys.version to make sure it's 3.7. $\endgroup$ – b3m2a1 Mar 26 at 20:57
  • $\begingroup$ I checked this using ExternalEvaluate["Python","import os; print(os.getcwd())"] e.t.c. Everything o.k. $\endgroup$ – Daniel Huber Mar 27 at 7:27
  • $\begingroup$ Heureka, after starting the PC and Mathematica this morning, ExternalEvaluate seems to work. I am at a loss to explain this. Anyway, thank you for your effort. $\endgroup$ – Daniel Huber Mar 27 at 7:36
  • $\begingroup$ My guess is that somehow the module wasn't on the python path or the wrong version of python was being used. $\endgroup$ – b3m2a1 Mar 27 at 7:43

Your Answer

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

Browse other questions tagged or ask your own question.