MATLAB Answers

Import python package macOS error

5 views (last 30 days)
Anny Hang
Anny Hang on 30 Nov 2020
Edited: Priysha LNU on 7 Jan 2021
I am using macOS 10.14.
I have created by Python package already, but I am not able to import it in Python. I first tried running 'import Pkg' in Visual Studio Code as it's my editor of choice, but I get the following error:
Then, I tried importing the module using mwpython, and I also get the same error:
$./mwpython -m EPGAnalysisPkg
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.
Traceback (most recent call last):
File "/opt/miniconda3/lib/python3.8/runpy.py", line 185, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/opt/miniconda3/lib/python3.8/runpy.py", line 144, in _get_module_details
return _get_module_details(pkg_main_name, error)
File "/opt/miniconda3/lib/python3.8/runpy.py", line 111, in _get_module_details
__import__(pkg_name)
File "/opt/miniconda3/lib/python3.8/site-packages/EPGAnalysisPkg/__init__.py", line 283, in <module>
_pir.get_paths_from_os()
File "/opt/miniconda3/lib/python3.8/site-packages/EPGAnalysisPkg/__init__.py", line 169, in get_paths_from_os
raise RuntimeError('On the Mac, you must run mwpython rather than python ' +
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?
  1 Comment
marco lin
marco lin on 4 Dec 2020
same here !!
I'm not sure what is going on here.
does anyone else have the same issue and know how to fix it ?

Sign in to comment.

Answers (1)

Priysha LNU
Priysha LNU on 7 Jan 2021
Edited: Priysha LNU on 7 Jan 2021
Hi Anny,
It is possible that this issue arrises due to the IDE not recognizing the correct Python Interpreter. Most IDEs (like Visual Studio) allow you to specify a Python interpreter. See if you can 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.
The following link might be of help:
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".
"mwpython" uses the latest supported version of Python which is currently 3.7. If you are looking to have mwpython use a different 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.
In another note, an indirect way of achieving this requirement might be as follows:
Change
import <pythonPackageName>
obj = <pythonPackageName>.initialize()
result = obj.<functionName>(<arg1>, <arg2>, <arg3>)
to:
result = obj.<functionName>.tryPyML(sys.argv[1], sys.argv[2], sys.argv[3])
%Then creat another python project where you wrap the mwpython and use it to run this lib:
import os
os.chdir(<PythonScript path>)
os.system("/Applications/MATLAB_R20xxx.app/bin/maci64/mwpython3.7.app/Contents/MacOS/mwpython3.7 pythonScript.py '<arg1>' '<arg2>' '<arg3>'")
Hope this helps!
Thanks!
DISCLAIMER: These are my own views and in no way depict those of MathWorks.

Community Treasure Hunt

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

Start Hunting!