'How to run Python from MATLAB

I'm trying to run Python from MATLAB, but when I try I got the message:

py.list({'Monday','Tuesday','Wednesday','Thursday','Friday'})

Unable to resolve the name py.list.

I also have tried pyenv

pyenv
ans = 

  PythonEnvironment with properties:

          Version: "3.7"
       Executable: "C:\Users\Familia\.conda\envs\matlab\python.exe"
          Library: "C:\Users\Familia\.conda\envs\matlab\python37.dll"
             Home: "C:\Users\Familia\.conda\envs\matlab"
           Status: NotLoaded
    ExecutionMode: InProcess

Could be my problem the Status? NotLoaded? How can I change that for Loaded? Since this path is with Conda, I also tried to change for another Python version I have installed without Anaconda, with this command:

pe = pyenv('Version','C:\Users\Familia\AppData\Local\Microsoft\WindowsApps\python.exe')

Error using pyenv
Path argument does not specify a valid executable.

How can I overcome that?



Solution 1:[1]

Well, you are seeing that error because the path you specified is not valid.

Open a Command prompt window and type:

where python

this will return a list of locations of Python installations on your machine. On my machine, the command returns:

C:\Users\paolo\AppData\Local\Programs\Python\Python36\python.exe

so we can use this location when calling pyenv in MATLAB:

>> pyenv('Version','C:\Users\paolo\AppData\Local\Programs\Python\Python36\python.exe')

then I can use py.list:

>> py.list({'Monday','Tuesday','Wednesday','Thursday','Friday'})

ans = 

  Python list with no properties.

    ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']

Solution 2:[2]

It worked for me by using the path from a created conda environment exclusively having python 3.7 for Matlab 2021a. So the steps were:

  1. conda create --name matlabpy python=3.7 #You can give any name to the env

  2. Add the path where this "matlabpy" environment is located by clicking in "Set Path" in Matlab

  3. Run py.list({'Monday','Tuesday','Wednesday','Thursday','Friday'})

Hope it works for you as well

Solution 3:[3]

I manage to overcome that uninstalling python and installing again the 64bit, and during the installation, I chose to add python to the path environment and install for all users. That works for me.

Solution 4:[4]

For people working on a network where the MATLAB you are using is installed on another users account you need to share your user's folder with the account that MATLAB is installed on.

Steps:

  1. go to C:\Users and right click on the folder with your username
  2. go to the Sharing tab and click Share...
  3. click on the arrow next to Add, and select Find people...
  4. Search for the user that owns the MATLAB you're using and select it from the search results, then OK
  5. Restart MATLAB

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paolo
Solution 2 capuchin7
Solution 3 Jeniffer Barreto
Solution 4 dML