[root@234571-app2 git]# ./test.py
File "./test.py", line 4
with open("/home/git/post-receive-email.log",'a') as log_file:
^
SyntaxError: invalid syntax
The code looks like this:
[root@234571-app2 git]# more test.py
#!/usr/bin/python
from __future__ import with_statement
with open("/home/git/post-receive-email.log",'a') as log_file:
log_file.write("hello world")
and I am using Python 2.5.5
[root@234571-app2 git]# python -V
Python 2.5.5
What you have should be correct. Python 2.5 introduced the with statement as something you can import from
__future__. Since your code is correct, the only explanation I can think of is that your python version is not what you think it is. There’s a good chance you have multiple versions of python installed on the system and for some reason your code is running with an older version. Try running it like this:Assuming this works, you can change your first line to indicate which version of python you’d like. That can either be a direct path to
python2.5or you can use theenvcommand to search the user’sPATHvariable for python2.5. The correct approach depends on what your systems python installs are. Here are the 2 approaches:To use /usr/bin/python2.5 directly, you can do this:
To use whichever version of python2.5 occurs first in your PATH, do this: