Skip to content
Advertisement

NameError: name ‘__file__’ is not defined

I am trying to store the path of a script into a variable using:

JavaScript

However, it keeps returning a name '__file__' is not defined error.

JavaScript

Advertisement

Answer

Pretty sure you are running this in a terminal in the interactive Python as it is the only place (I’m aware of) to not have __file__. Also, if it was a script, and it would be the first line of it (according to the error message) it would fail on os is not defined (as you’d not import it).

Try it in the actual script. This should work.

Advertisement