# run python with package numpy in mathematica

For example, if I have a python script called simple.py contains as simple as

a=1
print(a)


then

RunProcess[{"python", "E:\\w\\simple.py"}]


is ok and gives

<|"ExitCode" -> 0, "StandardOutput" -> "1

", "StandardError" -> ""|>


However, if I edit simple.py as

import numpy as np
a=np.array([1,2])
print(a)


then

RunProcess[{"python", "E:\\w\\simple.py"}]


gives

<|"ExitCode" -> 1, "StandardOutput" -> "",
"StandardError" -> "Traceback (most recent call last):

File \"E:\\w\\WORK\\python\\learning\\simple.py\", line 1, in \
<module>

import numpy as np

File \"D:\\IntelPython35\\lib\\site-packages\\numpy\\__init__.py\"\
, line 158, in <module>

py\", line 13, in <module>

File \"D:\\IntelPython35\\lib\\site-packages\\numpy\\lib\\__init__\
.py\", line 8, in <module>

from .type_check import *

File \"D:\\IntelPython35\\lib\\site-packages\\numpy\\lib\\type_\
check.py\", line 11, in <module>

import numpy.core.numeric as _nx

File \"D:\\IntelPython35\\lib\\site-packages\\numpy\\core\\__init_\
_.py\", line 21, in <module>

from . import umath

\[Micro]Ä\.b3ÌÐò¡£

"|>


What is wrong? How to make numpy work with RunProcess?

update

I've tried on a linux computer. The same python script works. So it is a problem of why RunProcess doesn't import packages correctly on windows?

update

A work around.

I uninstalled intel python and install anaconda.

Now it is working. Maybe intel python misses something in the PATH

• It's may be an issue with Environment["PATH"] or whatever the windows equivalent is. Check the appropriate environment variable in your shell and in Mathematica. I'm guessing they don't line up. – b3m2a1 Sep 7 '17 at 4:19
• Hi, @b3m2a1 Thank you so much for reply. I took your advice. I look into Environment["PATH"] and echo %PATH%. But they are the same except that Environment["PATH"] has 6 more path related to Mathematica than echo %PATH%. And here is my system path pastebin.com/2Zscj75e – matheorem Sep 7 '17 at 5:11

Starting in version 11.2 you may use ExternalEvaluate to run Python from Mathematica. See the Configure Python for ExternalEvaluate tutorial for setup.

session = StartExternalSession[<|"System" -> "Python", "ReturnType" -> "Expression"|>]


Then

res = ExternalEvaluate[session,
"import numpy as np; a=np.array([1,2]); a.tolist()"]

{1, 2}


End the session with the following.

DeleteObject@session


Hope this helps.

• Thank you Edmund. I updated to 11.2 windows. However, even though FindExternalEvaluators["Python"] has no problem, ExternalEvaluate[session, "import numpy as np; a=np.array([1,2]); \ a.tolist()"] gives error "Import::nopythonevals: No Python external evaluator found. Use RegisterExternalEvaluator to register an external evaluator." and "ExternalEvaluate::interpFail: The result [1, 2] failed to be interpreted as a WL expression. Use "ReturnType"->"String" instead." – matheorem Oct 8 '17 at 15:30
• @matheorem Did you configure Python as in the tutorial setup? – Edmund Oct 8 '17 at 16:04
• yeah. I think I did. I installed conda install pyzmq, also pip install zmq – matheorem Oct 8 '17 at 16:14
• @matheorem See this post (155821). – Edmund Oct 8 '17 at 17:01

I don't know how to resolve your issue, but here are my two cents.

I think it's likely a process environment issue as this works for me on Mac with Mathematica 11.1:

RunProcess[{"python3.4", "-i"},
"StandardOutput",
"import numpy as np; a=np.array([1,2]); print(a)",
ProcessEnvironment ->
<|
"PATH" -> "/usr/local/bin:" <> Environment["PATH"]
|>
]

"[1 2]
"


The different ProcessEnvironment is something I always need to run python, since I never bother to use SetEnvironment

