python NameError: global name '__file__' is not defined

前端 未结 12 866
误落风尘
误落风尘 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:19

    I'm having exacty the same problem and using probably the same tutorial. The function definition:

    def read(*rnames):
        return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
    

    is buggy, since os.path.dirname(__file__) will not return what you need. Try replacing os.path.dirname(__file__) with os.path.dirname(os.path.abspath(__file__)):

    def read(*rnames):
        return open(os.path.join(os.path.dirname(os.path.abspath(__file__)), *rnames)).read()
    

    I've just posted Andrew that the code snippet in current docs don't work, hopefully, it'll be corrected.

提交回复
热议问题