I'm working on unittest in python.
I'm working on a Ububtu machine from "/home/jamy/PycharmProjects/xcxzc/UnitTesting.py" directory and trying to run this following code:
import unittest
class LearningCase(unittest.TestCase):
def test_starting_out(self):
self.assertEqual(1, 1)
def main():
unittest.main()
if __name__ == "__main__":
main()
But I am getting this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "unitTesting.py", line 1, in <module>
import unittest
File "/home/jamy/PycharmProjects/xcxzc/unittest.py", line 4, in <module>
AttributeError: 'module' object has no attribute 'TestSequenceFunctions'
How can I fix this?
By naming your script unittest.py you are shadowing the built-in unittest module.
Related
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.
When I write the following line of code
import urllib
I get this error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/urllib.py", line 26, in <module>
import socket
File "socket.py", line 2, in <module>
s = socket.socket()
TypeError: 'module' object is not callable
After going through various questions on SO I tried these:
from urllib import urlopen
(Same error as above)
>>> urllib
Error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'urllib' is not defined
>>> urllib.urlopen()
Error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'urllib' is not defined
Please help. I get a similar error when I try to import urllib2, urllib3, requests.
You named your file socket.py, hiding the standard library socket module. Name it something else.
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.
With
def show(a):
""" Shows a string
>>> show(a)
a
"""
print(a)
def test():
import doctest
doctest.testmod()
if __name__ == '__main__': test()
I am getting an error while trying to learn how a docstring works.
Both this method and running it from command line with
python -m doctest unittest.py
ends with errors.
Traceback (most recent call last):
File "/home/liquid/workspace/MyPythonProject/src/unittest.py", line 19, in <module>
if __name__ == '__main__': test()
File "/home/liquid/workspace/MyPythonProject/src/unittest.py", line 16, in test
import doctest
File "/usr/lib/python3.2/doctest.py", line 2105, in <module>
class DocTestCase(unittest.TestCase):
AttributeError: 'module' object has no attribute 'TestCase'
Why?
Unfortunately you named your module the same as the one containing TestCase. Rename unittest.py to myunittest.py and see if it works.
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