It's possible, but going to be a narrow window. The problem is that rail infrastructure requires a developed metallurgy industry, both to create the metal vessels strong enough to handle the steam without bursting on a regular basis and to produce amount of metal needed for the rolling stock and the rails; this is not a trivial issue. For a straightforward comparison, the rail alone, if you use lightweight rail, will be about 80 tonnes of steel per kilometer, using heavy mechanized forming processes. This implies large-scale industrialization, which is going to imply other things, such as people fooling around with chemistry.
As you suggested, you don't want to hinder science, so that means someone, somewhere, will eventually find something that will go BOOM! Guncotton, for example, was discovered by accident in 1846 when Schönbein wiped up a spill of nitric acid and sulfuric acid (two common industrial chemicals) with a cotton apron and had it go off when the apron dried. What's less known is that another chemist, Rudolf Christian Böttger, discovered the same thing at the same time, although not in such an amusing way. Picric acid salts were known to explode by 1799, and in the 1830s it finally occurred to chemists to see if the acid itself could go bang. And it could.
That said, the first practical explosive that wasn't gunpowder was nitrogycerin, developed in 1847. So, if no one happens to stumble on gunpowder, you've probably got a narrow window of opportunity where you'll have rail, at least early versions, but no practical explosives. Once they get that chemical explosive, however, things are going to move fast. It took a thousand years to come up with an alternative to gunpowder. It took 16 years to go from nitroglycerin to TNT. In 1871, picric acid detonation was demonstrated, and it quickly became the basis of most military explosives.
If you've got steam engines, you've already got the basic concept of the gun (cylindrical housing using a blast of gas to propel something down its length). Someone will have inevitably tried steam cannons, which don't work great and certainly aren't man-portable, so there won't be man-portable steam guns, but as soon as those explosives are discovered, all bets are off.