The settlement of Pyli has been referenced since antiquity. It is first mentioned in the work of Pausanias, Description of Greece (Hellados Periegesis), a travel and cultural guide of ancient Greece, referenced as "Great Gates", because the location is a natural passage between Epirus and Thessaly. There also was a temple devoted to Aphaia Athena in the surrounding area, whose ruins can be still seen. It is also recorded that Alexander the Great passed through Epirus and came to Thessaly through Pyli, in order to ensure Thessaly's support in his plans (which was successful, the cavalry of Thessaly became notorious in Alexander's campaign). It was also later visited by King Philip V of Macedon.
During Greco-Roman times, the location was found to be of strategic importance and the castle of Athinaion was used as an outpost, enabling monitoring of the plains in distances of up to 30 or 40 km (the famous Meteora are visible on a clear day from these outposts). In Byzantine times, after the first fall of Constantinople to the Fourth Crusade in 1204 AD, various independent states emerged, among which the Despotate of Epirus and the principality of Thessaly. In 1283, the ruler of Thessaly, John I Doukas, founded the monastery of Porta Panagia in the area.
Under the rule of the Ottoman Empire, the town prospered and while it had originally been situated in the north side of the river Portaikos, it is mentioned in the writings of Cosmas of Aetolia that he visited Pyli, and the residents gave no heed to his teachings. It is said that he cursed Pyli and its residents (then known as Porta), and they relocated to the south bank, where Pyli stands today. During the Ottoman era, Ali Pasha of Ioannina rebelled against the Ottoman Empire and tried to turn his domain into an independent state. During his campaign, his army camped briefly at Pyli, and lit a very large and venerable platanus, which was badly burnt, but survived due to its large and strong roots, and still survives to this day. During World War II, the area came under the authority of Italian occupation forces, and during the years 1943-1944 under German occupation forces, which set fire to the monastery of the Dormition of Theotokos, believing that rebel forces of the ELAS were in hiding there.
In modern years, Pyli has been the local trading and education center for the surrounding villages and settlements, especially those situated in the mountainous area west of Pyli. Since Thessaly was merged into the Kingdom of Greece in 1888, Pyli was always intensely populated and bristling with activity, being characterized as a 'small town' and electing a mayor of its own. With the Kapodistrias plan in the late 1990s, Pyli officially became a municipality with 6 municipal prefectures under its jurisdiction, St. Vissarion, Ropoto, Palaiokarya, Petrohori, St. Prokopios and Kotroni.