Use the Library Compiler to package the matlab function file into a python package,but Import python package macOS error

7 visualizaciones (últimos 30 días)
I am using macbook M1 pro, macOS 12.5.1
I have created by Python package already, but I am not able to import it in Python. matlab file like this:
function s = myadd(a, b)
s = a + b;
end
The path to generate python package is as follows:
I first tried running 'import myadd' in jupyter notebook as it's my editor of choice, but I get the following error:
Exception caught during initialization of Python interface. Details: On the Mac, you must run mwpython rather than python to start a session or script that imports your package. For more details, execute "mwpython -help" or see the package documentation.
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Cell In[10], line 1
----> 1 import myadd
File ~/miniforge3/lib/python3.9/site-packages/myadd/__init__.py:297
295 try:
296 _pir = _PathInitializer()
--> 297 _pir.get_paths_from_os()
299 _pir.update_paths()
300 _pir.import_cppext()
File ~/miniforge3/lib/python3.9/site-packages/myadd/__init__.py:179, in _PathInitializer.get_paths_from_os(self)
177 if not path_elements:
178 if self.system == 'Darwin':
--> 179 raise RuntimeError('On the Mac, you must run mwpython rather than python ' +
180 'to start a session or script that imports your package. ' +
181 'For more details, execute "mwpython -help" or see the package documentation.')
182 else:
183 raise RuntimeError('On {0}, you must set the environment variable "{1}" to a non-empty string. {2}'.format(
184 self.system, self.path_var,
185 'For more details, see the package documentation.'))
RuntimeError: On the Mac, you must run mwpython rather than python to start a session or script that imports your package. For more details, execute "mwpython -help" or see the package documentation.
Is there another way to import the python package?

Respuestas (1)

Sai Sumanth Korthiwada
Sai Sumanth Korthiwada el 7 de Feb. de 2023
Hello 乙洁,
It is possible that this issue arises due to the IDE, in your case, jupyter notebook, not recognizing the correct Python Interpreter. Most IDEs (like Visual Studio) allow you to specify a Python interpreter. Try to specify "mwpython" as the interpreter in IDE at your end. Please note that you will need to specify the full path to "mwpython" when adding it as the interpreter.
"mwpython" is a bash script located at ‘matlabroot/bin/mwpython’. This script sets up the environment for the MCR and launches the default Python configured on your system
You can execute "mwpython" from your preferred IDE by creating a wrapper script in Python. This script can use a system command to execute the "mwpython" bash script, which can then call your main Python script.
If for some reason it requires the interpreter name to start with "python", you could write a script called "python" that is a symbolic link to "mwpython".
Then, you can write a wrapper which includes the "mwpython" call to your script/module with the arguments:
mwpython -m mymodule arg1 arg2
There should not be a difference in behavior between calling your script in a Windows setting with a regular Python interpreter, or when calling it within a Mac OS setting via "mwpython".
If you are looking to have "mwpython" use a different version other than default version, you can specify the desired version via "PYTHONHOME" environment variable. As stated in the mwpython documentation:
" If you want to use a specific version of Python, set the PYTHONHOME environment variable on your machine to point to the location of your desired Python installation prior to invoking mwpython."
I recommend to set PYTHONHOME according to your needs either before launching IDE or the project.
Hope this helps.
DISCLAIMER: These are my own views and in no way depict those of MathWorks.

Categorías

Más información sobre Python Package Integration en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by