python NameError: global name '__file__' is not defined

前端 未结 12 831
误落风尘
误落风尘 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条回答
  •  醉梦人生
    2020-12-02 08:22

    This error comes when you append this line os.path.join(os.path.dirname(__file__)) in python interactive shell.

    Python Shell doesn't detect current file path in __file__ and it's related to your filepath in which you added this line

    So you should write this line os.path.join(os.path.dirname(__file__)) in file.py. and then run python file.py, It works because it takes your filepath.

提交回复
热议问题