Yesterday, someone posted a question asking how to require a specific version of Python so that his script would fail gracefully when executed using an earlier version of Python that did not support a certain syntax.
Later, the question was closed as a duplicate of "How do I check what version of Python is running my script?" -- this was an incorrect duplicate, because finding the version alone would not solve the original problem.
Consider the following Python script:
with open('./t.py', 'r') as f: print len(f.readlines())
The user was saying that, when someone runs this script in Python < 2.6, it will fail with an error like:
$ python w.py # using Python 2.5 w.py:5: Warning: 'with' will become a reserved keyword in Python 2.6 File "w.py", line 5 with open('./t.py', 'r') as f: ^ SyntaxError: invalid syntax
He wanted to have it fail more gracefully.
Someone suggested that all you have to do is exit if the version is wrong:
import sys if sys.version_info < (2,6,0): print "this isn't going to work" sys.exit(1) with open('./t.py', 'r') as f: print len(f.readlines())
This is incorrect, because it produces the same error (the error occurs when lexing/compiling, not at runtime). However, the concept of checking the version must have been what caused users to close the question.
So, the answers (1, 2, etc.) were merged to this question about detecting the version of Python, which a) doesn't address the original question and b) would not have helped the original question either. (Answer 1 was a correct answer, btw)
If anything, it should have been merged with perhaps this question -- I don't think it should have been merged at all, but that's not up to me to decide.
Then the question I have (to which I don't really expect an answer), is how did this happen? Did a moderator see the single close vote and decide to merge it, without reading (or understanding) the full question? Or was this merge decided by more than one person?
I see now that it was fully voted to close as a duplicate of the wrong question. That's the community's fault. The question certainly could have been worded a lot better, but I think this snippet makes it clear that it isn't just a question of how to check the Python version:
... the problem I have is the interpreter on a 2.4 machine reads the file and sees a "finally" at the end of my try loop and errors out before it gets far enough into execution for my if statement to handle the version mismatch.
The title ("Determing Python version at runtime") was obviously the only part that the closers read.
a. Should not have been closed (but I'd settle for having it edited and reopened)
b. Should not have been merged, much less with the wrong question.
This is the kind of thing that stops people from coming back to a website/community. It's one thing to close a question like "how do i make stack in c", but this individual asked a decent question and unfairly had his question closed, locked, and merged.
Can the answers be un-merged? If not, can the question be unlocked so that the individual can receive a proper answer to his question?