Mathematica Asked on June 24, 2021
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:wsimple.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:wsimple.py"}]
gives
<|"ExitCode" -> 1, "StandardOutput" -> "",
"StandardError" -> "Traceback (most recent call last):
File "E:wWORKpythonlearningsimple.py", line 1, in
<module>
import numpy as np
File "D:IntelPython35libsite-packagesnumpy__init__.py"
, line 158, in <module>
from . import add_newdocs
File "D:IntelPython35libsite-packagesnumpyadd_newdocs.
py", line 13, in <module>
from numpy.lib import add_newdoc
File "D:IntelPython35libsite-packagesnumpylib__init__
.py", line 8, in <module>
from .type_check import *
File "D:IntelPython35libsite-packagesnumpylibtype_
check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "D:IntelPython35libsite-packagesnumpycore__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.
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
Answered by b3m2a1 on June 24, 2021
I installed IntelPython3 on Windows 7 and this worked (with Mathematica 10.1) for me:
SetDirectory["UsersgaussDocumentsPython Scripts"]
RunProcess[{"C:IntelPython3python.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:IntelPython3python.exe", "Python Scriptssimple.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 FilesWolfram
ResearchMathematica10.1SystemFilesLibrariesWindows-x86-64",
"C:Program FilesWolfram
ResearchMathematica10.1SystemFilesLibrariesWindows",
"C:Program FilesWolfram
ResearchMathematica10.1SystemFilesKernelBinariesWindows-
x86-64",
"C:Program FilesWolfram ResearchMathematica10.1",
"C:Program FilesWolfram
ResearchMathematica10.1SystemFilesFrontEndBinariesWindows-
x86-64",
"C:Program FilesWolfram
ResearchMathematica10.1SystemFilesKernelBinariesWindows-
x86-64",
"C:Windowssystem32",
"C:Windows",
"C:WindowsSystem32Wbem",
"C:WindowsSystem32WindowsPowerShellv1.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:IntelPython3python.exe", "Python Scriptssimple.py"}]
(*
<|"ExitCode" -> 0,
"StandardOutput" ->
"C:IntelPython3Librarybin;C:IntelPython3DLLs;C:Program
FilesWolfram
ResearchMathematica10.1SystemFilesLibrariesWindows-x86-64;C:
Program FilesWolfram
ResearchMathematica10.1SystemFilesLibrariesWindows;C:
Program FilesWolfram
ResearchMathematica10.1SystemFilesKernelBinariesWindows-
x86-64;C:Program FilesWolfram
ResearchMathematica10.1;C:Program FilesWolfram
ResearchMathematica10.1SystemFilesFrontEndBinariesWindows-
x86-64;C:Program FilesWolfram
ResearchMathematica10.1SystemFilesKernelBinariesWindows-
x86-64;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;
C:WindowsSystem32WindowsPowerShellv1.0
[1 2 3]
", "StandardError" -> ""|>
*)
Answered by LouisB on June 24, 2021
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.
Answered by Edmund on June 24, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP