Disclosed in the present invention are a pyrazolo[1,5-a]pyrimidine derivative having a structure of formula (I), a pharmaceutical composition comprising the compound of formula (I), and use of the compound in the preparation of a medicament for preventing or treating diseases associated with tropomyosin receptor kinases, in particular for preventing or treating cancers associated with tropomyosin receptor kinases. Each substituent in formula (I) has the same definition as that in the description.