Python - os.path does not exist: AttributeError: object 'module' does not have attribute 'path'

Investigating the strange error that I started getting suddenly with gdb-python, I reduced it to:

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 answers 'module' object has no attribute, the most common assumption is that there sys.pathmust be another rogue os.pyand that it is loading, not built-in. But I checked in the environment variable PYTHONPATHand in the current directory, and there was no other os.py.

So, I was looking for a way to find the name of the file where the object was defined, and it is not surprising that Python has such an object in the form 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, inspectrelied on os.path, and then I ran out of ideas.

. , , , , Python script, Python script , .

+4
2

, . 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^@^@^@^@

( Vim.) (: t os.pyt .)

( .pyc ) .

, , , python - os os.pyc (? ?) ( ), , . , , , .

, , ,

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

, os.path .

+5

, C:\Python27\Lib\os.py readlink(). Lib 6 , , :

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

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)
0

Source: https://habr.com/ru/post/1525888/


All Articles