I'm currently working through LearnPythonTheHardWay and have reached Exercise 48 which details Nosetests. I am able to perform a unit testing as long as all of the code is in a single python.py file. However if I include other files as part of a program, i.e. use import and then attempt to nosetest such a project I am getting an error, as follows:
======================================================================
ERROR: Failure: ImportError (No module named 'temp')
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/nose/failure.py", line 39, in runTest
raise self.exc_val.with_traceback(self.tb)
File "/usr/local/lib/python3.4/dist-packages/nose/loader.py", line 414, in loadTestsFromName ## ##
addr.filename, addr.module)
File "/usr/local/lib/python3.4/dist-packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/usr/local/lib/python3.4/dist-packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/usr/lib/python3.4/imp.py", line 235, in load_module
return load_source(name, filename, file)
File "/usr/lib/python3.4/imp.py", line 171, in load_source
module = methods.load()
File "", line 1220, in load
File "", line 1200, in _load_unlocked
File "", line 1129, in _exec
File "", line 1471, in exec_module
File "", line 321, in _call_with_frames_removed
File "/home/user/LEARNPYTHONTHEHARDWAY/ex48/tests/scanner_tests.py", line 6, in
from ex48.scanner import lexicon
File "/home/user/LEARNPYTHONTHEHARDWAY/ex48/ex48/scanner.py", line 6, in
import temp
ImportError: No module named 'temp'
Ran 1 test in 0.028s
FAILED (errors=1)
The structure of my project directories are as follows:
ex48/
ex48/
scanner.py
temp.py
__pycache__/
tests/
__init__.py
scanner_tests.py
Screenshot of my directory::
Screen shot of files themselves::
My scanner_tests.py file is as follows:
from nose.tools import *
from ex48.scanner import lexicon
from ex48 import temp
def test_directions():
assert_equal(lexicon.scan("north"),[('direction','north')])
result = lexicon.scan("north south east")
assert_equal(result, [('direction', 'north'),
('direction', 'south'),
('direction', 'east')])
My scanner.py file is as follows:
import temp
class lexicon:
def scan(val):
if(val == "north"):
return [('direction', 'north')]
else:
return [('direction', 'north'),
('direction', 'south'),
('direction', 'east')]
runner = temp.temp("hello")
And finally my temp.py file is as follows:
class temp(object):
def __init__(self,name):
self.name = name
def run(self):
print "Your name is; %s" % self.name
runner.run()
My question is how to overcome the ImportError: No Module named 'temp' because it seems as if I have imported the temp.py file in both the scanner.py file and the scanner_tests.py file but nose does not seem to be able to import it when it runs. Nosetests works fine when its just the single scanner.py file but not when importing. Is there a special syntax for importing into a unit test for nose? The script also works fine when run and imports properly at the command line.
*Note: I'm running python off a limited account off an online server so some admin privileges are not available.
**Note below are entirely different screenshots from another project with the exact same error:
Directory Layout:
Game.py:
Otherpy.py - the imported file:
the Nose test script file:
And finally the nosetests importerror:
Everything needs to be with respect to your execution point. You are running your nose command from the root of ex48, therefore all your imports need to be with respect to that location.
Therefore, in game.py you should be importing with respect to ex48. Therefore:
from ex48.otherpy import House
The same logic should be applied to your example referencing the temp folder.
from ex48.temp import temp
The only solution I have found is to post the following to the top of the main file:
try:
# This handles imports when running .py files from inside app directory
from file_to_import.py import class_instance
except:
# This handles imports when running nosetests from top-level (above app)
# directory
from directory_containing_app_files.file_to_import import class_instance
I am supremely interested in an alternative solution.
Related
I am trying to Use PyUnit within PyDev for the first time. I created a unittest.py module. When I did run as -> Python unit-test, I got the following error:
Finding files... done.
Importing test modules ... done.
======================================================================
Traceback (most recent call last):
File "/Applications/eclipse/plugins/org.python.pydev_2.7.5.2013052819/pysrc/runfiles.py", line 163, in
main()
File "/Applications/eclipse/plugins/org.python.pydev_2.7.5.2013052819/pysrc/runfiles.py", line 77, in main
pydev_runfiles.main(configuration)
File "/Applications/eclipse/plugins/org.python.pydev_2.7.5.2013052819/pysrc/pydev_runfiles.py", line 761, in main
PydevTestRunner(configuration).run_tests()
File "/Applications/eclipse/plugins/org.python.pydev_2.7.5.2013052819/pysrc/pydev_runfiles.py", line 747, in run_tests
runner.run(test_suite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/runner.py", line 158, in run
result.printErrors()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/runner.py", line 108, in printErrors
self.printErrorList('ERROR', self.errors)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/runner.py", line 114, in printErrorList
self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/runner.py", line 46, in getDescription
return '\n'.join((str(test), doc_first_line))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 1060, in __str__
self._testFunc.__name__)
AttributeError: 'str' object has no attribute '__name__'
My unit test is just the PyDev-generated default:
import unittest
class Test(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def testName(self):
pass
print "hello test"
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
I expected it to print hello test. What am I missing?
Ok, I realized your problem, after having a similar issue for several minutes. You need to rename your file. Name it anything other than unittest.py. This causes the compiler to confuse between your module and the unittest module that you are importing.
Second, even after you change the name of your file, you may run into the same error. That is caused by the unittest.pyc file not being removed from your project. Rename the file, delete the previously generated unittest.pyc file, and the test should run just fine.
I am currently developing wep.py application. This is my web application which is binded with web.py and wsgi.
root/main.py
import web
import sys
import imp
import os
sys.path.append(os.path.dirname(__file__))
#from module import module
from exam import exam
urls = (
'/exam', 'exam'
)
application = web.application(urls, globals(), autoreload = True).wsgifunc()
My application has an abstract class called module in module.py in root directory and its purpose is to be inherited by modules.
root/module.py
class module:
def fetchURL(self, url):
# ...
return content
The lower level module called "exam" would inherits module
root/exam/init.py
from module import module
class exam(module):
def getResults(self):
# error occurs here
self.fetchURL('math.json')
When I call the parent method, web.py raises an exception
WalkerError: ('unexpected node type', 339)
Environment: Python 2.5
How can I resolve the problem? Thanks
// EDIT 03 July 10:22 GMT+0
The stack trace is as follows
mod_wsgi (pid=1028): Exception occurred processing WSGI script 'D:/py/labs_library/index.py'.
Traceback (most recent call last):
File "D:\csvn\Python25\lib\site-packages\web\application.py", line 277, in wsgi
result = self.handle_with_processors()
File "D:\csvn\Python25\lib\site-packages\web\application.py", line 247, in handle_with_processors
return process(self.processors)
File "D:\csvn\Python25\lib\site-packages\web\application.py", line 244, in process
raise self.internalerror()
File "D:\csvn\Python25\lib\site-packages\web\application.py", line 467, in internalerror
return debugerror.debugerror()
File "D:\csvn\Python25\lib\site-packages\web\debugerror.py", line 305, in debugerror
return web._InternalError(djangoerror())
File "D:\csvn\Python25\lib\site-packages\web\debugerror.py", line 290, in djangoerror
djangoerror_r = Template(djangoerror_t, filename=__file__, filter=websafe)
File "D:\csvn\Python25\lib\site-packages\web\template.py", line 845, in __init__
code = self.compile_template(text, filename)
File "D:\csvn\Python25\lib\site-packages\web\template.py", line 924, in compile_template
ast = compiler.parse(code)
File "D:\csvn\Python25\lib\compiler\transformer.py", line 51, in parse
return Transformer().parsesuite(buf)
File "D:\csvn\Python25\lib\compiler\transformer.py", line 128, in parsesuite
return self.transform(parser.suite(text))
File "D:\csvn\Python25\lib\compiler\transformer.py", line 124, in transform
return self.compile_node(tree)
File "D:\csvn\Python25\lib\compiler\transformer.py", line 167, in compile_node
raise WalkerError, ('unexpected node type', n)
WalkerError: ('unexpected node type', 339)
If it is possible I would like to turn off the template functionality as I use python only for JSON output for mobile app.
if you create python module you should add __init__.py in top of your hierarchy:
dvedit/
__init__.py
clipview.py
filters/
__init__.py
it means that in every directory which will be imported via from ... import ... should have __init__.py file.
further info available: http://wiki.cython.org/PackageHierarchy
I am trying to get started with using nosegae, however I run into the issue that I can't seem to get it to pass even the simplest of cases when using django.
when running without the --without-sandbox flag both the following tests fail
def test_import_django ():
import django
def test_import_django_http ():
import django.http
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose-1.1.2-py2.7.egg\nose\case.py", line 1
97, in runTest
self.test(*self.arg)
File "C:\Users\User\Desktop\TDD_GAE\myproj\tests.py", line 2, in test_import_d
jango
import django
File "C:\Python27\lib\site-packages\nosegae-0.1.9-py2.7.egg\nosegae.py", line
207, in find_module
return super(HookMixin, self).find_module(fullname, path)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\de
v_appserver.py", line 1505, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\de
v_appserver.py", line 1998, in find_module
search_path)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\de
v_appserver.py", line 1505, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\de
v_appserver.py", line 2119, in FindModuleRestricted
result = self.FindPathHook(submodule, submodule_fullname, path_entry)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\de
v_appserver.py", line 2219, in FindPathHook
return self._imp.find_module(submodule, [path_entry])
Howevere if I do use --without-sandbox at least the first test passes
myproj.tests.test_import_django ... ok
myproj.tests.test_import_django_http ... ERROR
======================================================================
ERROR: myproj.tests.test_import_django_http
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose-1.1.2-py2.7.egg\nose\case.py", line 1
97, in runTest
self.test(*self.arg)
File "C:\Users\User\Desktop\TDD_GAE\myproj\tests.py", line 5, in test_import_d
jango_http
import django.http
File "C:\Program Files (x86)\Google\google_appengine\lib\django_1_2\django\htt
p\__init__.py", line 9, in <module>
from mod_python.util import parse_qsl
File "C:\Python27\lib\site-packages\nosegae-0.1.9-py2.7.egg\nosegae.py", line
199, in find_module
mod_path = self.find_mod_path(fullname)
File "C:\Python27\lib\site-packages\nosegae-0.1.9-py2.7.egg\nosegae.py", line
251, in find_mod_path
_sf, path, _desc= self._imp.find_module(top, None)
AttributeError: 'str' object has no attribute 'find_module'
Has anyone encountered and know how I can go about past this?
Edit
It seems that the issue is recursive imports
def test_import_pdb ():
import pdb
pdb.set_trace ()
part of the stack trace is
File "C:\Python27\lib\pdb.py", line 72, in __init__
import readline
notice that an import in __init__ of django.http is also part of the stack trace
Read https://docs.djangoproject.com/en/dev/topics/testing/ about Django testing.
As I know it's better to use unittest or doctest shipped with django as it have several improvements for django-specific testing like form field output testing and some database features. Hovewer it's not essential and if you want to continue using nose - think you missed django environment setup:
from django.test.utils import setup_test_environment
setup_test_environment()
This lines needed to run your tests outside of ./manage.py --test
UPD
Yeah my previous thought's were wrong. So I just digged into sources of nose and nose-gae, and what I think - check HardenedModulesHook definition in your nose version, cause in trunk of nose I've found following:
class HardenedModulesHook(object):
...
def __init__(self,
module_dict,
imp_module=imp,
os_module=os,
dummy_thread_module=dummy_thread,
pickle_module=pickle):
...
That gives following - when noseGAE plugin begin() method is executed -> there self._install_hook(dev_appserver.HardenedModulesHook) is called which declares mixed-hook class and creates it's instance like self.hook = Hook(sys.modules, self._path). <- There is HardenedModulesHook.__init__ called with second argument as mystic '_path' however in NOSE this argument should be 'imp' module by default -> That makes an exception you've got:
_sf, path, _desc= self._imp.find_module(top, None)
AttributeError: 'str' object has no attribute 'find_module'
So I think it might be a problem with nose-gae :(
I was trying to install a module for opencv and added an opencv.pth file to the folder beyond my sites.py file. I have since deleted it and no change.
When I try to run help('modules'), I get the following error:
Please wait a moment while I gather a
list of all available modules...
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/twisted/words/im/init.py:8:
UserWarning: twisted.im will be
undergoing a rewrite at some point in
the future.
warnings.warn("twisted.im will be
undergoing a rewrite at some point in
the future.")
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pkgutil.py:110:
DeprecationWarning: The wxPython
compatibility package is no longer
automatically generated or actively
maintained. Please switch to the wx
package as soon as possible.
import(name) Traceback (most recent call last): File "",
line 1, in File
"/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site.py",
line 348, in call
return pydoc.help(*args, **kwds) File
"/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pydoc.py",
line 1644, in call
self.help(request) File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pydoc.py",
line 1681, in help
elif request == 'modules': self.listmodules() File
"/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pydoc.py",
line 1802, in listmodules
ModuleScanner().run(callback) File
"/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pydoc.py",
line 1853, in run
for importer, modname, ispkg in pkgutil.walk_packages(): File
"/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pkgutil.py",
line 110, in walk_packages
import(name) File "/BinaryCache/wxWidgets/wxWidgets-11~262/Root/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/wxaddons/init.py",
line 180, in import_hook File
"/Library/Python/2.5/site-packages/ctypes_opencv/init.py",
line 19, in
from ctypes_opencv.cv import * File
"/BinaryCache/wxWidgets/wxWidgets-11~262/Root/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/wxaddons/init.py",
line 180, in import_hook File
"/Library/Python/2.5/site-packages/ctypes_opencv/cv.py",
line 2567, in ('desc', CvMat_r, 1), # CvMat* desc File
"/Library/Python/2.5/site-packages/ctypes_opencv/cxcore.py",
line 114, in cfunc
return CFUNCTYPE(result, *atypes)((name, dll), tuple(aflags)) AttributeError: dlsym(0x2674d10, cvCreateFeatureTree): symbol not found
What gives?!
This happens because help('modules') imports all modules, which can result in a lot of unsentineled code being executed. There's nothing you can do short of reporting bugs in every single package that causes this (opencv in this case) and wait for them to fix it.
I write a program :
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def main():
app = QApplication([])
button = QPushButton("hello?")
button.show()
app.exec_()
if __name__=="__main__":
main()
the file name is t.py,
when I run:
pylint t.py
in ubuntu9.10, pyqt4,
I got this:
pylint t.py
No config file found, using default configuration
error while building astng for /home/halida/data/workspace/test/t.py
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.6/logilab/astng/manager.py", line 126, in astng_from_file
astng = ASTNGBuilder(self).file_build(filepath, modname)
File "/usr/lib/pymodules/python2.6/logilab/astng/builder.py", line 118, in file_build
node = self.string_build(data, modname, path)
File "/usr/lib/pymodules/python2.6/logilab/astng/builder.py", line 128, in string_build
return self.ast_build(parse(data + '\n'), modname, path)
File "/usr/lib/pymodules/python2.6/logilab/astng/builder.py", line 147, in ast_build
self.rebuilder.walk(node)
File "/usr/lib/pymodules/python2.6/logilab/astng/rebuilder.py", line 89, in walk
self._walk(node)
File "/usr/lib/pymodules/python2.6/logilab/astng/rebuilder.py", line 109, in _walk
self._walk(child, node)
File "/usr/lib/pymodules/python2.6/logilab/astng/rebuilder.py", line 103, in _walk
handle_leave = node.accept(self)
File "/usr/lib/pymodules/python2.6/logilab/astng/nodes.py", line 159, in accept
return func(self)
File "/usr/lib/pymodules/python2.6/logilab/astng/rebuilder.py", line 188, in visit_from
imported = node.root().import_module(node.modname)
File "/usr/lib/pymodules/python2.6/logilab/astng/scoped_nodes.py", line 282, in import_module
return MANAGER.astng_from_module_name(self.relative_name(modname, level))
File "/usr/lib/pymodules/python2.6/logilab/astng/manager.py", line 172, in astng_from_module_name
return self.astng_from_module(module, modname)
File "/usr/lib/pymodules/python2.6/logilab/astng/manager.py", line 207, in astng_from_module
astng = ASTNGBuilder(self).module_build(module, modname)
File "/usr/lib/pymodules/python2.6/logilab/astng/builder.py", line 80, in module_build
node = self.inspect_build(module, modname=modname, path=path)
File "/usr/lib/pymodules/python2.6/logilab/astng/builder.py", line 95, in inspect_build
self.object_build(node, module)
File "/usr/lib/pymodules/python2.6/logilab/astng/builder.py", line 195, in object_build
self.object_build(class_node, member)
File "/usr/lib/pymodules/python2.6/logilab/astng/builder.py", line 198, in object_build
object_build_methoddescriptor(node, member)
File "/usr/lib/pymodules/python2.6/logilab/astng/raw_building.py", line 150, in object_build_methoddescriptor
func = build_function(member.__name__, doc=member.__doc__)
AttributeError: 'PyQt4.QtCore.pyqtSignal' object has no attribute '__name__'
************* Module t
F: 1: <class 'logilab.astng._exceptions.ASTNGBuildingException'>: Unable to load module t ('PyQt4.QtCore.pyqtSignal' object has no attribute '__name__')
Compilation exited abnormally with code 1 at Sat Dec 26 10:43:54
in windows XP, with pythonxy,
I only got a error message, why?
Looks like a bug in astng. They try to read the name of a function which does not publish it (native extension func). I'd report a bug to both astng and pyqt projects. The first one would be that they should handle a no-name situation better. The second one would be that every sane extension should publish at least the function names.
I would check if you're using the very latest astng, pylint, etc.