7
$\begingroup$

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

      from . import add_newdocs

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

      from numpy.lib import add_newdoc

    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

  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.

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – b3m2a1
    Commented Sep 7, 2017 at 4:19
  • $\begingroup$ 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 $\endgroup$
    – matheorem
    Commented Sep 7, 2017 at 5:11

3 Answers 3

12
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ 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." $\endgroup$
    – matheorem
    Commented Oct 8, 2017 at 15:30
  • $\begingroup$ @matheorem Did you configure Python as in the tutorial setup? $\endgroup$
    – Edmund
    Commented Oct 8, 2017 at 16:04
  • $\begingroup$ yeah. I think I did. I installed conda install pyzmq, also pip install zmq $\endgroup$
    – matheorem
    Commented Oct 8, 2017 at 16:14
  • $\begingroup$ @matheorem See this post (155821). $\endgroup$
    – Edmund
    Commented Oct 8, 2017 at 17:01
0
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ I seems not working for me. I want to know If you don't set ProcessEnvironment, the example will work or not? $\endgroup$
    – matheorem
    Commented Sep 7, 2017 at 5:39
  • $\begingroup$ 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. $\endgroup$
    – b3m2a1
    Commented Sep 7, 2017 at 5:41
  • $\begingroup$ Yeah, the problem is that I don't which path to add : ) $\endgroup$
    – matheorem
    Commented Sep 7, 2017 at 5:48
  • $\begingroup$ @matheorem unfortunately me neither. Hopefully someone with a Windows machine can help you out here. $\endgroup$
    – b3m2a1
    Commented Sep 7, 2017 at 5:56
  • $\begingroup$ I appreaciate your help : ) $\endgroup$
    – matheorem
    Commented Sep 7, 2017 at 6:23
0
$\begingroup$

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" -> ""|>
*)
$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – matheorem
    Commented Sep 8, 2017 at 0:10

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.