Error message when using import: 'with' will become a reserved keyword in Python

Asked by Jan

Hi,

I get this error when using 'import' and 'with':
ImportError: Errors in loading sikuli module: lib_base_Init
'with' will become a reserved keyword in Python 2.6 (lib_base_Init.py, line 11)
 - HOW TO FIX? Try adding "from sikuli import *" in the module.

Environment:
- Mac OS X 10.7.4
- Sikuli X-1.0rc3 (r905)
- When typing "python" in the console:
  Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin

My code:
------------------------------
MyTest.sikuli:
import MyLib

MyLib.sikuli:
def initApp(self):
    anyapp = App.open("/../AnyApp.app")
    with Region(lookapp.window()):
        # do something
------------------------------

I've run the code from the IDE and from commandline. Always the same error message.
When using the 'with' statement within the same file (MyTest.sikuli in my case) then the code works fine. The import works fine without using 'with'.

I've made some tests on my code (unsuccessfully):
MyTest.sikuli:
from __future__ import with_statement
from sikuli.Sikuli import *
from sikuli import *
import MyLib

I've found those two directories, but I'm not sure if Sikuli uses them:
/Developer/Applications/Utilities/Python 2.6
/Developer/Applications/Utilities/MacPython 2.5

Any ideas?

Thanks in advance,
Jan

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:

This question was reopened

  • by Jan
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Do just what it says:
- HOW TO FIX? Try adding "from sikuli import *" in the module.

So this means - might not be that clear :-( - you have to add
from sikuli import *

to every IMPORTED module that uses Sikuli features (so to MyLib.sikuli in your case).

See docs: http://sikuli.org/docx/globals.html#importing-other-sikuli-scripts-reuse-code-and-images

BTW: def initApp(self):
as a convention in Python self is only used with function definitions in classes (aka methods) as the parameter cobntaining the reference to the current object.

So if you want to use initApp with a parameter use another name.

e.g.
def initApp(path):
    anyapp = App.open(path)
    with Region(lookapp.window()):
        # do something

Revision history for this message
Jan (oa-aegiqter-ot) said :
#2

Hi RaiMan,

thanks for the quick answer! Sorry, I forgot to mention that I've tried that too.

But I've implemented your suggestions and removed the 'self' AND I added "from __future__ import with_statement" in the included file (MyLib.sikuli in my case).

That worked!

Source:
----------------------------
MyTest.sikuli:
import MyLib
MyLib.initApp()

MyLib.sikuli:
from __future__ import with_statement
from sikuli import *

def initApp():
    anyapp = App.open("/Applications/TextEdit.app")
    with Region(anyapp.window()):
        popup('it worked')
----------------------------

As I don't know anything about python, I have no clue if the additional statement "from __future__ import with_statement" comes with any drawbacks ...

Jan

Revision history for this message
Jan (oa-aegiqter-ot) said :
#3

But ... running it as test-case does not work (IDE and commandoline)

Sourcecode:
----------------------------
MyTest.sikuli:
import MyLib

def setUp(self):
    print "setUp"

def tearDown(self):
    print "tearDown"

def testMy(self):
    print "testMy"
    MyLib.initApp()

MyLib.sikuli:
#from __future__ import with_statement # same result with or without that line
from sikuli import *

def initApp():
    anyapp = App.open("/Applications/TextEdit.app")
    with Region(anyapp.window()):
        print('it works')
----------------------------

Output from commandoline:
localhost$ /Applications/Sikuli-IDE.app/sikuli-ide.sh --stderr --test /.../Tests/Sikuli/en/MyTest.sikuli
[info] Sikuli vision engine loaded.
[info] Mac OS X utilities loaded.
[info] VDictProxy loaded.
[error] null

Running the test-case from IDE crashes the IDE.

Any ideas?

Thanks,
Jan

Revision history for this message
Best RaiMan (raimund-hocke) said :
#4

the IDE test feature does not allow any imports and has many more odds.

see faq 1804

Revision history for this message
Jan (oa-aegiqter-ot) said :
#5

Thanks RaiMan, that solved my question.