7

For example, if I have a Python script called simple.py that contains code 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 \&quot;E:\\w\\WORK\\python\\learning\\simple.py\&quot;, line 1, in \

<module>

  import numpy as np

File \&quot;D:\\IntelPython35\\lib\\site-packages\\numpy\\__init__.py\&quot;\

, line 158, in <module>

  from . import add_newdocs

File \&quot;D:\\IntelPython35\\lib\\site-packages\\numpy\\add_newdocs.\

py&quot;, line 13, in <module>

  from numpy.lib import add_newdoc

File \&quot;D:\\IntelPython35\\lib\\site-packages\\numpy\\lib\\__init__\

.py&quot;, line 8, in <module>

  from .type_check import *

File \&quot;D:\\IntelPython35\\lib\\site-packages\\numpy\\lib\\type_\

check.py&quot;, line 11, in <module>

  import numpy.core.numeric as _nx

File \&quot;D:\\IntelPython35\\lib\\site-packages\\numpy\\core\\__init_\

_.py&quot;, line 21, in <module>

  from . import umath

ImportError: DLL load failed: ÕÒ.b2»[Micro]½Ö¸[Paragraph]¨
[Micro]Ä.b3ÌÐò¡£

"|>

What is wrong? How can I make NumPy work with RunProcess?


Update

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


Update

A workaround.

I uninstalled Intel Python and install Anaconda.

Now it is working. Maybe Intel Python misses something in the PATH.

Peter Mortensen
  • 759
  • 4
  • 7
matheorem
  • 17,132
  • 8
  • 45
  • 115
  • 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 07 '17 at 04: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 https://pastebin.com/2Zscj75e – matheorem Sep 07 '17 at 05:11

3 Answers3

12

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"|>]

Mathematica graphics

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.

Edmund
  • 42,267
  • 3
  • 51
  • 143
  • 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 08 '17 at 15:30
  • @matheorem Did you configure Python as in the tutorial setup? – Edmund Oct 08 '17 at 16:04
  • yeah. I think I did. I installed conda install pyzmq, also pip install zmq – matheorem Oct 08 '17 at 16:14
  • @matheorem See this post (155821). – Edmund Oct 08 '17 at 17:01
0

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

b3m2a1
  • 46,870
  • 3
  • 92
  • 239
  • I seems not working for me. I want to know If you don't set ProcessEnvironment, the example will work or not? – matheorem Sep 07 '17 at 05: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 07 '17 at 05:41
  • Yeah, the problem is that I don't which path to add : ) – matheorem Sep 07 '17 at 05:48
  • @matheorem unfortunately me neither. Hopefully someone with a Windows machine can help you out here. – b3m2a1 Sep 07 '17 at 05:56
  • I appreaciate your help : ) – matheorem Sep 07 '17 at 06:23
0

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 two 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" -> ""|> *)

Peter Mortensen
  • 759
  • 4
  • 7
LouisB
  • 12,528
  • 1
  • 21
  • 31
  • 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 08 '17 at 00:10