6

I developed a project using python. Now i need a gui for that project. So i choose jython for gui(java swing). I also integrate theme in one code (existing project + gui(jython) code). When i run the file with the following command then it shows a syntax error

jython project.py

Error:

File "project.py", line 33
SyntaxError: 'with' will become a reserved keyword in Python 2.6

Line#33:

32 def _finished_loading(self, view, frame):
33        with open(self._file, 'w') as f:

When i run the existing project with python command then it runs ok. That means there is no problem with the project. And i assure you that gui(jython) code and integration are also fine.

shantanu
  • 2,408
  • 2
  • 26
  • 56
  • What version of jython are you running? – tkone Apr 09 '12 at 18:03
  • @tkon thanks for quick reply. Jython 2.5.1+ Python 2.7.3rc1 Ubuntu 11.10 64bit. – shantanu Apr 09 '12 at 18:10
  • 2
    `from __future__ import with` -- i'm not sure if `with` has been ported to Jython since it's at 2.5.1 and Python 2.6 is where `with` exists. – tkone Apr 09 '12 at 18:12
  • So what's your question -- don't run code that is incompatible with Python 2.5 -- don't use with – j13r Apr 09 '12 at 18:14
  • 1
    It seems you're using a version of Jython that is out of sync with the version of (C)Python that you wrote your code for. You'll need to back-port your code. – Cameron Apr 09 '12 at 18:14
  • jython and python are not similar !! I just modified the line without with, now it is showing it import gtk, urllib ImportError: No module named gtk How can i integrate jython and python. I used python-webkit in my existing project – shantanu Apr 09 '12 at 18:18
  • 2
    @tkone: it does in fact exist in Jython 2.5 if you use the import, though it's `import with_statement`, not `import with`. – Mattie Apr 09 '12 at 18:18
  • 1
    @shantanu: `gtk` is not available in Jython, owing to the fact it was compiled for CPython. From http://www.jython.org/docs/library/indexprogress.html "...any Python that does not use C extensions should work on Jython." – Mattie Apr 09 '12 at 18:23
  • @zigg what should i do now? i need gui. Problem is that this program must need to run in mac so i used cross platform jython. :( – shantanu Apr 09 '12 at 18:26
  • @zigg ah yes `with_statement` -- it's been a while since i've had to import it. – tkone Apr 09 '12 at 18:28
  • @shantanu You might be able to get `gtk` working on OS X with CPython. Have a look here: http://stackoverflow.com/questions/1164949/where-is-pygtk-for-mac-os-x But this is outside my expertise, so I cannot help. Just know that if you go Jython, you'll need libraries that are available on Jython--it doesn't magically all CPython libraries available. – Mattie Apr 09 '12 at 18:32
  • Why not continue to use cpython and Tkinter? It works fine on osx. – Bryan Oakley Apr 09 '12 at 18:43

1 Answers1

10

Because with only just appeared in 2.5, you need a from __future__ import:

from __future__ import with_statement

Then you can use your with statement. It won't solve your other problems that cropped up in your comments, though...

Mattie
  • 20,280
  • 7
  • 36
  • 54
  • Don't literally use the `...`. Take the `from __future__` import and put it the incomplete source file you posted in the question. – Mattie Apr 09 '12 at 18:28