apommel / vscode-matlab-interactive-terminal

VS Code extension allowing for an interactive Matlab terminal and launching of scripts through Matlab Engine for Python
MIT License
46 stars 10 forks source link

Troubles when using Python 3.7+ added to PATH #1

Closed igorjrd closed 4 years ago

igorjrd commented 4 years ago

I was trying to install the Matlab Engine API for Python following the installation instructions available in the URL given in extenstion README. Unfortunatelly, seems like MATLAB Engine API for Python does not support Python 3.7+ (some errors are raised informing that the script is not compatible with Python Version). As the extension currently runs just the Python added to PATH variables, users that have Python 3.7+ added to PATH may have problems to start the interactive MATLAB terminal. Do you have any suggestion of approach to fix this trouble?

I think that would be nice if users were able to specify (at extenstion configurations) a Python executable or a command to activate a python virtual environment in which Python 2 (or a Python 3.6-) is installed.

apommel commented 4 years ago

Thanks for your feedback. It works properly for me with Python 3.7.x, but indeed the Engine API does not support that many Python versions, including the latest 3.8 releases. It should be possible to add an option to configure the path to the desired python executable, or a environment activation in case of using Anaconda (the problem is that in this case the terminal used in VS Code should be conda initialized beforehand).

I will try to see what works best, but I might have to trouble to give an update quickly as I am currently in China. Thanks for your patience.

If you are using Anaconda, you could try changing your default environment to the one that is working while the option is not implemented.

apommel commented 4 years ago

Still, according to MathWorks and my own experience (I developed the extension using exclusively Python 3.7), the Matlab Engine API should be compatible with Python 3.7. Could you share your error message more precisely and your environment configuration if possible?

igorjrd commented 4 years ago
import matlab.engine
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\MATLAB\R2016a\extern\engines\python\dist\matlab\engine\__init__.py", line 82
    def start_matlab(option="-nodesktop", async=False):

I tried to manually change async args to _async, because async has became a reserved word. When I tried this I got:

import matlab.engine
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\MATLAB\R2016a\extern\engines\python\dist\matlab\engine\__init__.py", line 36, in <module>
    raise EnvironmentError("Python %s is not supported." % _version)
OSError: Python 3_7 is not supported.

I am using MATLAB R2016a, Python3.7.3 (using conda base environment as default python) in Win10 64x

apommel commented 4 years ago

Indeed, Python 3.7 was not released at the time of Matlab R2016a, so it must be unsupported by this version of the Matlab Engine API. Changing the keyword was a good reflex, maybe the missing quickfix would be to add 3.7 in the _supported_versions variable of the file "C:\Program Files\MATLAB\R2016a\extern\engines\python\dist\matlab\engine\__init__.py". Assuming the code didn't change, this would solve your problem, but this could also well not be enough. In this case, an obvious solution would be to update Matlab to the latest version if it is possible on your side. I will try to find time to implement the Python path option in the meanwhile.

apommel commented 4 years ago

@igorjrd The latest pre-release (v0.2.7) has been added to the Marketplace. It adds an option to specify the Python executable to use, I believe it should solve your problem. Tell me if you find any issue, I could not test it extensively.

igorjrd commented 4 years ago

@apommel My sincerelly thanks for this addition to the extension. It's everything working fine, but I found a possible improvement that can be added:

In Python 2 the input() function seems to evaluates the user entries as python code instead of get entries as strings. If an extension user runs the MATLAB terminal with python 2, the expressions are correctly evaluated by MATLAB just if the user writes it surrounded by double quoes (ex.: "a = [1,2,3]" works while a = [1,2,3] does not works). If the entries are not surrounded by double quotes, the Python interpreter evaluates it and possibly break the execution of interactive loop due raised errors. The fix for it is quite simple: when python version is 2 the user entries should be collected with raw_input() instead of input().

apommel commented 4 years ago

I didn't plan on making it Python 2 compatible as it is officially discontinued and I don't have much experience with it but if it is that simple I will try to add it. I won't make it officially supported but a basic compatibility would be nice, thanks for the heads-up! I will close this issue now as this is resolved, thanks for your help!