AttributeError: 'module' object has no attribute 'TemporaryFile' - python

I am new to python.And i am learning the standard library.
Whenever i run the code below , it always raise the AttributeError...
And it seems like there is something wrong with the import command.
Also , i try to run it on the interactive interpreator,and it works just fine.
The sample code
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
The error output
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'
My enviroment
ubuntu12.04
python2.7

Did you name your own file tempfile.py? If so, rename it, delete all your *.pyc files, and try again.
PS: providing the actual text of the error with the traceback would tell us these things.

Trying to access an attribute that does not belong to a class or function in a module raises an AttributeError exception, the attribute might have been deprecated in a later version of the Python interpreter being used. I suggest you check the version of the Python you're running and make sure your dir(module) includes the attribute you're trying to use

Related

using Textatistic: OSError: Cannot load hyphen dictionary

I am trying to use Textatistic. However it curs a error and I have am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.
The source code is
from pathlib import Path
import textatistic
text = Path('Robinson Crusoe.txt').read_text(encoding='utf-8')
readability = textatistic.Textatistic(text)
print(readability.dict())
Traceback (most recent call last):
File "D:\anaconda3\lib\site-packages\hyphen\hyphenator.py", line 60, in __init__
self.__hyphenate__ = hnj.hyphenator_(file_path,
OSError: Cannot load hyphen dictionary.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\Demos\python_demo\main.py", line 2, in <module>
import textatistic
File "D:\anaconda3\lib\site-packages\textatistic\__init__.py", line 27, in <module>
from .textatistic import *
File "D:\anaconda3\lib\site-packages\textatistic\textatistic.py", line 39, in <module>
class Textatistic(object):
File "D:\anaconda3\lib\site-packages\textatistic\textatistic.py", line 42, in Textatistic
def __init__(self, text, abbr=Abbreviations(), hyphen=Hyphenator('en_US'), easy=EasyWords()):
File "D:\anaconda3\lib\site-packages\hyphen\hyphenator.py", line 64, in __init__
raise RuntimeError(f'C extension raised error \
RuntimeError: C extension raised error when initializing Hyphenator for dictionary at C:\Users\root\AppData\Local\pyhyphen\hyph_en_US.dic
Please tell me how to do!

Simple code for create a thread in Python 2.7

I'm new to Python and I'm trying to do a simple thread as follows.
import threading
def func(x):
print x
t1 = threading.Thread(target=func,args=("Hello",));
t1.start();
Then I got following error:
Traceback (most recent call last):
File "ex2.py", line 1, in <module>
import threading
File "/Users/treinetic-macbook/Desktop/threading.py", line 2, in <module>
File "/Library/Python/2.7/site-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/Library/Python/2.7/site-packages/urllib3/__init__.py", line 8, in <module>
from .connectionpool import (
File "/Library/Python/2.7/site-packages/urllib3/connectionpool.py", line 3, in <module>
import logging
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 207, in <module>
_lock = threading.RLock()
AttributeError: 'module' object has no attribute 'RLock'
can someone help me to understand this error?
/Users/treinetic-macbook/Desktop/threading.py is being imported because it's on your path somewhere. This is most likely not correct, try renaming that file and removing any threading.pyc file in your local dir.

Pandas: AttributeError: 'module' object has no attribute 'getLogger'

How do I fix this? I want to use the Panda module, do I need to rename certain files?
When trying to import pandas, I get these errors:
Traceback (most recent call last):
File "C:\Users\Mathias\Documents\Cognitive Science\Cognition and Communication\Exam project\Embodiment_Exp.py", line 11, in <module>
import pandas as pd
File "C:\Python27\lib\site-packages\pandas\__init__.py", line 44, in <module>
from pandas.io.api import *
File "C:\Python27\lib\site-packages\pandas\io\api.py", line 15, in <module>
from pandas.io.gbq import read_gbq
File "C:\Python27\lib\site-packages\pandas\io\gbq.py", line 37, in <module>
AttributeError: 'module' object has no attribute 'getLogger'
This is to do with a filename clash, see https://github.com/pandas-dev/pandas/issues/10167
If you have a file named logging.py this needs to be renamed.

AttributeError: 'module' object has no attribute '[x]'

I am trying to make a script that moves all the .txt files in your desktop to desktop/org, the code is as follows:
import os
import shutil
userhome = os.path.expanduser('~')
src = userhome + '/Desktop/'
dst = src+ 'org/'
def main():
txtlist = os.listdir(src)
for file in txtlist:
sortFiles(file)
def sortFiles(file):
if file.endswith(".txt"):
shutil.move(src+file,dst)
main()
If I execute the .py I get this error: AttributeError: 'module' object has no attribute 'copy'. However, if I erase the last line "main()" and I import this script as a module in the python command line and I call .main() from there it works perfectly well. How can I make this work as a script?
Traceback (most recent call last):
File "C:\Python32\org.py", line 3, in <module>
import shutil
File "C:\Python32\lib\shutil.py", line 14, in <module>
import tarfile
File "C:\Python32\lib\tarfile.py", line 50, in <module>
import copy
File "C:\Python32\lib\copy.py", line 61, in <module>
from org.python.core import PyStringMap
File "C:\Python32\org.py", line 19, in <module>
main()
File "C:\Python32\org.py", line 12, in main
sortFiles(file)
File "C:\Python32\org.py", line 16, in sortFiles
shutil.move(src+file,dst)
AttributeError: 'module' object has no attribute 'move'
I am using python 3.2
Wow, that’s some bad luck. You can understand what’s going on when you look at the traceback:
Traceback (most recent call last):
File "C:\Python32\org.py", line 3, in <module>
import shutil
So, the first line that is being executed is import shutil. That’s where everything starts going wrong—which is suprising given that it’s a built-in module.
File "C:\Python32\lib\shutil.py", line 14, in <module>
import tarfile
File "C:\Python32\lib\tarfile.py", line 50, in <module
import copy
So shutil import tarfile, which imports copy.
File "C:\Python32\lib\copy.py", line 61, in <module>
from org.python.core import PyStringMap
And copy has this nice thing that tries to import PyStringMap from a module called org.python.core. Now, this module usually does not exist, which would cause copy to use some alternative code instead: PyStringMap = None.
The problem is, that there is something called org: Your own script, org.py. So what happens is that Python tries to find something called python.core.PyStringMap in your org.py. To be able to go that far, it needs to execute the script, including the main() call at the end:
File "C:\Python32\org.py", line 19, in <module>
main()
File "C:\Python32\org.py", line 12, in main
sortFiles(file)
File "C:\Python32\org.py", line 16, in sortFiles
shutil.copy(src+file,dst)
AttributeError: 'module' object has no attribute 'copy'
And that leads us to the shutil.copy line which is a call to the shutil module. As this is the module we are still importing (from the very first line!), its import hasn’t completely yet, so the copy function inside does not exist, causing the AttributeError.
This is a very unfortunate situation in which the naming of your script caused a circular import for something that doesn’t exist.
You can easily fix this by renaming your script into something else.

AttributeError 'module' has no attribute 'Queue"

I am trying to import Queue and I keep getting the following
Traceback (most recent call last):
File "threading.py", line 2, in <module>
import Queue
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/Queue.py", line 5, in <module>
import threading as _threading
File "/Users/zaq/threading.py", line 10, in <module>
queue = Queue.Queue()
AttributeError: 'module' object has no attribute 'Queue'
I am using the code in the link Threading in python using queue
Also, I can import and use Queue in the python interpreter.
What am I doing wrong?
Name of my script was threading.py... Changed it and everthing works fine. Rookie mistake.

Categories