Calling Python from Matlab / Can I use Python installed by Anaconda? / "NULL pointer returned from Python."

48 次查看(过去 30 天)
Would love to use some Python code a colleague wrote. I already have Anaconda installed, so I really don't want to install another version of Python (and associated libraries).
Tried:
pe = ...
pyenv('Version',...
fullfile('e:\', 'anaconda3', 'python.exe'));
myPythonVersion = pe.Version
py.print("Hello, Python!")
Got:
>> pe = ...
pyenv('Version',...
fullfile('e:\', 'anaconda3', 'python.exe'));
myPythonVersion = pe.Version
py.print("Hello, Python!")
myPythonVersion =
"3.8"
NULL pointer returned from Python.
>> py
>> pe
pe =
PythonEnvironment with properties:
Version: "3.8"
Executable: "e:\anaconda3\python.exe"
Library: "e:\anaconda3\python38.dll"
Home: "e:\anaconda3"
Status: NotLoaded
ExecutionMode: OutOfProcess
>>
Matlab Version R2020a (happy to upgrade, just too lazy right now...)

采纳的回答

Brian
Brian 2022-9-30
编辑:Brian 2022-9-30
Not sure what the best answer is for this issue.
Due to other circumstances, I needed to re-install Matlab and Anaconda. Now everything works:
>> pe = ...
pyenv('Version',...
fullfile('c:\', 'temp', 'anaconda3', 'python.exe'));
>> py.print("Hello, Python!")
Hello, Python!
Is this due to:
  • Re-installing Matlab?
  • Installing the latest version of Matlab (R2022b)?
  • Re-installing Anaconda?
  • Putting Anaconda on the C:\ ?
sorry, I don't know.
What I do know is that launching from the Anaconda prompt is not required.

更多回答(1 个)

Al Danial
Al Danial 2022-9-30
Try this start-up sequence: first open an Anaconda Prompt or Anaconda PowerShell terminal. Then within that terminal window, type the path to the matlab executable (or is it a launch .bat file on Windows?). Or just type 'matlab' if it is in your system-wide search path.
Then within matlab just do
>> pyenv
with no arguments to see if matlab figured everything out on its own.

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by