← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #240852]: It seems that __file__ can't work in Sikuli --- but only in the main script

 

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

    Status: Open => Answered

RaiMan proposed the following answer:
*** definition of __file__
... contains the absolute file name of the file (.py) containing the statement that references __file__

*** Sikuli main scripts
... the one you run using the run button in the IDE or via -r option on command line:
__file__ is not available, since this is internally not run from a file directly, but via a dynamic Jython interpreter instance (same as running Jython interactively: __file__ is not defined in this case.

The Sikuli feature to use: getBundlePath()

so in the above case in the main script:
print os.path.dirname(getBundlePath())
... will print the absolute path of the folder containing your running main script xxx.sikuli

This is for the cases where we run using Sikuli.

When running stuff from Eclipse, the script file is run through an
external Jython interpreter process and hence __file__ is known.

Hence you might catch the name exception to distinguish the two run
cases (see example below).


*** __file__ in imported scripts
... will generally be defined and valid (contain the importers modules filename.py)

... hence your apprehension according to xlrd (and every "thing"
imported in Sikuli) is for no reason.

*** a simple test
# having both in same folder
# main.sikuli
try:
    print __file__
except:
    print "__file__ does not work in Sikuli main scripts run through Sikuli"

# sub.sikuli
print "from sub:", __file__

running main in IDE will print:
__file__ does not work in Sikuli main scripts run through Sikuli
from sub: absolute-path-to-sub.sikuli/sub.py

Nevertheless: I will put this on the list to add the __file__ variable
in main script's global context, when internally calling the Interpreter
(so it would contain getBundlePath()+"/script-name.py")

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