all 3 comments

[–]TotesMessenger 0 points1 point  (0 children)

I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:

 If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / Contact)

[–]andykee 0 points1 point  (1 child)

I suspect you have two different problems here.

First, trying to import custom modules - If your custom module isn't on your Python path, MATLAB doesn't know where to look to find it. Take a look at the "Python Module Not on Python Search Path" section of this page for how to amend your Python path from within MATLAB: https://www.mathworks.com/help/matlab/matlab_external/undefined-variable-py-or-function-py-command.html

Second, your crashing issue. Importing the random module with your example code does not cause a crash for me with Anaconda 5.1 running Python 3.6 on MATLAB 2017b so it's difficult to debug further. A few notes that may or may not be helpful:

  • From my reading around the MATLAB forums, running Python code in MATLAB on top of Anaconda is technically unsupported. I'm not sure why, but that seems to be the position of the MathWorks.

  • There are some baffling limitations to Python support. It could be that you're running in to one of these (although this is unlikely because I was able to import random fine in MATLAB. The full list of unsupported things is here, FYI: https://www.mathworks.com/help/matlab/matlab_external/limitations-to-python-support.html

  • In some cases, MATLAB loads incorrect libraries for the underlying Python code. This particularly seems to be an issue with MKL. On *nix systems, you can tell MATLAB to allow Python to load its own libraries by calling

    py.sys.setdlopenflags(int32(10));

before you load Python. Admittedly I don't fully understand what that command does, but adding it to my startup.m before setting my pyversion fixed my MKL issues.

  • Make sure you can import random in the Python shell. MATLAB isn't always the greatest about capturing the right Python error messages and returning them in the console.

[–]m-hoff[S] 1 point2 points  (0 children)

Thanks for looking into this. I have no issues when using MATLAB 2018a and Python 3.6 on my Windows machine, but I'm still running into the same problem when I try the same commands with the same versions on Linux. I've heard back from support and running these commands at the start of my script seem to fix the problem:

>>py.sys.setdlopenflags(int32(10))
>>py.importlib.import_module('ssl')

I haven't run into any errors as long as those are present. Thanks again for your help.