• I seems not working for me. I want to know If you don't set ProcessEnvironment, the example will work or not? – matheorem Sep 7 '17 at 5:39
• The ProcessEnvironment will differ from your machine to mine. I need /usr/local/bin because my python3.4 binary is symlinked to /usr/local/bin/python3.4 You're on windows so the environment will obviously be different. It can find your python distro, but I was guessing the DLL couldn't be loaded because it couldn't be found. – b3m2a1 Sep 7 '17 at 5:41
• Yeah, the problem is that I don't which path to add : ) – matheorem Sep 7 '17 at 5:48
• @matheorem unfortunately me neither. Hopefully someone with a Windows machine can help you out here. – b3m2a1 Sep 7 '17 at 5:56
• I appreaciate your help : ) – matheorem Sep 7 '17 at 6:23

I installed IntelPython3 on Windows 7 and this worked (with Mathematica 10.1) for me:

SetDirectory["\\Users\\gauss\\Documents\\Python Scripts"]
RunProcess[{"C:\\IntelPython3\\python.exe", "simple.py"}]


It worked without any adjustment to paths or environment variables. It also worked without the ".exe" file extension, but it did not work without the "C:\\".

The SetDirectory was only necessary because I used the filename "simple.py". It is also possible to use a relative pathname without SetDirectory. That is, this also worked for me as the first command after launching Mathematica:

RunProcess[{"C:\\IntelPython3\\python.exe", "Python Scripts\\simple.py"}]


In both cases, python was able to import the numpy package, create the array and produce the proper output.

Mathematica environment:

When I wrote in the above that it did not work, I should have said the problem was a Mathematica "file not found" error, not a python message saying that numpy could not be imported. Here is the environment information you requested:

StringSplit[Environment["PATH"], ";"] // Column
(*

"C:\\Program Files\\Wolfram \
Research\\Mathematica\\10.1\\SystemFiles\\Libraries\\Windows-x86-64",
"C:\\Program Files\\Wolfram \
Research\\Mathematica\\10.1\\SystemFiles\\Libraries\\Windows",
"C:\\Program Files\\Wolfram \
Research\\Mathematica\\10.1\\SystemFiles\\Kernel\\Binaries\\Windows-\
x86-64",
"C:\\Program Files\\Wolfram Research\\Mathematica\\10.1",
"C:\\Program Files\\Wolfram \
Research\\Mathematica\\10.1\\SystemFiles\\FrontEnd\\Binaries\\Windows-\
x86-64",
"C:\\Program Files\\Wolfram \
Research\\Mathematica\\10.1\\SystemFiles\\Kernel\\Binaries\\Windows-\
x86-64",
"C:\\Windows\\system32",
"C:\\Windows",
"C:\\Windows\\System32\\Wbem",
"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\"

*)


My python file

I added code to the python script to print the environment variable PATH, so here is my script:  try : import os print(os.environ["PATH"]) except : print("no import os, or no PATH") try : import numpy as np b = np.array([1,2,3]) print(b) except : a = "jello world" print(a) 

Mathematica evaluation

You will note that IntelPython3 has added 2 path elements at the beginning of the path:

RunProcess[{"C:\\IntelPython3\\python.exe", "Python Scripts\\simple.py"}]
(*
<|"ExitCode" -> 0,
"StandardOutput" ->
"C:\\IntelPython3\\Library\\bin;C:\\IntelPython3\\DLLs;C:\\Program \
Files\\Wolfram \
Research\\Mathematica\\10.1\\SystemFiles\\Libraries\\Windows-x86-64;C:\
\\Program Files\\Wolfram \
Research\\Mathematica\\10.1\\SystemFiles\\Libraries\\Windows;C:\\\
Program Files\\Wolfram \
Research\\Mathematica\\10.1\\SystemFiles\\Kernel\\Binaries\\Windows-\
x86-64;C:\\Program Files\\Wolfram \
Research\\Mathematica\\10.1;C:\\Program Files\\Wolfram \
Research\\Mathematica\\10.1\\SystemFiles\\FrontEnd\\Binaries\\Windows-\
x86-64;C:\\Program Files\\Wolfram \
Research\\Mathematica\\10.1\\SystemFiles\\Kernel\\Binaries\\Windows-\
x86-64;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;\
C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\

[1 2 3]

", "StandardError" -> ""|>
*)

• Thank you LouisB. But I tried to provide the full path to python before, it doesn't work. Would you please provide your result of Environment["PATH"]? I want to see if I missed something before. – matheorem Sep 8 '17 at 0:10