← Back to team overview

sikuli-driver team mailing list archive

[Bug 567303] Re: Use of "__file__" gives NameError

 

Hi RaiMain,

I am not sure this is a valid fix.

__file__ is also set by the import statement.
This is from python.org's reference: http://docs.python.org/reference/simple_stmts.html
The loader must set several attributes on the module. __name__ is to be set to the name of the module. __file__ is to be the “path” to the file unless the module is built-in (and thus listed in sys.builtin_module_names) in which case the attribute is not set.

Basically this does solve this problem.


python C:\MyScripts\Script.py

Script.py:
sys.path.append(C:\MyModules\)
import Module
print "Script Path: " + sys.argv[0]


Module.py:
print "Module Path: " + sys.argv[0]


Output:
Module Path: C:\MyScripts\Script.py
Script Path: C:\MyScripts\Script.py


If __file__ worked properly and you used that instead of sys.argv[0] you would get the expected output!

Output:
Module Path: C:\MyModules\Module.py
Script Path: C:\MyScripts\Script.py

__file__ not being set actually prevents me from structuring my scripts
in a reasonable way currently.

Thoughts?
My structure should look like this:
sikuli
  |
  ---> lib
   |        |
   |        ---> Lib1, Lib2, Lib3.sikuli
   |
   ---> ScriptFolder1
            |
            ---> Script1, Script2, Script3.sikuli
   |
   ---> ScriptFolder2
            |
            ---> Script1, Script2, Script3.sikuli

-- 
You received this bug notification because you are a member of Sikuli
Drivers, which is subscribed to Sikuli.
https://bugs.launchpad.net/bugs/567303

Title:
  Use of "__file__" gives NameError

Status in Sikuli:
  Fix Released

Bug description:
  Trying to use __file__ python functionality results in Sikuli throwing
  a NameError exception.

  Example Code:
  script_name = __file__

  
  I think I might get why this happens.  It's the same behavior as if using __file__ in a python command line interpreter session.

  But, it would be nice for this to work inside of Sikuli the same as if
  running command "python testfile.py" from command line.

  So, I am requesting that Sikuli is modified to recognize __file__ as
  valid python code that gives the current script name.  Or,
  alternatively to implement a new Sikuli specific command to get the
  name of the current running file.

  Thanks!

  Current Setup:
  Sikuli 0.9.9
  Windows XP, 32-bit

To manage notifications about this bug go to:
https://bugs.launchpad.net/sikuli/+bug/567303/+subscriptions