Python API cannot be built with setup.py in R2021a
51 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
Randall Pittman
2024-4-11
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)
Yoga
2023-8-2
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 个评论
Menger Chen
2023-9-11
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!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call MATLAB from Python 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!