6

Please do not mark this as a duplicate of how to call python and sklearn from matlab? as the question is in my opinion not really answered.
Since Matlab Release R2014b I think, it is possible to directly use python from matlab.
In short words, you only have to place py in front of the python call.
I my setup (after giving matlab the python path with the command pyversion('PATH_TO_PYTHON'), that is running fine. I can even use dask multiprocessing. Quite cool. For example, executing py.dask.distributed.Client results in

  Python Client with properties:

              asynchronous: 0
                   cluster: [1×1 py.distributed.deploy.local.LocalCluster]
         get_futures_error: [1×1 py.method]
                coroutines: [1×1 py.list]
            scheduler_file: [1×1 py.NoneType]
                      loop: [1×1 py.tornado.platform.select.SelectIOLoop]
    recreate_error_locally: [1×1 py.method]
                  refcount: [1×1 py.collections.defaultdict]
                extensions: [1×1 py.dict]
                 scheduler: [1×1 py.distributed.core.rpc]
                       rpc: [1×1 py.distributed.core.ConnectionPool]
                   futures: [1×1 py.dict]
            scheduler_comm: [1×1 py.distributed.batched.BatchedSend]
                    status: [1×7 py.str]
           connection_args: [1×1 py.dict]
                        id: [1×43 py.str]
                generation: [1×1 py.int]
                   io_loop: [1×1 py.tornado.platform.select.SelectIOLoop]
                  security: [1×1 py.distributed.security.Security]

    <Client: scheduler='tcp://127.0.0.1:59795' processes=4 cores=4>

Coming back to the question: I have sklearn installed and can use it from the referenced python Installation. It is working the same way as dask. But MATLAB R2017a is not able to find sklearn.
A similiar call to the given above py.sklearn.cluster.dbscan results in

Undefined variable "py" or class "py.sklearn.cluster.dbscan".

Is there any python expert being able to explain?

Bastian Ebeling
  • 1,138
  • 11
  • 38
  • 1
    Cannot reproduce. MATLAB R2017a works perfectly fine with sklearn here. No additional setup needed, just had to set the `pyversion` correctly. Are you 100% sure that you are using the same python version in the console and in MATLAB? Can you try creating a new virtual environment with sklearn and use that in MATLAB? – hbaderts Aug 30 '17 at 06:41
  • @hbaderts: Thanks for testing. I run Windows and have exactly one python all over the Computer. I'm exactly sure running/testing the same pyhton Versions (Anaconda 3 64 Bit). Which Setup are you running? – Bastian Ebeling Aug 30 '17 at 07:15

3 Answers3

6

I got a solution from the mathworks Support.
It reads the way, that maybe the python environment is not completely setup. I was asked to start matlab from within the Anaconda Prompt which has that complete arranged environment. Running matlab from there yielded the wanted results thus being able to use for example sklearn.
Further comparing the diffenrences from there showed up, that some more directories from python have to be added to the systems search path.

Further I learned, that running py.importlib.import_module(<MODULENAME>) will show details if that python module and its dependencies are available or not.

Bastian Ebeling
  • 1,138
  • 11
  • 38
2

On a Mac:

  • Open a new terminal window;

  • type: which python (to find out where the default version of python is installed);

  • Restart MATLAB;

  • type: pyversion('/anaconda2/bin/python'), in the command line (obviously replace with your path).
  • You can now run all the libraries in your default python installation.

For example:

>>py.sys.version;

>>py.sklearn.cluster.dbscan
user3666197
  • 1
  • 6
  • 50
  • 92
1

You can create a conda environment and use it from MATLAB as follows. Note that it you are debugging Python at the same time, and making changes to some_awesome_python_module, you have to reload it every time (code below starting with clear classes is how to do that):

py_root_useFromMATLAB = fileparts(C:\anaconda_3\envs\useFromMATLAB\_conda.exe);
ENV = getenv('PATH');
ENV = strsplit(ENV, ';');
items_to_add_to_path = {
    fullfile(py_root_useFromMATLAB, 'Library', 'mingw-w64', 'bin')
    fullfile(py_root_useFromMATLAB, 'Library', 'usr', 'bin')
    fullfile(py_root_useFromMATLAB, 'Library', 'bin')
    fullfile(py_root_useFromMATLAB, 'Scripts')
    };
ENV = [items_to_add_to_path(:); ENV(:)];
ENV = unique(ENV, 'stable');
ENV = strjoin(ENV, ';');
setenv('PATH', ENV);
clear classes
module_to_load = 'some_awesome_python_module';
python_module_to_use = py.importlib.import_module(module_to_load);
py.importlib.reload(python_module_to_use);

Now you can use it like:

output = py.some_awesome_python_module.some_awesome_python_function(input)
brethvoice
  • 350
  • 1
  • 4
  • 14
  • More details here: https://www.mathworks.com/matlabcentral/answers/443558-matlab-crashes-when-using-conda-environment-other-than-base#answer_486374 – brethvoice Aug 28 '20 at 16:34
  • My answer to a similar question about conda from MATLAB: https://stackoverflow.com/a/63671567/12763497 – brethvoice Aug 31 '20 at 13:17