Matlab system and ! commands point to wrong python version?

3 ビュー (過去 30 日間)
Mark Davidson
Mark Davidson 2021 年 10 月 12 日
回答済み: Steven Baird 2024 年 1 月 30 日
I am trying to run python programs using the system function in Matlab. In spite of changing the PythonEnvironment using;
>> pe=pyenv('Version','/opt/anaconda3/bin/python3.8')
pe =
PythonEnvironment with properties:
Version: "3.8"
Executable: "/opt/anaconda3/bin/python3.8"
Library: "/opt/anaconda3/lib/libpython3.8.dylib"
Home: "/opt/anaconda3"
Status: NotLoaded
ExecutionMode: InProcess
When using the ! or system comands, Matlab still utilises the old Python vesion. This is illustrated below...
>> pyenv
ans =
PythonEnvironment with properties:
Version: "3.8"
Executable: "/opt/anaconda3/bin/python3.8"
Library: "/opt/anaconda3/lib/libpython3.8.dylib"
Home: "/opt/anaconda3"
Status: NotLoaded
ExecutionMode: InProcess
>> ! python --version
Python 2.7.16
>> system('python --version')
Python 2.7.16
ans =
0
How can I get these commands to access my python 3.8?

採用された回答

Walter Roberson
Walter Roberson 2021 年 10 月 12 日
This is expected. Using ! or system() find the python that is first in your system path, which might not be the same one you have configured MATLAB to use. Using the pyenv() call does not reconfigure your PATH environment variables.
You might need to do something like
!PATH="/opt/anaconda3/bin:$PATH" python -version
I do not know enough about python to know whether you need to configure an environment variable for the library.
  1 件のコメント
Mark Davidson
Mark Davidson 2021 年 10 月 13 日
Thank you! This worked very well.
>> myPath='/opt/anaconda3/bin:$PATH'
system(['export PATH=' myPath ' ; ','python --version']);
myPath =
'/opt/anaconda3/bin:$PATH'
Python 3.8.8

サインインしてコメントする。

その他の回答 (1 件)

Steven Baird
Steven Baird 2024 年 1 月 30 日
This worked for me:
>> system('python --version');
Python 3.12.0

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by