Python API cannot be built with setup.py in R2021a

I'm trying to get the matlab engine python API to work as I have a long piece of python code which inherits a single call to a MATLAB function.
For sanitary reasons, I want to couple to API to one of my virtual anaconda environments. As I'm using the R2021a release I've started by creating a new conda environment with python 3.8 as per the compatibility requirements Versions of Python Compatible with MATLAB Products by Release - MATLAB & Simulink (mathworks.com), and installing no further libraries. Afterwards a python --version reveals this installed Python 3.8.16.
In MATLAB I've confirmed the license is activated by running
license('inuse')
which returns "matlab" as I went with a complete blank installation after multiple failed attempts.
When trying to actually built the engine, I'm aware I have to specify the associated python directory with the virtual environment as explained in Install MATLAB Engine API for Python in Nondefault Locations - MATLAB & Simulink - MathWorks Benelux as the default Python environment runs in an incompatible version, so I execute
"python setup.py install --prefix="C:<my_anaconda_path>\envs\matlab-api-env"
which returns the following lengthy error message
=============================================================================================================
C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\dist.py:520: SetuptoolsDeprecationWarning: Invalid version: 'R2021a'.
!!
********************************************************************************
The version specified is not a valid version according to PEP 440.
This may not work as expected with newer versions of
setuptools, pip, and PyPI.
By 2023-Sep-26, you need to update your project and remove deprecated calls
or your builds will no longer be supported.
See https://peps.python.org/pep-0440/ for details.
********************************************************************************
!!
self._validate_version(self.metadata.version)
C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\_distutils\cmd.py:66: SetuptoolsDeprecationWarning: setup.py install is deprecated.
!!
********************************************************************************
Please avoid running ``setup.py`` directly.
Instead, use pypa/build, pypa/installer, pypa/build or
other standards-based tools.
See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details.
********************************************************************************
!!
self.initialize_options()
C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\_distutils\cmd.py:66: EasyInstallDeprecationWarning: easy_install command is deprecated.
!!
********************************************************************************
Please avoid running ``setup.py`` and ``easy_install``.
Instead, use pypa/build, pypa/installer, pypa/build or
other standards-based tools.
See https://github.com/pypa/setuptools/issues/917 for details.
********************************************************************************
!!
self.initialize_options()
C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\command\egg_info.py:131: SetuptoolsDeprecationWarning: Invalid version: 'R2021a'.
!!
********************************************************************************
Version 'R2021a' is not valid according to PEP 440.
Please make sure to specify a valid version for your package.
Also note that future releases of setuptools may halt the build process
if an invalid version is given.
By 2023-Sep-26, you need to update your project and remove deprecated calls
or your builds will no longer be supported.
See https://peps.python.org/pep-0440/ for details.
********************************************************************************
!!
return _normalization.best_effort_version(tagged)
Traceback (most recent call last):
File "setup.py", line 80, in <module>
setup(
File "C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\_distutils\core.py", line 185, in setup
return run_commands(dist)
File "C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\_distutils\core.py", line 201, in run_commands
dist.run_commands()
File "C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\_distutils\dist.py", line 969, in run_commands
self.run_command(cmd)
File "C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\dist.py", line 1244, in run_command
super().run_command(command)
File "C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\_distutils\dist.py", line 988, in run_command
cmd_obj.run()
File "C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\command\install.py", line 80, in run
self.do_egg_install()
File "C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\command\install.py", line 129, in do_egg_install
self.run_command('bdist_egg')
File "C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\_distutils\cmd.py", line 318, in run_command
self.distribution.run_command(command)
File "C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\dist.py", line 1244, in run_command
super().run_command(command)
File "C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\_distutils\dist.py", line 987, in run_command
cmd_obj.ensure_finalized()
File "C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\_distutils\cmd.py", line 111, in ensure_finalized
self.finalize_options()
File "C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\command\bdist_egg.py", line 92, in finalize_options
ei_cmd = self.ei_cmd = self.get_finalized_command("egg_info")
File "C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\_distutils\cmd.py", line 305, in get_finalized_command
cmd_obj.ensure_finalized()
File "C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\_distutils\cmd.py", line 111, in ensure_finalized
self.finalize_options()
File "C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\command\egg_info.py", line 219, in finalize_options
parsed_version = packaging.version.Version(self.egg_version)
File "C:\Users\s162286\AppData\Local\anaconda3\envs\matlab-api-env\lib\site-packages\setuptools\_vendor\packaging\version.py", line 197, in __init__
raise InvalidVersion(f"Invalid version: '{version}'")
setuptools.extern.packaging.version.InvalidVersion: Invalid version: 'R2021a'
=============================================================================================================
So it seems that the official way of creating the MATLAB-python API through the setup.py script is depracated in the default setuptools (ver 67.2.2.) that comes with a fresh python 3.8 installation. The suggested alternative based to use pip install instead (based on Why you shouldn't invoke setup.py directly (ganssle.io)), is only supported for R2022b and upwards according to Install MATLAB Engine API for Python - MATLAB & Simulink - MathWorks Benelux (and trying to do so despite the lack official support still generates a similar error surrounding SetuptoolsDeprecationWarning: Invalid version: 'R2021a').
Any help would be appreciated.

Answers (2)

This can be addressed fairly simply.
Edit matlabroot/extern/engines/python/setup.py. At the end of the file in the setup() call, change the version= argument to the numeric version found in matlabroot/VersionInfo.xml.
This has been fixed in later versions of MATLAB (setup.py now has a PEP 440-compatible version number)
This issue seems to occur when you try to install the python package generated from MATLAB in Anaconda based virtual environments. The generated package works completely fine inside virtual environments created through methods other than conda like "pip". It is not guaranteed that the library compiler generated code will install fine inside Conda based virtual environments.
However, it is recommended to use the "py" command instead of the "python" command inside the Conda virtual environment terminal to make it work if you have some Python installation other than Anaconda in the computer.
"py" command:
py setup.py install
The "py" command python launcher which is a utility that comes with Python installations on Windows. It gets installed into "C:\Windows\" so it’s available without requiring path modifications.

3 Comments

Thank you very much for the reply and a clarification on why the error occurs.
In the meantime I have found another solution which also works in Anaconda environments which I'll leave down below just in case someone else with the same issue finds this thread.
===================================================================================
The python API package exists on Python Package Index (PyPi) library so it can be pulled and installed from there using a standard pip install. By default this will pull the most recent version, of course compatible with most recent MATLAB release. As PyPI is like an archive, it is possible to pull an older version that is compatible with older MATLAB versions (in case you can't or just don't want to upgrade to the latest release).
To do this it is important to know which MATLAB version you have, which differs from the colloquial release name (e.g. R2023a for the most recent version). This can be found e.g. in Windows command prompt by
matlab -help
the last line of which will yield the version (in my case for R2021a this is 9.10.0,1577079). As this version number corresponds to the version of the matlabengine python package, it can now be installed with
pip install matlabengine==9.10
(note running exactly this will reproduce an error as there is no vanilla 9.10 distribution that can be matched; however pip will provide a list of existing distributions from which one can pick the correct 9.10.X version, or one can pick a suitable distribution directly from the list of matlabengine version history on PyPI). It is however highly recommened to do this only in a contained virtual environment due to the clashes when modules require modifications (see e.g. the Conda documentation about this issue).
I really would like to know how you later install the Python API. For me, even after I install the matlabengine version properly, I still get the same error. If you could give more detail that could be really helpful!
It has been a while since I figured it out, so I don't recall any further hiccups occuring. After using the pip install with proper compatible versioning, the API correctly worked for me.
If you are trying to install the API in a virtual Anaconda environment, I'd strongly suggest starting over, i.e. deleting the environment and creating it again from scratch. It could be some leftover files from running
python setup.py install --prefix="C:<my_anaconda_path>\envs\matlab-api-env
before, which have gone rogue and are clashing with the package you are trying to install through "pip" (which is precisely why it it strongly recommended to use pip ONLY in a separate virtual Anaconda environment, as pip version control is different from Anaconda's, so it might not detect the leftover files).
Without much details, like which python/MATLAB version you use, whether the pip install is succesful, etc etc, I don't further know what could be going wrong.
Hope that fixed the issue for you.

Sign in to comment.

Products

Release

R2021a

Asked:

on 13 Jun 2023

Commented:

on 27 Jun 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!