Python 属性错误:';模块';对象没有属性';临时文件&x27;

Python 属性错误:';模块';对象没有属性';临时文件&x27;,python,import,Python,Import,我是python新手,正在学习标准库 每当我运行下面的代码时,它总是引发AttributeError… 看起来import命令有点问题 另外,我试着在交互式对讲机上运行它,效果很好 示例代码 错误输出 回溯(最近一次呼叫最后一次): 文件“tempfile.py”,第1行,在 导入临时文件 文件“/home/zhkzyth/codeRep/pytest/tempfile.py”,第5行,在 tempfile=tempfile.mktemp() AttributeError:“模块”对象没有属性“

我是python新手,正在学习标准库

每当我运行下面的代码时,它总是引发
AttributeError…
看起来import命令有点问题

另外,我试着在交互式对讲机上运行它,效果很好

示例代码 错误输出
回溯(最近一次呼叫最后一次):
文件“tempfile.py”,第1行,在
导入临时文件
文件“/home/zhkzyth/codeRep/pytest/tempfile.py”,第5行,在
tempfile=tempfile.mktemp()
AttributeError:“模块”对象没有属性“mktemp”
sys.excepthook中出错:
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python2.7/dist packages/apport\u python\u hook.py”,第66行,apport\u excepthook中
从apport.fileutils导入可能的\u打包,获取\u最近的\u崩溃
文件“/usr/lib/python2.7/dist-packages/apport/_-init___;u.py”,第1行,在
从apport.report导入报告
文件“/usr/lib/python2.7/dist-packages/apport/report.py”,第12行,在
导入子流程、tempfile、os.path、urllib、re、pwd、grp、os
文件“/home/zhkzyth/codeRep/pytest/tempfile.py”,第5行,在
tempfile=tempfile.mktemp()
AttributeError:“模块”对象没有属性“mktemp”
最初的例外是:
回溯(最近一次呼叫最后一次):
文件“tempfile.py”,第1行,在
导入临时文件
文件“/home/zhkzyth/codeRep/pytest/tempfile.py”,第5行,在
tempfile=tempfile.mktemp()
AttributeError:“模块”对象没有属性“mktemp”
我的环境
  • ubuntu12.04
  • 蟒蛇2.7

    • 您是否为自己的文件命名了
      tempfile.py
      ?如果是,请重命名它,删除所有*.pyc文件,然后重试


      PS:通过回溯提供错误的实际文本可以告诉我们这些事情。

      尝试访问不属于模块中的类或函数的属性会引发AttributeError异常,该属性可能在正在使用的Python解释器的更高版本中被弃用。我建议您检查您正在运行的Python版本,并确保您的dir(模块)包含您试图使用的属性

      您真的有AttributeError吗?请报告完整的异常tracebackAdd
      print tempfile.\uuuuu file\uuuuuuu
      就在导入下面,以调试导入的内容。@joaquin,我已经更新了它。它现在正在运行吗?或者你有新的错误吗?@joaquin,现在可以了。我把它重命名为temp.py.=)“可能已经被弃用了”?您可以查看文档并确定。OP声明他们正在使用Python 2.7
      tempfile.TemporaryFile
      在Python 2.7中是一个有效的类。是的,你说得对。我临时将它命名为
      tempfile.py
      ,并通过命令
      Python tempfile.py
      在shell中运行它。
      import tempfile
      import os
      
      #temp = tempfile.TemporaryFile()
      temp = tempfile.mktemp()
      
      print "tempfile","=>",temp
      
      file = open(temp,"w+b")
      file.write("*" * 1000)
      file.seek(0)
      print len(file.read()),"byte"
      file.close()
      
      try:
         os.remove(temp)
      except OSError:
         pass
      
      Traceback (most recent call last):
        File "tempfile.py", line 1, in <module>
          import tempfile
        File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in <module>
          tempfile = tempfile.mktemp()
      AttributeError: 'module' object has no attribute 'mktemp'
      Error in sys.excepthook:
      Traceback (most recent call last):
        File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 66, in apport_excepthook
          from apport.fileutils import likely_packaged, get_recent_crashes
        File "/usr/lib/python2.7/dist-packages/apport/__init__.py", line 1, in <module>
          from apport.report import Report
        File "/usr/lib/python2.7/dist-packages/apport/report.py", line 12, in <module>
          import subprocess, tempfile, os.path, urllib, re, pwd, grp, os
        File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in <module>
          tempfile = tempfile.mktemp()
      AttributeError: 'module' object has no attribute 'mktemp'
      
      Original exception was:
      Traceback (most recent call last):
        File "tempfile.py", line 1, in <module>
          import tempfile
        File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in <module>
          tempfile = tempfile.mktemp()
      AttributeError: 'module' object has no attribute 'mktemp'