Django测试ImportError:没有名为'taskbuster.unittests'的模块(Django test ImportError: No module named 'taskbuster.unittests')
我根据教程创建了一个项目,当我运行测试时,它似乎创建了测试数据库并运行脚本但是我收到错误
ImportError: No module named 'taskbuster.module_name'
我更改了virtualenvwrapper postactivate中的设置。 只会让事情变得更糟,这意味着测试根本没有开始。 我已经更改了base.py设置文件中的路径,但这也使事情变得更糟。
├── db.sqlite3 ├── functional_tests │ ├── __init__.py │ └── test_all_users.py ├── __init__.py ├── manage.py ├── taskbuster │ ├── __init__.py │ ├── settings │ │ ├── base.py │ │ ├── development.py │ │ ├── __init__.py │ │ ├── production.py │ │ └── testing.py │ ├── urls.py │ ├── views.py │ └── wsgi.py └── unittests ├── admin.py ├── __init__.py ├── migrations │ ├── __init__.py │ └── __pycache__ │ └── __init__.cpython-34.pyc ├── models.py ├── __pycache__ │ ├── admin.cpython-34.pyc │ ├── __init__.cpython-34.pyc │ └── models.cpython-34.pyc ├── tests.py └── views.py
这是settings / base.py的INSTALLED_APPS内容
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'unittests', )
testing.py
# -*- coding: utf-8 -*- from .base import * DEBUG = True
wsgi.py
import os from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "taskbuster.settings") application = get_wsgi_application()
manage.py
#!/usr/bin/env python import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "taskbuster.settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv)
在tb_test中进行postactivate
#!/bin/bash # This hook is sourced after this virtualenv is activated. export DJANGO_SETTINGS_MODULE="taskbuster.settings.testing" export SECRET_KEY="" (Secretkey has a value in the actual code)
在tb_test中预先激活
#!/bin/bash # This hook is sourced before this virtualenv is deactivated. unset DJANGO_SETTINGS_MODULE unset SECRET_KEY
我已激活tb_test
$ workon tb_test
然后我运行python测试
$python manage.py test
本教程实际上是在使用functional_tests,但由于这不起作用,我创建了一个Django应用程序单元测试,所以这就是为什么它们都在那里。
我希望有人可以帮助解决这个问题。
提前致谢。
I have created a project according to a tutorial and when I am running the test it seems to create the test database and run the script but I get an error
ImportError: No module named 'taskbuster.module_name'
I have changed the settings in the virtualenvwrapper postactivate. Only made things worse, meaning the test didn't start at all. I have changed the path in the base.py settings file, but that also only made things worse.
├── db.sqlite3 ├── functional_tests │ ├── __init__.py │ └── test_all_users.py ├── __init__.py ├── manage.py ├── taskbuster │ ├── __init__.py │ ├── settings │ │ ├── base.py │ │ ├── development.py │ │ ├── __init__.py │ │ ├── production.py │ │ └── testing.py │ ├── urls.py │ ├── views.py │ └── wsgi.py └── unittests ├── admin.py ├── __init__.py ├── migrations │ ├── __init__.py │ └── __pycache__ │ └── __init__.cpython-34.pyc ├── models.py ├── __pycache__ │ ├── admin.cpython-34.pyc │ ├── __init__.cpython-34.pyc │ └── models.cpython-34.pyc ├── tests.py └── views.py
This is the INSTALLED_APPS content of settings/base.py
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'unittests', )
testing.py
# -*- coding: utf-8 -*- from .base import * DEBUG = True
wsgi.py
import os from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "taskbuster.settings") application = get_wsgi_application()
manage.py
#!/usr/bin/env python import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "taskbuster.settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv)
postactivate in tb_test
#!/bin/bash # This hook is sourced after this virtualenv is activated. export DJANGO_SETTINGS_MODULE="taskbuster.settings.testing" export SECRET_KEY="" (Secretkey has a value in the actual code)
predeactivate in tb_test
#!/bin/bash # This hook is sourced before this virtualenv is deactivated. unset DJANGO_SETTINGS_MODULE unset SECRET_KEY
I have activated tb_test
$ workon tb_test
And then I run a python test
$python manage.py test
The tutorial is actually working with functional_tests, but since that did not work I created a Django app unittests, so that is why they are both in there.
I hope someone could help figure this out.
Thanks in advance.
原文:https://stackoverflow.com/questions/34140639
最满意答案
看起来像pip不适用于安装此版本,我使用setup.py,现在一切似乎都在工作。
looks like pip does not work for installing this version, I used setup.py and everything seems to be working now.
相关问答
更多-
什么是./webapp.py和python webapp.py之间的区别(Whats the difference between ./webapp.py and python webapp.py)[2023-09-18]
直接运行python脚本时(不指定命令中的解释器),您需要告诉shell哪个解释器将处理脚本,例如: #!/usr/bin/env python from flask import Flask from flask import render_template 第一行通常被称为“ shebang ”。 When running a python script directly (without specifying the interpreter in the command), you need to ... -
InvalidHeader at /ebay_accounts/begin_create Header value 3 must be of type str or bytes, not
你必须将str附加到你发送的数据如此str(“不管”) I used code from this repo: https://github.com/luke-dixon/django-ebay-accounts with fix from: here it works great -
我们开发了一个计算量很大的Django网络应用程序(每个过程需要11到88小时才能在高端服务器上完成)。 Celery: Celery是基于分布式消息传递的异步任务队列/作业队列。 它专注于实时操作,但也支持调度。 芹菜提供 异步运行任务。 分布式执行昂贵的进程。 定期和/或计划任务。 如果出现问题,请重试任务。 这只是冰山一角。 芹菜提供了很多功能。 看看文档和常见问题 。 您还需要为工作流程设计一个非常好的画布 。 例如,您不希望所有任务在多个并发用户的情况下同时运行,因为这是一种资源消耗。 您也可以根 ...
-
我认为这是因为你没有调用webapp2.RequestHandler.__init__() 。 以下是来自webapp2.py的代码段: class RequestHandler(object): # ... def __init__(self, request=None, response=None): self.initialize(request, response) 如您所见, RequestHandler已使用request和response初始化。 所以你必须在 ...
-
首先,Postfix邮件路由规则可能非常复杂,你可能首选的解决方案在错误的地方涉及很多欺骗。 你不想不小心显示一些用户和他们的邮件,是吗? 其次,尽管Postfix几乎可以做任何事情,但它不应该只是一个MDA(邮件投递代理)。 您的解决方案最好通过使用POP3或IMAP服务器(Cyrus IMAPd,Courier等)来解决。 IMAP服务器可以拥有可以阅读所有用户邮件的“超级用户帐户”。 然后,您的Web应用程序可以连接到用户邮箱并检索标题和身体。 如果您只想显示主题行,则可以使用特殊的IMAP命令获取这 ...
-
只需使用import os在settings.py等Django代码中导入os包,然后通过代码os.getenv("
") 。 对于在App settings定义的环境变量,您只需传递定义为os.getenv()的变量名称作为os.getenv()的参数os.getenv() 。 要获取Connection strings定义的环境变量的值,您需要为VARNAME这样的变量名称添加不同的前缀或外VARNAME ,如下所示。 对于SQL Database ,使用前缀SQLAZ ... -
如果您在多线程配置或甚至多进程配置中使用mod_wsgi,则一个请求不应阻止另一个请求执行某些操作。 他们应该能够同时运行。 如果使用多线程配置,您确定您没有在自己的应用程序中的某些资源上使用某种锁定机制,这会阻止通过相同代码段运行的请求吗? 另一种可能性是您已经很好地配置了Apache MPM和/或mod_wsgi守护程序模式,以便排除并发请求。 无论如何,正如另一个答案中所提到的,你最好先看一下缓存策略,以便首先避免天气查找,或者卸载到客户端。 If you are using mod_wsgi in ...
-
我推荐django上的jinja2模板: http : //jinja.pocoo.org/它基于django模板,但更强大,更易于使用。 我不认为在没有django堆栈的情况下使用Django模板是个好主意。 此外,如果你想要一个更结构化的框架,一个非常好的极简主义框架是Flask: http : //www.pocoo.org/projects/flask/#flask 。 我不能赞美这两个图书馆。 我在Django工作了很长时间,发现这个组合非常清爽简洁。 PS这应该是一个非常简单的移植过程。 当我刚 ...
-
看起来像pip不适用于安装此版本,我使用setup.py,现在一切似乎都在工作。 looks like pip does not work for installing this version, I used setup.py and everything seems to be working now.
-
Django报告说数据库不会保存您的Post数据,因为slug字段的值已经被另一个Post使用了。 如果您不想要此行为,请不要在模型中的Post.slug unique属性设置为True 。 但是请注意, slug通常用于查询数据库以查找关联的Post ,因此通常您会希望它是唯一的。 Django is reporting that the database will not save your Post data because the value of the slug field is already ...