At the beginning of “The Man Who Killed Don Quixote,” text onscreen proclaims that what we are about to see has been “25 years in the making.” It might be even longer than that. Terry Gilliam has been tilting at this particular windmill since its eventual star, Adam Driver, was in elementary school. (It’s not Driver who plays the Knight of Doleful Countenance, by the way, but the Gilliam stalwart Jonathan Pryce.) The legends surrounding the project have made “Quixote” one of the great films maudits of our time; a documentary on its failure to come to fruition appeared way back in 2003.

All of that has perhaps created outsized expectations. Surely a movie so long in gestation, inspired by a doorstop-thick novel that has beguiled and baffled readers for several centuries, would turn out to be either a world-class catastrophe or a world-historical masterpiece.

With a mixture of relief and regret, I must report that the movie is neither. “The Man Who Killed Don Quixote” has moments of slackness and chaos (the book does, too), but for the most part it’s a lively, charming excursion into a landscape claimed by Gilliam in the name of Miguel de Cervantes, the Spanish gentleman who gave Don Quixote life back in the early 1600s. The filmmaker’s devotion to the novelist adds luster and vigor to the images, but this is more than just an act of literary-minded reverence. It’s a meeting of minds — a celebration of artistic kinship across the gulfs of history, culture and technology.

Gilliam, like Cervantes, is a wily inventor who also serves as an analyst and evangelist of the imagination. In his most successful movies — “Brazil,” “The Adventures of Baron Munchausen,” “The Fisher King,” “Twelve Monkeys,” — imagination is a mighty force and a fragile vessel. Art is a heroic expression of will even as it also stands as poor compensation for human frailty. The only hope is to dream, which is also a kind of doom.