Query regarding importing Sunpy module in Pyhton and MATLAB

4 views (last 30 days)
So, through VS Code, I am able to generate the output of the python script, which involves SunPy module. Howver, even though I am getting the ciorrect outpu , in the Problem space of VS Code, I am getting error which says that "Sunpy can not be resolved" (thus, I am also getting the yellow underline under the Sunpy in my python script). Thus, through pip command I checked if the module is installed and according to it it is installed correctly.
The error in Sunpy:
Correct output:
Now, in Matlab, when through Live Task option , when I import this python file and try to run it does not shows the output and I am getting the error message which says that the Sunpy module can not be found.
Error in MATLAB:-
Also, I tried running this python file in MATLAB through open command, but it dispalys the same error.
Therefore, I have twor questions, first why I got the output in VS Code (if the module is not installed correctly and second how to resolve this error)
This my Python Script:
from sunpy.net import Fido
from sunpy.net import attrs as a
event_type = "FL"
tstart = "2024/05/07"
tend = "2024/05/15"
result = Fido.search(a.Time(tstart, tend),
a.hek.EventType(event_type),
a.hek.FL.GOESCls > "M1.0",
a.hek.OBS.Observatory == "GOES")
# Here we only show two columns due there being over 100 columns returned normally.
print(result.show("hpc_bbox", "refs"))
# It"s also possible to access the HEK results from the
# `~sunpy.net.fido_factory.UnifiedResponse` by name.
hek_results = result["hek"]
###################################################################
# We can also print the key names that correspond to the HEK
# parameters returned by the query.
# We only print every 10th key to avoid the output being too long.
print(hek_results.colnames[::10])
###################################################################
# The results returned contain a lot of information and we may
# only want to keep some main results such as start time,
# end time, peak time, GOES-class, and active region number.
# This can be done as so:
filtered_results = hek_results["event_starttime", "event_peaktime",
"event_endtime", "fl_goescls", "ar_noaanum"]
###################################################################
# ``hek_result`` is already sorted by date, if one wants to sort
# by magnitude, one can do the following:
# Sorting is done by using the flare class from "fl_goescls"
# By converting the flare class to a number using ord()
# and adding the flare strength, we can sort by value
by_magnitude = sorted(filtered_results, key=lambda x: ord(x['fl_goescls'][0]) + float(x['fl_goescls'][1:]), reverse=True)
for flare in by_magnitude:
print(f"Class {flare['fl_goescls']} occurred on {flare['event_starttime']}")
###################################################################
# These results can then be saved to a CSV file, or any other file
# format that `~astropy.table.Table` supports.
filtered_results.write("october_M1_flares.csv", format="csv")
-Thank You

Answers (1)

Adarsh
Adarsh on 28 May 2025
I understand that you are facing an import error while trying to import “sunpy” library in MATLAB interface for Python but facing no error while doing it in vs code.
This issue occurs when there are multiple python versions present in your personal computer and pip is using a different python version to install libraries when you call “pip install <library>” command.
To resolve this use the “/path/to/python -m pip install sunpy” command and replace the “/path/to/python” with the path of the python interpreter that you are using for pyenv.
Once you install the “sunpy” library using this command, restart the MATLAB and run “pyenv” command again to load the new python environment with modified changes.
Now you can import the “sunpy” module in MATLAB and use it as required.
Secondly, the error that you are facing while executing the command “help import_module” occurs because, the “importlib” is a python library and “import_module” routine is a part of this “importlib” library.
Any python library that has to be accessed in MATLAB is called by using the “py” variable. For example:
py.importlib.import_module(sunpy);
For more information on how to use MATLAB Interface for python you can refer to the following documentation links:
  1. https://www.mathworks.com/help/matlab/matlab_external/create-object-from-python-class.html
  2. https://www.mathworks.com/help/matlab/matlab_external/call-user-defined-custom-module.html
I hope it helps in resolving the error.

Tags

Community Treasure Hunt

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

Start Hunting!