Import python package macOS error

조회 수: 5 (최근 30일)
Anny Hang
Anny Hang 2020년 11월 30일
편집: Priysha LNU 2021년 1월 7일
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
marco lin
marco lin 2020년 12월 4일
same here !!
I'm not sure what is going on here.
does anyone else have the same issue and know how to fix it ?

댓글을 달려면 로그인하십시오.

답변 (1개)

Priysha LNU
Priysha LNU 2021년 1월 7일
편집: Priysha LNU 2021년 1월 7일
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.

카테고리

Help CenterFile Exchange에서 Python Package Integration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by