1

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::

enter image description here

Screen shot of files themselves::

enter image description here

enter image description here

enter image description here 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: enter image description here

Game.py:

enter image description here

Otherpy.py - the imported file:

enter image description here

the Nose test script file:

enter image description here

And finally the nosetests importerror:

enter image description here

Jeeves
  • 424
  • 1
  • 7
  • 25
  • Looking over your file layout, you are missing an `__init__.py` in your ex48 folder where settings and temp.py are. Can you ensure that you have the appropriate `__init__.py`'s in place and try again? – idjaw Mar 30 '16 at 20:01
  • Added thanks-- but still getting same result :/ – Jeeves Mar 30 '16 at 20:03
  • Where are you running nose from? inside the `ex48` directory? – idjaw Mar 30 '16 at 20:03
  • Yes, the top ex48 directory inside which is the ex48/ and tests/ – Jeeves Mar 30 '16 at 20:04
  • Where exactly are your `__init__.py` files? – idjaw Mar 30 '16 at 20:09
  • ex48/ex48/__init__.py – Jeeves Mar 30 '16 at 21:24
  • add screenshot of directory listing – Jeeves Mar 30 '16 at 21:30
  • The first screen shot you are importing temp twice. What are you doing there? That should not be happening. Import one way and use that one. – idjaw Mar 30 '16 at 22:11
  • And for the first screenshot, which file is that? – idjaw Mar 30 '16 at 22:15
  • That is scanner.py – Jeeves Mar 31 '16 at 02:43
  • Look at your first screen shot...you have `from temp import temp` then below you have `import temp`. Remove `import temp`. – idjaw Mar 31 '16 at 04:24
  • I removed that. I attached some more screenshots of an entirely different project, created from scratch which gives the same error. This has to be an issue with the path that nosetests is searching for when it does its imports but I've yet to see a workaround – Jeeves Mar 31 '16 at 04:35
  • You have the *exact* same exception when you removed `import temp` from the `scanner.py` file? Are you sure? – idjaw Mar 31 '16 at 04:38
  • Yes same, same error also across projects. – Jeeves Mar 31 '16 at 04:39
  • What do you mean `across projects`? Also, if you removed `import temp`, then you should be getting a different exception because that line should no longer exist in your code. – idjaw Mar 31 '16 at 04:41
  • Across projects == I started from scratch with a new project/directory, created simple .py files and tried to run a nosetests on these files and got the same importerror. I refer to them as the same exception because, while the line numbers may change, the same error results, that being: ImportError: No module named 'whatever' . – Jeeves Mar 31 '16 at 04:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107808/discussion-between-idjaw-and-jeeves). – idjaw Mar 31 '16 at 04:57

2 Answers2

0

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
idjaw
  • 25,487
  • 7
  • 64
  • 83
0

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.

Jeeves
  • 424
  • 1
  • 7
  • 25