MHKiT-Software / MHKiT-MATLAB

MHKiT-MATLAB provides the marine renewable energy (MRE) community tools for data processing, visualization, quality control, resource assessment, and device performance.
https://mhkit-software.github.io/MHKiT/
BSD 3-Clause "New" or "Revised" License
15 stars 23 forks source link

Failure to load Python modules #68

Closed Matthew-Boyd closed 2 years ago

Matthew-Boyd commented 2 years ago

Running the function circular(30) to test for a successful installation results in a few different errors, depending on the state of the Python environment. Errors thrown on call to: _py.importlib.importmodule('mhkit');

No errors occurred when running the same function in Python (MHKiT-MATLAB)

Environment Python = 3.9 Fresh Conda virtual environment (not base environment)

Errors _Error using _backendtk> (line 22) _Python Error: RuntimeError: Failed to load TkFindPhoto or _Python Error: RuntimeError: Failed to load TclSetVar or Importing the numpy C-extensions failed

Matthew-Boyd commented 2 years ago

These errors eventually resolved while troubleshooting, intermittently came back, then seemingly resolved permanently on their own. Creating a new Conda environment, clearing all added system environment variables, uninstalling packages in the base environment, reverting changed MATLAB settings, etc. could not reproduce the original errors...

Things tried around the time problem resolved:

  1. Setting the following Windows User Environment variables:
    1. CONDA_DLL_SEARCH_MODIFICATION_ENABLE = 1
    2. PYTHONUNBUFFERED = 1
  2. Adding the following to the Windows System Environment Path variable: C:\Users\\AppData\Local\Continuum\miniconda3\envs\mhkit\Library\bin (where mhkit is the name of your conda environment and the path is to the environment's libraries)
  3. Setting pyversion() in MATLAB to ...\pythonw.exe instead of ...\python.exe
  4. Changing the execution mode to InProcess, via: pyenv('ExecutionMode','InProcess')
  5. Importing matplotlib and tkinter before running code that uses it, e.g.: py.importlib.import_module('matplotlib') py.importlib.import_module('tkinter') [x,y]=circular(30)

Note MATLAB staff do report that "MATLAB does not support Anaconda Python. To call Python libraries from MATLAB, you need to install a supported version of the reference implementation (CPython) for Python" [link]

See this link for installing CPython alongside Anaconda.

rpauly18 commented 2 years ago

@Matthew-Boyd Thank you for documenting this issue and the possible fixes. Since this is resolved and not reproducible, I am going to close this issue. We can re-open it if these or similar issues appear in the future.