pyenv Library property empty. Unable to resolve the name (Linux:Pop OS 22.04, python 3.9, R2022a)

90 views (last 30 days)
Hello All,
I've hit a wall trying to call python from MATLAB.
Since, MATLAB R2022a supports python 3.8 and 3.9 (ref) I've installed python3.9 via my system's package manager. (Note that python 3.10 was installed first, and is used system wide)
However, Python 3.9 is definitly working
$which python3.9
/usr/bin/python3.9
$python3.9 --version
Python 3.9.13
Trying to configure pyenv in MATLAB(following these instructions), yeilds:
>>pyenv('Version','/usr/bin/python3.9')
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "/usr/bin/python3.9"
Library: ""
Home: "/usr"
Status: NotLoaded
ExecutionMode: InProcess
Note, the library propery of the pyenv object is empty.
Then trying to force python to load by calling a simple python function yields:
>> py.print("Printed from python")
Unable to resolve the name 'py.print'.
I was able to replicate this issue on Ununtu 22.04 also with python3.9.
The closest related post I've found is on OSX, in which the user had a simmilar empty propery issue.
Can anyone advise on how to correct this, and get python calls working?
Thank you!

Accepted Answer

Jorian Khan
Jorian Khan on 23 Jul 2022
Really thank you for your post! It inspired me!
TLDR: the library property can be set by editing matlab.mlsettings particualrily: /fsroot/settingstree/matlab/external/interfaces/python/settings.json
{
"attributes": {
"groupName": "python",
"readonly": false,
"validationFcn": {},
"visible": false
},
"path": "\/settingstree\/matlab\/external\/interfaces\/python",
"settings": [
{
"attributes": {
"readonly": false,
"runtimeDefaultFcn": {},
"settingValueValidator": {},
"validationFcn": {},
"visible": false
},
"hasValue": true,
"isUserDefined": false,
"isVector": true,
"name": "Version",
"value": "{\"mwdata\":[\"3.9\",\"\\\/usr\\\/bin\\\/python3.9\",\"\\\/usr\\\/bin\\\/python3.9\",\"\\\/usr\\\/lib\\\/x86_64-linux-gnu\\\/libpython3.9.so.1\"],\"mwsize\":[1,4],\"mwtype\":\"cell\"}"
}
]
}
Editing "value" to include /usr/lib/x86_64-linux-gnu/libpython3.9.so.1 as Library and /usr/bin/python3.9 as home fixed this issue!
Now calling pyenv yeilds:
>> pyenv
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "/usr/bin/python3.9"
Library: "/usr/lib/x86_64-linux-gnu/libpython3.9.so.1"
Home: "/usr/bin/python3.9"
Status: NotLoaded
ExecutionMode: InProcess
>> py.print('Printed from Python 3.9!')
Printed from Python 3.9!
>> pyenv
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "/usr/bin/python3.9"
Library: "/usr/lib/x86_64-linux-gnu/libpython3.9.so.1"
Home: "/usr/bin/python3.9"
Status: Loaded
ExecutionMode: InProcess
ProcessID: "487263"
ProcessName: "MATLAB"
Unfortunately, I don't think this setting can be eddited via the settings api as settings.matlab does not contain an 'external' property. i.e.:
>> s = settings
s =
SettingsGroup with properties:
slhistory: [1×1 SettingsGroup]
matlab: [1×1 SettingsGroup]
Simulink: [1×1 SettingsGroup]
>> s.matlab
ans =
SettingsGroup 'matlab' with properties:
colors: [1×1 SettingsGroup]
appdesigner: [1×1 SettingsGroup]
codeanalyzer: [1×1 SettingsGroup]
toolboxpathcache: [1×1 SettingsGroup]
editor: [1×1 SettingsGroup]
keyboard: [1×1 SettingsGroup]
commandwindow: [1×1 SettingsGroup]
appearance: [1×1 SettingsGroup]
latestgr: [1×1 SettingsGroup]
fonts: [1×1 SettingsGroup]
general: [1×1 SettingsGroup]
So settings.json must be editied manually, I guess.
Finally!
  1 Comment
Al Danial
Al Danial on 23 Jul 2022
That's impressive and well beyond what I knew about the Python interface in MATLAB, great info here for future reference!

Sign in to comment.

More Answers (1)

Al Danial
Al Danial on 22 Jul 2022
On my Ubuntu 20.04 system pyenv returns
>> pyenv
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "/usr/local/anaconda3/2021.05/envs/matpy/bin/python"
Library: "/usr/local/anaconda3/2021.05/envs/matpy/lib/libpython3.9.so"
Home: "/usr/local/anaconda3/2021.05/envs/matpy"
Status: Loaded
ExecutionMode: InProcess
ProcessID: "1527848"
ProcessName: "MATLAB"
so try finding the libpython3.9.so shared library and explicitly setting the Library field with that path. For example on my system that would be
>> pyenv('Library','/usr/local/anaconda3/2021.05/envs/matpy/lib/libpython3.9.so')
If your Python shared libraries for both 3.9 and 3.10 exist in the same directory I can see how that will cause you grief. Ideally you'd your 3.9 installation would live in its own directory. I don't know if apt install gives you that freedom if that's what you did. My recommendation is to uninstall 3.9 then install Anaconda Python since that lets you control the location.
  1 Comment
Jorian Khan
Jorian Khan on 23 Jul 2022
Hello,
Thanks taking the time to reply!
So, libpthon3.9.so.1 is definitly on my system. It's located at:
/usr/lib/x86_64-linux-gnu/libpython3.9.so.1
However, the Library property of PythonEnvironment cannot be set like you've described. From the documentation (here) vailid input arguments for pyenv are soley 'Version' and 'ExecutionMode'
So trying so set 'Library' yeids:
>>pyenv('Library', '/usr/lib/x86_64-linux-gnu/libpython3.9.so.1')
Error using pyenv
Invalid input Library. Key must be 'Version' or 'ExecutionMode'.
Also I'm not sure I want to go the anaconda route yet as the intended the target platform is admisitrated and only has python 3.10 and python 3.9.
Thank you for the solution sugestion!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!