python __file__ is not defined 解決方法
__file__ 是在python module 被導入的時候生成的一個變量,所以在 __file__ 不能被使用,但是又想獲取當前文件的路徑應該怎么做:
方法一:
import inspect, os.path filename = inspect.getframeinfo(inspect.currentframe()).filename path = os.path.dirname(os.path.abspath(filename))
方法二:
import inspect import os os.path.abspath(inspect.getsourcefile(lambda:0))