JCV
JCV

Reputation: 515

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?

Upvotes: 0

Views: 5264

Answers (4)

dML
dML

Reputation: 49

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

Upvotes: 0

capuchin7
capuchin7

Reputation: 57

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

Upvotes: 1

JCV
JCV

Reputation: 515

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.

Upvotes: 0

Paolo
Paolo

Reputation: 25999

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']

Upvotes: 2

Related Questions