error: the local data file is outside of the package's tree

Viewed 29

I have this script:

from astropy.io import fits
from astropy.utils.data import get_pkg_data_filename

fits_file = get_pkg_data_filename("../path_to_the_test_candidate/number_000150.fits") 
fits.info(fits_file)


with fits.open(fits_file, 'update') as f:
    for hdu in f:
        hdu.header['OBJECT'] = "Mao"


fits.info(fits_file)

print(repr(fits.getheader(fits_file, 0)))

when copy-paste it in the shell line by line it works but when copy in a file and run python3 file.py this error appears: AssertionError: attempted to get a local data file outside of the __main__ tree. What is the problem?

1 Answers

The utility function get_pkg_data_filename() is only for retrieving data files that are included in the astropy package or on Astropy data servers for demonstration and internal uses.

But it looks like you're just trying to open a file somewhere on your filesystem. Perhaps you saw the use of this function in a tutorial and cargo-culted it over :)

In general there is no reason to use this. You can just pass the filename directly:

fits.info('../path_to_the_test_candidate/number_000150.fits')
fits.open('../path_to_the_test_candidate/number_000150.fits')

and so on.

Related