Search code examples
pythonnose

nosetests web module cannot be found


I am using python version 2.7.10 (the default python on my machine). I have a python module which I would like to test. The first (and only) import in the module is:

import web

urls = (
    '/hello', 'index'
)

app = web.application(urls, globals())

render = web.template.render('templates/', base="layout")

class index:
    def GET(self):
        return render.hello_form()

    def POST(self):
        form = web.input(name="Nobody", greet="Hello")
        greeting = "%s %s" % (form.greet, form.name)
        return render.index(greeting = greeting)

if __name__ == "__main__":
    app.run()

The module is called app.py and resides under the bin directory of my project. I also have a __init__.py defined in this directory. I run the corresponding test module, called app_tests.py using nose, like so:

nosetests

and I get the following error:

======================================================================
ERROR: Failure: ImportError (No module named web)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/loader.py", line 418, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/local/lib/python2.7/site-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/local/lib/python2.7/site-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/Users/dev/python/workspace/projects/gothonweb/tests/app_tests.py", line 2, in <module>
    from bin.app import app
  File "/Users/dev/python/workspace/projects/gothonweb/bin/app.py", line 1, in <module>
    import web
ImportError: No module named web

----------------------------------------------------------------------

I have no idea why this is happening as I can run and execute app.py without any issues.

I run the nosetests command from my project root. The project structure looks like so:

devs-MacBook-Pro:gothonweb dev$ ls -R
bin     docs        gothonweb   setup.py    templates   tests

./bin:
__init__.py __init__.pyc    app.py      app.pyc

./docs:

./gothonweb:
__init__.py __init__.pyc

./templates:
hello_form.html index.html  layout.html

./tests:
__init__.py     app_tests.py        gothonweb_tests.py  tools.py
__init__.pyc        app_tests.pyc       gothonweb_tests.pyc tools.pyc

I am using the system python in /usr/bin/python because I have some vim plugins which won't work if I use the python I installed with brew. I did link the python installed via brew (under /usr/local/bin/python, version 2.7.13) and tried that instead, but I get the same error, saying web cannot be found. Any ideas how I can fix this?


Solution

  • When you got this error

    ImportError: No module named web

    That means you haven't a module, You must install it or have it in your project directory (Actually project PATH)

    You need to install python-webpy

    sudo apt-get install  python-webpy
    

    or install it with pip:

    sudo pip install web.py
    

    Read this manual.