Discussion:
[wingide-users] behavior of __file__ in shell vs debug process
Matthew Bibee
2014-06-06 03:49:58 UTC
Permalink
Hi

I?m a new user to wing so this might be noob kind of question. I have a script that after it is done running it needs to place a copy of itself in a log. I get very different behavior depending on if I run this script in the the shell or in the debugger. Basically the __file__ variable takes on a very different value in the shell. For example if I create a file called ?myFile.py? as:

import os
print os.path.basename(__file__)
print __file__
print __name__
print os.getcwd()

print '###################\n'
import inspect
print inspect.stack()

print '###################\n'
import sys
print sys.argv

print '###################\n'
print inspect.currentframe()
print inspect.getfile(inspect.currentframe())

Then n the debugger it will basically print out the name of the file ?myFile.py?, the full name of the path and name of the file, the top module and the name of the file that called it ??/myFile.py?.

If I run this in the shell (clicking options ?Evaluate myFile.py?) then it says the name of the file is ?_sandbox.py? and top of the frame is <winddb_compile>. It causes my actual script to bomb in a very unpleasant way as it can not actually find the file that was running. I was not expecting this when running at the shell. I was thinking my script would just run the way it normally runs at the command line and I could then try out some things on the shell prompt after it is finished. However it seems like the only way I can make this particular script work is to run in the debugger and make sure I have a dummy line of code at the end of the file with a break point set.

Is is possible to run in the python shell in wing and have it correctly acquire the true name of the __file__ variable (in this case ?myFile.py?).

Thanks
Matt
Wing IDE Support
2014-06-06 15:00:05 UTC
Permalink
Post by Matthew Bibee
If I run this in the shell (clicking options ?Evaluate myFile.py?) then it says the name of the file is ?_sandbox.py? and top of the frame is <winddb_compile>. It causes my actual script to bomb in a very unpleasant way as it can not actually find the file that was running. I was not expecting this when running at the shell. I was thinking my script would just run the way it normally runs at the command line and I could then try out some things on the shell prompt after it is finished. However it seems like the only way I can make this particular script work is to run in the debugger and make sure I have a dummy line of code at the end of the file with a break point set.
I think what you want to do is run in the debugger, set a breakpoint on
the last line, and use the Debug Probe. Evaluating in the Python Shell
is (roughly) the equivalent of copying the text from the file and then
pasting it into the Python Shell so there's no __file__ set.

We plan to add an option to the debugger to stop just before the program
exits.

Cheers,

John

Loading...