BackgroundRhizosphere soil physicochemical, endophytic fungi have an important role in plant growth. A large number of endophytic fungi play an indispensable role in promoting plant growth and development, and they can provide protection for host plants by producing a variety of secondary metabolites to resist and inhibit plant pathogens. Due to the terrain of Gansu province is north–south and longitudinal, different climatic conditions, altitude, terrain and growth environment will affect the growth ofCodonopsis pilosula, and the changes in these environmental factors directly affect the quality and yield ofC. pilosulain different production areas. However, InC. pilosula, the connection between soil nutrients, spatiotemporal variation and the community structure of endophytic fungi isolated fromC. pilosularoots has not been well studied.ResultsSeven hundred six strains of endophytic fungi were obtained using tissue isolation and the hyphaend-purification method fromC. pilosularoots that picked at all seasons and six districts (Huichuan, HC; Longxi, LX; Zhangxian, ZX; Minxian, MX; Weiyuan, WY; and Lintao, LT) in Gansu Province, China.Fusariumsp.(205 strains, 29.04%),Aspergillussp.(196 strains, 27.76%),Alternariasp.(73 strains, 10.34%),Penicilliumsp.(58 strains, 8.22%) andPlectosphaerellasp.(56 strains, 7.93%) were the dominant genus. The species composition differed from temporal and spatial distribution (Autumn and Winter were higher than Spring and Summer, MX and LT had the highest similarity, HC and LT had the lowest). physical and chemical of soil like Electroconductibility (EC), Total nitrogen (TN), Catalase (CAT), Urease (URE) and Sucrase (SUC) had significant effects on agronomic traits ofC. pilosula(P< 0.05). AK (Spring and Summer), TN (Autumn) and altitude (Winter) are the main driving factors for the change of endophytic fungal community. Moreover, geographic location (such as altitude, latitude and longitude) also has effects on the diversity of endophytic fungi.ConclusionsThese results suggested that soil nutrients and enzyme, seasonal variation and geographical locations have an impact on shaping the community structure of culturable endophytic fungi in the roots ofC. pilosulaand its root traits. This suggests that climatic conditions may play a driving role in the growth and development ofC. pilosula.Supplementary InformationThe online version contains supplementary material available at 10.1186/s12866-023-02848-3.