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))
Similar Posts:
- How to Solve Python SyntaxError: EOL while scanning string literal
- [Solved] RuntimeError: Model class apps.users.models.UserProfile doesn’t declare an explicit app_label and isn’t in an application in INSTALLED_APPS.
- Python3.x Run Python2.x Codes syntax error: “Missing parentheses in call to ‘print’
- [Solution] systemerror: the solution of parent module ” not loaded, cannot perform relative Import
- [Solved] PyCharm Start Error: TypeError: unsupported operand type(s) for /: ‘str’ and ‘str’
- [Solved] pip Install Error: is not a supported wheel on this platform
- OSError: [WinError 126] Could not find the specified Module self._handle = _dlopen(self._name, mode)
- SVN Error: ‘you’ is not valid as filename in directory [How to Solve]
- python: _tkinter.TclError: couldn’t recognize data in image file
- docker:ImportError: libcuda.so.1: cannot open shared object file: No such file or directory