Import python package macOS error
22 views (last 30 days)
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
File "/opt/miniconda3/lib/python3.8/site-packages/EPGAnalysisPkg/__init__.py", line 283, in <module>
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?
Priysha LNU on 7 Jan 2021
Edited: Priysha LNU on 7 Jan 2021
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:
obj = <pythonPackageName>.initialize()
result = obj.<functionName>(<arg1>, <arg2>, <arg3>)
result = obj.<functionName>.tryPyML(sys.argv, sys.argv, sys.argv)
%Then creat another python project where you wrap the mwpython and use it to run this lib:
os.system("/Applications/MATLAB_R20xxx.app/bin/maci64/mwpython3.7.app/Contents/MacOS/mwpython3.7 pythonScript.py '<arg1>' '<arg2>' '<arg3>'")
Hope this helps!
DISCLAIMER: These are my own views and in no way depict those of MathWorks.