Name Error __file__

Hello,
I am trying to understand how some code works, so I am trying to run it line by line.

from ROOT import *
import sys
sys.path.append("modules/")
import math


print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
print 'Use as: script.py -b 0 (or 1,2)'
myinput="interactive"
xdir=""
cross=0
expected_pb=1000.
if (len(sys.argv) ==2):
   myinput = sys.argv[1]
   



print 'Mode=',myinput
gROOT.Reset()
figdir="figs/"

This all works, until I execute the next line

epsfig=figdir+__file__.replace(".py",".eps")

Then I get the error

NameError: name '__file__' is not defined

Does somebody know how to fix this? I am running Python 2.7.16
Any help is appreciated.
_ROOT Version: 6.23/01

Hello,

That line can only work if you run the code as a script, not from the Python prompt. In the Python prompt, there is no attribute __file__ since no file is being executed.

Cheers,
Enric

1 Like