繁体   English   中英

找不到鼻子测试Web模块

[英]nosetests web module cannot be found

我正在使用python版本2.7.10(计算机上的默认python)。 我有一个要测试的python模块。 该模块中的第一个(也是唯一一个)导入是:

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()

该模块名为app.py ,位于我项目的bin目录下。 我还在此目录中定义了一个__init__.py 我使用鼻子运行名为app_tests.py的相应测试模块,如下所示:

nosetests

我收到以下错误:

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

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

我不知道为什么会这样,因为我可以运行和执行app.py而没有任何问题。

我从项目根目录运行nosetests命令。 项目结构如下所示:

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

我在/usr/bin/python使用系统python,因为我有一些vim插件,如果我使用与brew一起安装的python,它们将无法工作。 我确实链接了通过brew(在/usr/local/bin/python ,版本2.7.13下)安装的/usr/local/bin/python ,并尝试了它,但是我遇到了同样的错误,说找不到web 有什么想法我可以解决这个问题吗?

当您收到此错误时

ImportError:没有名为web的模块

这意味着您还没有模块,您必须安装它或将其放在项目目录中(实际上是项目PATH

您需要安装python-webpy

sudo apt-get install  python-webpy

或使用pip安装它:

sudo pip install web.py

阅读手册。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM