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

I have file with unittest named: test.py
My code:
import unittest
class Test(unittest.TestCase):
def myTest(self):
a = 1
self.assertEqual(a, 1)
if __name__ == '__main__':
unittest.main()
When I press F5, I get an error:
Traceback (most recent call last):
File "/home/mariusz/Pulpit/test.py", line 1, in <module>
import unittest
File "/home/mariusz/Pulpit/unittest.py", line 3, in <module>
AttributeError: 'module' object has no attribute 'TestCase'

You have a local file named unittest.py that is being imported instead:
/home/mariusz/Pulpit/unittest.py
Rename that file or remove it altogether. Make sure you remove any corresponding unittest.pyc file in the same folder if it is there.
The file is masking the standard library package.

Your script named unittest.py is replacing the module file.
Rename your unittest.py script to something else.

In my case, one of the dependency was not there.
import os
from some_package import some_module
the some_module was not there in python(python couldn't import it). Once I commented the import statement python started discovering my test cases.
python -m unittest tests.test_my_own_module

Related

AttributeError on 'tkinter' project

I get the following AttributeError:
Traceback (most recent call last):
File "C:\Users\thaku\OneDrive\Desktop\tkinter python\tkinter.py", line 1, in <module>
import tkinter
File "C:\Users\thaku\OneDrive\Desktop\tkinter python\tkinter.py", line 2, in <module>
win = tkinter.Tk()
AttributeError: partially initialized module 'tkinter' has no attribute 'Tk' (most likely due to a circular import)
this is my code snippet
import tkinter
win = tkinter.Tk()
win.title('GUI')
win.mainloop()
From the traceback I can see that you named your file tkinter.py which confuses python as it thinks that you are trying to import that file from itself. If you rename your file to something else it should work.
Well I looked into Traceback and found the problem.
You have named the file on which you have been working on as tkinter.py. While using the command import tkinter it imports your file (the one you are working on) itself rather than import the module tkinter.
The preferred answer would be that you rename the file you are working on as tkinter_pratice.py or something like that.
You can now learn that you can't give a file the same name as a module's name as if would import itself rather than importing that particular module.
You have named you file tkinter.py because of which python imports that file as the module and also runs the same file while running which creates a loop and gives that error
Solution:
Name your file something else.
For example:- tkinter_.py or whatever else you want except for the modules name itself as that is reserved for the python interpreter.

Running python from command line gives import error

I want to run my python program from command line but it gives me below error
ImportError: No module named 'main'
My folder structure is as below
Project
|-----main
|-----__init__.py
|-----S3Operations.py
|-----BusinessOperations.py
My init.py code is as below
import sys
from S3Operations import Models
sys.path.append("D:/code/Project/main")
if __name__ == '__main__':
s3=Models()
s3.test()
And my S3Operations.py code is
import os.path
from main import BusinessService
class ModelsMlS3(object):
def test(self):
print("Testing success")
When I run the program using command line i get the below error
$ python __init__.py
Traceback (most recent call last):
File "__init__.py", line 2, in <module>
from S3Operations import ModelsMlS3
File "D:\code\Project\main\S3Operations.py", line 11, in <module>
from main import BusinessService
ImportError: No module named 'main'
Can any one please suggest a solution for the same.
You just need to do:
import BusinessService # instead of `from main import BusinessService`
as in your project, there is no __init__.py file present in Project directory (which holds main.py).
For importing it like:
from main import BusinessService
you need to create __init__.py in the folder in order to make it as module.

python absolute import of submodule fails

I have seen other posts but did not find answer that worked!
File structure
my_package/
__init__.py -- empty
test/
__init__.py -- empty
test1.py
Fail
from my_package import test
test.test1
gives
AttributeError: 'module' object has no attribute test
Following Passes
from my_package.test import test1
# or
import my_package.test.test1
from my_package import test
# now this works
test.tes1
<module 'my_package.test.test1' from ...
I have
from __future__ import absolute_import
in all files, and using python2.7
When you import a package (like test), the modules (like test1) are not automatically imported (unless perhaps you put some special code to do so in __init__.py). This is different from importing a module, where the contents of the module are available in the modules namespace. Compare with the Python standard library's xml.etree.ElementTree module:
>>> import xml
>>> xml.etree
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'etree'
>>> from xml import etree
>>> etree.ElementTree
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'ElementTree'
>>> from xml.etree import ElementTree
>>> ElementTree.ElementTree
<class 'xml.etree.ElementTree.ElementTree'>

How can you find where python imported a particular module from?

How can you find where python imported a particular module from?
Each module object has a __file__ attribute:
import module
print module.__file__
Some modules are part of the Python executable; these will not have the attribute set.
Demo:
>>> import urllib2
>>> urllib2.__file__
'/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.pyc'
>>> import sys
>>> sys.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
You could also run Python in verbose mode, with the -v command line switch or the PYTHONVERBOSE environment variable; Python then prints out every imported file as it takes place.

How to mock python module in unittest

I have a problem when mocking in unittest.
#!/usr/bin/env python
import sys
sys.modules["foo.Bar"] = __import__("mock_bar")
import foo.Bar
print foo.Bar.__name__
I've got an ImportError exception in line 4. I don't know why since I have do some mock at line 3. There is a reference of how to mock import here.
Here's the error message:
Traceback (most recent call last):
File "test.py", line 4, in <module>
import foo.Bar
ImportError: No module named foo.Bar
"import foo.Bar" should equal to "__import__('foo.Bar')", and before that I've hacked sys.modules to pretend module 'foo.Bar' has been already imported. Why python still try to import foo.Bar and complain?
Try doing import foo before your __import__ line: I think it could help.

Categories

Resources