Tag Archives: python __file__ is not defined

How to Solve Python Error: python __file__ is not defined

 

python __ file__ Is not defined solution

__ file__ It is a variable generated when Python module is imported__ file__ Can’t be used, but what should I do to get the path of the current file

Method 1

import inspect, os.path

filename = inspect.getframeinfo(inspect.currentframe()).filename
path     = os.path.dirname(os.path.abspath(filename))

Method 2

import inspect
import os

os.path.abspath(inspect.getsourcefile(lambda:0))

Link: https://stackoverflow.com/questions/2632199/how-do-i-get-the-path-of-the-current-executed-file-in-python/18489147#18489147