← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #210826]: import fails

 

Question #210826 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/210826

    Status: Open => Answered

RaiMan proposed the following answer:
ok, this is basic Python knowledge:

when importing a module:
- all code, that would be executed, if this module would be run standalone, is executed once at import time
- the name of the module is registered in the current name space
- using from foobar import * will also register all names of the modules namespace into the current namespace, so the names inside module can be used directly as name without the need for foobar.name

usually, a module does not contain any code, that can be executed at
import time (exception: some initialization stuff and/or module global
variables). A module mainly contains classes (class():) and functions
(def():).

So in your case:

# openmenu.sikuli
from sikuli import *

def function1():
    click("1349796451457.png")
    hover("ycrlage.png")
    hover("Neu.png")

# main script
myScriptPath = r"d:\sikuli"
if not myScriptPath in sys.path: sys.path.append(myScriptPath)
import openmenu

openmenu.function1()
click("SprachEditor.png")

or
# main script with from openmenu import *
myScriptPath = r"d:\sikuli"
if not myScriptPath in sys.path: sys.path.append(myScriptPath)
from openmenu import *

function1()
click("SprachEditor.png")


One more thing:
In your situation all scripts seem to be in the same folder.
If you plan to leave it this way: no need to add sys.path.
Sikuli import finds scripts in the same directory as the main script automagically.


# main script with from openmenu import *
from openmenu import *

function1()
click("SprachEditor.png")

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.