0

I want to call Python modules or user-defined functions/scripts I built in python through Matlab. I'm aware of many of the things I need to cover before something like this is possible, I'll make a list of everything I've done so far.

  1. Created Virtual Environment with Anaconda and istalled all the dependencies I needed. Python version 3.5.2
  2. Ran the scripts using PyCharm and all runs good/as expected.
  3. When running pyversion(mypath) in Matlab, though it appears "loaded: 1", I can't call anything on the site-packages module. For example, I can't even call something like numpy (py.importlib.import_module('numpy')) because I get the following error:

Python Error: ImportError: Importing the multiarray numpy extension module failed. Most likely you are trying to import a failed build of numpy. If you're working with a numpy git repo, try git clean -xdf (removes all files not under version control). Otherwise reinstall numpy. Original error was: DLL load failed: The specified module could not be found.

I've verified everything I could so far. Python and Matlab (2016a) are both 64 bits. Although, I can run this small snippet of code with the expected outcome:

T = 'MATLAB(R) is a high-level language'
wrapped = py.textwrap.wrap(T);
whos wrapped

Since I can execute everything properly from PyCharm, and I can't from Matlab's side, I'm wondering if there's any restriction to enable from Matlab or Anaconda (in the configuration).

Is there a setting in the configuration of Anaconda that is maybe not allowing me to do this? Or is this an issue within Matlab? I'm not sure, I'm clueless.

merv
  • 67,214
  • 13
  • 180
  • 245
Aquiles Páez
  • 503
  • 6
  • 18
  • possible solution here : https://stackoverflow.com/questions/45848273/matlab-cant-call-python-module-with-imports – Kryesec Jan 16 '19 at 06:08
  • Otherwise, could be a numpy package issue. Try a reinstall of numpy package pip install --upgrade --force-reinstall numpy Source: https://github.com/numpy/numpy/issues/10714 – Kryesec Jan 16 '19 at 06:11
  • @Kryesec hi thanks for replying. I used numpy as an example, there are other dependencies facing the same issue. I know because I tried calling some user-defined functions that worked on PyCharm but when I get to call them from Matlab I get errors. With anything that's in the site-package folder. – Aquiles Páez Jan 16 '19 at 06:17
  • What exactly is `mypath`? E.g., `anaconda/envs//bin/python`? or something else? – merv Jan 17 '19 at 00:36
  • 1
    Could be worth checking out [this MATLAB utility](https://github.com/sg-s/condalab) that someone wrote for switching conda envs within MATLAB. Perhaps the code might also indicate the correct way to load env-specific packages. – merv Jan 17 '19 at 00:44
  • @merv "mypath" is where the python.exe is located in my environment, the interpreter path for Python. I tried undoing a couple of changes to the configuration. I ended up reinstalling Anaconda, setted up a new environment and it all worked good. But still can't understand what the issue was the last time. BTW: thanks for the link on switching conda for Matlab! – Aquiles Páez Jan 18 '19 at 02:01

0 Answers0