python NameError: global name '__file__' is not defined

前端 未结 12 865
误落风尘
误落风尘 2020-12-02 08:08

When I run this code in python 2.7, I get this error:

Traceback (most recent call last):
File \"C:\\Python26\\Lib\\site-packages\\pyutilib.subprocess-3.5.4\\         


        
12条回答
  •  旧时难觅i
    2020-12-02 08:11

    I solved it by treating file as a string, i.e. put "__file__" (together with the quotes!) instead of __file__

    This works fine for me:

    wk_dir = os.path.dirname(os.path.realpath('__file__'))
    

提交回复
热议问题