针对我突然在gdb-python中发现的一个奇怪错误进行的调查,我将其简化为以下内容:
C:\Users\User>python -i
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win 32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> dir(os.path)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'path'
>>> dir(os)
['__builtins__', '__doc__', '__file__', '__name__', '__package__']
阅读一些其他的'module' object has no attribute
答案,最常见的建议是,在sys.path
中必须有另一个流氓的os.py
,并且它已被加载,而不是内置的。但我检查了PYTHONPATH
环境变量和当前目录,并没有其他的os.py。
所以我寻找了一种方法来找到定义实体的文件名,并且并不奇怪,Python有一个这样的工具,即inspect
模块。
>>> inspect.getsourcelines(os)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Programs\Python273\lib\inspect.py", line 690, in getsourcelines
lines, lnum = findsource(object)
File "C:\Programs\Python273\lib\inspect.py", line 527, in findsource
sourcefile = getsourcefile(object)
File "C:\Programs\Python273\lib\inspect.py", line 451, in getsourcefile
if os.path.exists(filename):
AttributeError: 'module' object has no attribute 'path'
因此,inspect
依赖于 os.path
,然后我就没有了想法。
我最近没有安装任何新东西。唯一发生的事情是我不得不进行强制关闭,这可能与运行 Python 脚本同时发生,因为当机器变得无响应并发生强制关闭时,我正在重复运行一个短的 Python 脚本。
os.__file__
。 - user2357112