Python - os.path 不存在:AttributeError: 'module' 对象没有属性 'path'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21640794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:19:27  来源:igfitidea点击:

Python - os.path doesn't exist: AttributeError: 'module' object has no attribute 'path'

pythonpathpython-importcorruptionbuilt-in

提问by Evgeni Sergeev

Investigating a strange error that I started getting all of a sudden with gdb-python, I reduced it down to this:

调查我突然开始使用 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__']

Looking through some other 'module' object has no attributeanswers, the most common suggestion is that there must be another rogue os.pysomewhere in sys.path, and that it was getting loaded, rather than the built-in one. But I checked in PYTHONPATHenvironment variable, and in the current directory, and there wasn't any other os.py.

查看其他一些'module' object has no attribute答案,最常见的建议是 中os.py某处必须有另一个流氓sys.path,并且它正在加载,而不是内置的。但是我检查了PYTHONPATH环境变量和当前目录,没有任何其他os.py.

So I looked for a way to find the name of the file where an entity was defined, and unsurprisingly, Python has such a facilityin the form of the inspectmodule.

因此,我寻找一种方法来查找定义实体的文件的名称,不出所料,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'

So inspectwas relying on os.path, and then I ran out of ideas.

所以inspect是依靠os.path,然后我用完了想法。

I haven't installed anything new recently. The only thing that happened was a forced shutdown that I had to do, which might have coincided with running a Python script, since I was running a short Python script in a loop repeatedly when the machine became unresponsive and the forced shutdown occurred.

我最近没有安装任何新东西。唯一发生的事情是我不得不强制关机,这可能与运行 Python 脚本同时发生,因为当机器无响应并发生强制关机时,我正在循环中反复运行一个简短的 Python 脚本。

采纳答案by Evgeni Sergeev

After wasting a lot of time, I figured it out. Looking under C:\Programs\Python273\Lib, I noticed that os.pycwas much smaller in size than os.pyand os.pyo, while for the other modules, abc.pycis equal in size to abc.pyoand slightly smaller than abc.py. Looking inside os.pyc, it contained only:

浪费了很多时间后,我想通了。往下看C:\Programs\Python273\Lib,我注意到它的os.pyc大小比os.py和小得多os.pyo,而对于其他模块,abc.pyc它的大小等于abc.pyo并略小于abc.py. 往里面看os.pyc,它只包含:

^Có
\{GOc^@^@^@^@^@^@^@^@^A^@^@^@@^@^@^@s^D^@^@^@d^@^@S(
^A^@^@^@N(^@^@^@^@(^@^@^@^@(^@^@^@^@(^@^@^@^@s^_^@^@
^@C:\Programs\Python273\lib\os.pyt^H^@^@^@<module>^A
^@^@^@s^@^@^@^@

(This is how it shows up in Vim.) (Note: the tat the end of os.pytis not part of the filename.)

(这就是它在 Vim 中的显示方式。)(注意:t末尾的os.pyt不是文件名的一部分。)

Deleting this file (and two other .pycfiles with the same story) solved the problem.

删除这个文件(以及其他两个.pyc具有相同故事的文件)解决了这个问题。

So what happened must be that pythonwas recompiling osinto os.pycfor some reason (why? if it's already compiled before?) and has output the first part of the file (which was valid as a file by itself), then the forced shutdown occurred before it had a chance to output the rest. So it's an example of the frustrating problems that happen when operations are not atomic.

到底发生了什么,必须是python被重新编译osos.pyc某种原因(为什么呢?如果它之前已经编译?),并具有输出文件(这是由自身文件有效)的第一部分,那么它有前发生的强行关机有机会输出其余的。所以这是当操作不是原子的时发生的令人沮丧的问题的一个例子。

Also, it turns out I could have tracked it down faster, because doing

此外,事实证明我可以更快地找到它,因为这样做

>>> inspect.getfile(os)
'C:\Programs\Python273\lib\os.pyc'

still works when os.pathisn't available.

os.path不可用时仍然有效。

回答by ZXX

I have the same problem and C:\Python27\Lib\os.py doesn't have readlink() function at all. Searched the Lib and found 6 paces there it's being used but no definition:

我有同样的问题, C:\Python27\Lib\os.py 根本没有 readlink() 函数。搜索 Lib 并找到 6 步,它正在使用但没有定义:

Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32

Win32 上的 Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 位(英特尔)]

C:\Python27\Lib\pdb.py(1194):                 dirname = os.readlink(dirname)
C:\Python27\Lib\platform.py(952):             os.path.join(os.path.dirname(filepath),os.readlink(filepath)))
C:\Python27\Lib\posixpath.py(386):         resolved = os.readlink(path)
C:\Python27\Lib\rexec.py(146):     ok_posix_names = ('error', 'fstat', 'listdir', 'lstat', 'readlink',
C:\Python27\Lib\shutil.py(183):                 linkto = os.readlink(srcname)
C:\Python27\Lib\tarfile.py(1873):             linkname = os.readlink(name)