The invention relates to the field of chemical treatment of natural medicines, in particular to a method for extracting swainsonine from barbadosweed endophytic fungi (U.oxytropis), which comprises the following steps: (1) preparing the barbadosweed endophytic fungi into a bacterial suspension, and culturing; (2) after culture of the bacterial suspension in the step (1) is finished, performing suction filtration, rinsing, drying, grinding and wall breaking, and collecting dry powder; (3) weighing the dry powder collected in the step (2), transferring the dry powder into a centrifuge tube, adding methanol, extracting, centrifuging, collecting supernate, evaporating to dryness, redissolving, filtering and diluting; according to the extraction method, the yield of swainsonine reaches 220 mu g/g and is remarkably improved compared with the prior art, the method is simple in process condition, safe, low in cost and easy for industrial production, a basis is provided for development and utilization of swainsonine produced through microbial fermentation, raw material resources of swainsonine products are expanded, and the method is suitable for industrial production. Wide application prospects are realized.