2
$\begingroup$

I have the following 'test.py' written in Python:

import numpy as np

f = open('normals.txt', 'r')

content = f.read()
print(content)

and I want to execute it from Mathematica using:

session = StartExternalSession["Python"];
ExternalEvaluate[session, File["~/test.py"]]

but when I run this, I get the error:

enter image description here

Following the answer given in Using local variables in ExternalEvaluate Python, I have checked that numpy is already installed, by running FindExternalEvaluators[] where I get a table with Python, Python-NumPy, Python-PIL, etc. so I guess everything is fine. Now, I should run:

session = StartExternalSession["Python-NumPy"]

However, it returns a failure with the session specification, StartExternalSession::invalidSpec. How can I ExternalEvaluate a Python code which has import numpy in it?

$\endgroup$
1
  • 2
    $\begingroup$ My first thought is that you have multiple instances of Python installed on your computer, and Mathematica just seems to run the wrong one. Try ExternalEvaluate[session, "import sys; print(sys.executable)"] and see whether the path is correct. Did you install numpy via pip? Do you use conda environments? $\endgroup$
    – Domen
    Nov 30, 2021 at 22:45

1 Answer 1

1
$\begingroup$

First, try registering the external evaluator (Python or other). You just have to locate the path where you have it installed.

enter image description here

Test:

enter image description here

$\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.