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

import numpy as np

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

content = f.read()

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?

  • 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
    Commented Nov 30, 2021 at 22:45

1 Answer 1


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


enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.