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.
Related
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
I am trying to import the importlib module but I get this message:
>>> importlib
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'importlib' is not defined
I have manually located the importlib file within the Python folder, so I know I have it. I am running Python version 3.4.1. I also tried to pip install importlib but it wasn't found either.
What's going on?
You need to use an import statement to load it in:
>>> import importlib
>>> importlib
<module 'importlib' from 'C:\\Python33\\lib\\importlib\\__init__.py'>
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.
I tried to import a file var.py to openfile.py. var is passed as an argument to openfile. I am unable to figure out how to import it by reading the name from command line.
var = sys.argv[1]
importlib.import_module(var, package=None)
I tried the above code but it gave the below error
Traceback (most recent call last):
File "true.py", line 7, in <module>
importlib.import_module(var, package=None)
NameError: name 'importlib' is not defined
you are missing importlib module.
Thats why you are getting NameError: name 'importlib' is not defined
do import importlib
Note:
If imporlib is not installed, you can install it using easy_install/pip on *nix:
easy_install importlib
You have to import the importlib module first:
import importlib
....
importlib.import_module(...)
I have a funny problem I'd like to ask you guys ('n gals) about.
I'm importing some module A that is importing some non-existent module B. Of course this will result in an ImportError.
This is what A.py looks like
import B
Now let's import A
>>> import A
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/tmp/importtest/A.py", line 1, in <module>
import B
ImportError: No module named B
Alright, on to the problem. How can I know if this ImportError results from importing A or from some corrupt import inside A without looking at the error's string representation.
The difference is that either A is not there or does have incorrect import statements.
Hope you can help me out...
Cheers bb
There is the imp module in the standard lib, so you could do:
>>> import imp
>>> imp.find_module('collections')
(<_io.TextIOWrapper name=4 encoding='utf-8'>, 'C:\\Program Files\\Python31\\lib\\collections.py', ('.py', 'U', 1))
>>> imp.find_module('col')
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
imp.find_module('col')
ImportError: No module named col
which raises ImportError when module is not found. As it's not trying to import that module it's completely independent on whether ImportError will be raised by that particular module.
And of course there's a imp.load_module to actually load that module.
You can also look at the back-trace, which can be examined in the code.
However, why do you want to find out - either way A isn't going to work.