Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB crashes on Linux when trying to import Python module

Every time I try to import a Python module from MATLAB (using py.importlib.import_module) it crashes. Sometimes the crash is immediate but other times it hangs for a while before crashing. Some modules seem to work, for example py.importlib.import_module('math') works fine, but py.importlib.import_module('random') causes a crash. I'm also unable to import custom modules.

This is the output of pyversion:

>> pyversion

       version: '3.7'
    executable: '/home/garden/anaconda3/bin/python3'
       library: '/home/garden/anaconda3/lib/libpython3.7m.so'
          home: '/home/garden/anaconda3'
      isloaded: 0

Any ideas how to fix this?

I'm not sure if this is helpful but I've narrowed it down to a particular line that's causing a crash, line 244 in the hashlib module:

240 for __func_name in __always_supported:
241     # try them all, some may not work due to the OpenSSL
242     # version not supporting that algorithm.
243     try:
244         globals()[__func_name] = __get_hash(__func_name)
245    except ValueError:
246         import logging
247         logging.exception('code for hash %s was not found.', __func_name)
like image 790
hoffee Avatar asked Dec 08 '25 16:12

hoffee


1 Answers

Per MathWorks support, this issue sometimes can arise when Python attempts to use the MathWorks version of libcrypto. This can be resolved by loading SSL using Python before attempting to import other modules. Adding these lines to the beginning of my MATLAB script seems to fix the problem for me:

py.sys.setdlopenflags(int32(10))
py.importlib.import_module('ssl')
like image 140
hoffee Avatar answered Dec 10 '25 09:12

hoffee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!