MATLAB: Python EngineError: Import argument ‘matlab.in​ternal.eng​ine.input’ cannot be found or cannot be imported.

engineerrorMATLABmatlab.internal.engine.inputpython

I'm unable to call Matlab from Python in on Windows10. Things were working with Matlab 2019a and Python 3.7 until about the time I installed the Matlab 2016b Python engine with Python 3.5 in another conda environment. I have tried creating new conda environments and even installing a new version of Matlab with no change.
To reproduce this issue:
(in the Administrator Anaconda Prompt window):
conda create -n env_name python=3.7
conda activate env_name
cd c:\Program Files\MATLAB\R2019b\extern\engines\python
python setup.py install
conda install spyder
Then open Spyder through the new Start menu item (under the name env_name). Then run a .py script that contains these lines:
import matlab.engine
eng = matlab.engine.start_matlab()
Note that the above two lines run directly in Spyder do work, but if I run them in a script they do not.
The full error I get is:
File "g:\users\dgthomp\gryphon2\radar\Radar\RTIF\sph2pickle.py", line 4, in <module>
myengine = matlab.engine.start_matlab()
File "C:\Users\dgthomp\.conda\envs\gryphon\lib\site-packages\matlab\engine\__init__.py", line 117, in start_matlab
eng = future.result()
File "C:\Users\dgthomp\.conda\envs\gryphon\lib\site-packages\matlab\engine\futureresult.py", line 67, in result
return self.__future.result(timeout)
File "C:\Users\dgthomp\.conda\envs\gryphon\lib\site-packages\matlab\engine\matlabfuture.py", line 87, in result
handle = pythonengine.getMATLAB(self._future)
EngineError: Import argument 'matlab.internal.engine.input' cannot be found or cannot be imported.
The error message isn't really helpful because there isn't and never (in my experience when this functionality was working in Matlab 2019a and Python 3.7) has been anything that can be imported as matlab.internal.engine.input. I have tried this with Matlab versions 2019a and 2019b with the same effect. I've searched for help with this question online and found things referring to permission issues (I find no evidence of permission issues, although I'm not sure how to confirm that Spyder or the python engine under it can see particular files) and to source files named matlab.py (I don't have any in my folder structure, although I do see some deep in the folder structure that conda installs by default – e.g. /mnt/c/ProgramData/Anaconda3/Lib/site-packages/pygments/lexers/matlab.py). A co-worker followed the same steps (including first trying to get Matlab 2016b to work with Python 3.5) and has the same result, starting from his first attept to connect Python3.7 to Matlab 2019b.
Part of the reason that we are still using Matlab (instead of switching entirely to Python) is because we pay for support. So I'm hoping to get some support for this issue.
Thanks

Best Answer

I ended up submitting a service request and got the support that helped me find the answer to this question. The real problem (or at least the most persistent problem) was that I had a Linux-configured pathdef.m file in my current working directory, so it tried (unsuccessfully) to use that and then didn't have a correct path set up. I was assuming that matlab.internal.engine.input was referring to a Python file when I said it was an invalid error message, but it really is a Matlab file. On my system, that file is at C:\Program Files\MATLAB\R2019b\toolbox\matlab\external\engines\engine_api\+matlab\+internal\+engine\input.m. So when I searched for any file whose name was like matlab/internal/engine, I wasn't able to find it because of the '+' signs in the actual path.
Related Question