Note that there are some explanatory texts on larger screens.

plurals
  1. POAttributeError: 'module' object has no attribute 'TemporaryFile'
    primarykey
    data
    text
    <p>I am new to python.And i am learning the standard library.</p> <p>Whenever i run the code below , it always raise the <code>AttributeError...</code> And it seems like there is something wrong with the import command.</p> <p>Also , i try to run it on the interactive interpreator,and it works just fine.</p> <h1>The sample code</h1> <pre><code>import tempfile import os #temp = tempfile.TemporaryFile() temp = tempfile.mktemp() print "tempfile","=&gt;",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 </code></pre> <h1>The error output</h1> <pre><code>Traceback (most recent call last): File "tempfile.py", line 1, in &lt;module&gt; import tempfile File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in &lt;module&gt; 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 &lt;module&gt; from apport.report import Report File "/usr/lib/python2.7/dist-packages/apport/report.py", line 12, in &lt;module&gt; import subprocess, tempfile, os.path, urllib, re, pwd, grp, os File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in &lt;module&gt; tempfile = tempfile.mktemp() AttributeError: 'module' object has no attribute 'mktemp' Original exception was: Traceback (most recent call last): File "tempfile.py", line 1, in &lt;module&gt; import tempfile File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in &lt;module&gt; tempfile = tempfile.mktemp() AttributeError: 'module' object has no attribute 'mktemp' </code></pre> <h1>My enviroment</h1> <ul> <li>ubuntu12.04</li> <li>python2.7</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload