4
$\begingroup$

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():
   print("Test")
print("Hello")

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

python
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

$\endgroup$
4
  • 1
    $\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 '19 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 '19 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 '19 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 '19 at 7:43
0
$\begingroup$

By default when you start a session, Working directory in Windows, is Documents folder:

ExternalEvaluate["Python", "import os;os.getcwd()"]

(*Out: "C:\\Users\\USERNAME\\Documents" *)

Import a single file

If you want to import a single file that does not import other files (not dependent on others), this solution will work:

ss = StartExternalSession["Python"];

ExternalEvaluate[ss, File["C:\\foo.py"]]

DeleteObject[ss];

Now it's like you had executed from foo.py import * and have access to all of its functions and variables.

Import a file with its dependencies

If you have a file that imports other files, the above solution will raise an error:

File: C:\bar.py
===============

import foo

foo.test()
ss = StartExternalSession["Python"];

ExternalEvaluate[ss, File["C:\\bar.py"]]

DeleteObject[ss];

enter image description here

The solution is to make the file directory known to the python interpreter which have so many solutions (some discussed here):

One of the solutions is to add sys.path.append(...) to the beginning of that file:

File: C:\bar.py
===============

import sys
sys.path.append('C:\\')

import foo

foo.test()

Now you have access to all of the functions and variables + ability to import other files.

All the code tested on Mathematica 12.2.

$\endgroup$

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.