首页 \ 问答 \ Django测试ImportError:没有名为'taskbuster.unittests'的模块(Django test ImportError: No module named 'taskbuster.unittests')

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
更新时间:2022-12-25 20:12

最满意答案

看起来像pip不适用于安装此版本,我使用setup.py,现在一切似乎都在工作。


looks like pip does not work for installing this version, I used setup.py and everything seems to be working now.

相关问答

更多
  • 直接运行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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)