We investigate how charmonium hadroproduction at fixed-target energies can be used to constrain the gluon distribution in pions. Using nonrelativistic QCD (NRQCD) formulation, the $J/\psi$ and $\psi(2S)$ cross sections as a function of longitudinal momentum fraction $x_F$ from pions and protons colliding with light targets, as well as the $\psi(2S)$ to $J/\psi$ cross section ratios, are included in the analysis. The color-octet long-distance matrix elements are found to have a pronounced dependence on the pion parton distribution functions (PDFs). This study shows that the $x_F$ differential cross sections of pion-induced charmonium production impose strong constraints on the pion's quark and gluon PDFs. In particular, the pion PDFs with larger gluon densities provide a significantly better description of the data. It is also found that the production of the $\psi(2S)$ state is associated with a larger quark-antiquark contribution, compared with $J/\psi$.