ImportError:无法导入名称信号
我使用 Django 1.3.0 和 Python 2.7.1。 在每次测试中,我编写以下导入,都会得到上面的导入错误:
from django.utils import unittest from django.test.client import Client
完整的堆栈跟踪:
File "C:\Program Files (x86)\j2ee\plugins\org.python.pydev.debug_1.6.3.2010100513\pysrc\runfiles.py", line 342, in __get_module_from_str mod = __import__(modname) File "C:/Users/benjamin/workspace/BookIt/src/BookIt/tests\basic_flow.py", line 11, in from django.test.client import Client File "C:\Python27\lib\site-packages\django\test\__init__.py", line 5, in from django.test.client import Client, RequestFactory File "C:\Python27\lib\site-packages\django\test\client.py", line 21, in from django.test import signals ImportError: cannot import name signals ERROR: Module: basic_flow could not be imported.
有什么想法为什么会发生这种情况吗?
I'm using Django 1.3.0 with Python 2.7.1.
In every test I write the following imports I get the importError above:
from django.utils import unittest from django.test.client import Client
The full stack trace:
File "C:\Program Files (x86)\j2ee\plugins\org.python.pydev.debug_1.6.3.2010100513\pysrc\runfiles.py", line 342, in __get_module_from_str mod = __import__(modname) File "C:/Users/benjamin/workspace/BookIt/src/BookIt/tests\basic_flow.py", line 11, in from django.test.client import Client File "C:\Python27\lib\site-packages\django\test\__init__.py", line 5, in from django.test.client import Client, RequestFactory File "C:\Python27\lib\site-packages\django\test\client.py", line 21, in from django.test import signals ImportError: cannot import name signals ERROR: Module: basic_flow could not be imported.
Any ideas why this happening ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@Hugo 是对的,这是一个 settings.py 问题。但我在Django环境中运行时并没有遇到这个问题。但是当我想一一运行单元测试时(通过使用 Pydev 的 run as unittest),它无法运行。
我需要做的是添加 Django 设置模块信息,所以现在我要做的是以下行添加到我的单元测试中:
这会加载我的 Django 项目设置并允许我作为常规单元测试运行。
如果有人对如何在 Pydev 中更干净地配置它有更好的建议,请告诉我。
@Hugo was right in that it was a settings.py problem. But I didn't had that problem when running through the Django environment. But when I wanted to run unit tests one by one (By using Pydev's run as unittest) it failed to run.
What I needed to do was to add the Django settings module information, so for now what I'm doing is adding the following lines to my unit tests:
This loads my Django project settings and allow me to run as regular unittest.
If anyone has better suggestion on how to configure this more cleanly in Pydev please let me know.
就在一分钟前我也遇到了同样的问题。经过调查,我意识到问题出在我的 settings.py* 文件上。
检查 Django 正确查找设置文件时是否遇到问题。
这个错误信息完全是无意义的。
* IIRC Django 查找 settings.py 文件,如果没有找到,它会查找环境变量 DJANGO_SETTINGS_MODULE 并尝试。
I had the same problem just a minute ago. Investigating I realized the problem was with my settings.py* file.
Check if you are having problems with Django finding your settings file correctly.
This error message is totally non-sense.
* IIRC Django looks for settings.py file, if not found, it looks for environment variable DJANGO_SETTINGS_MODULE and try that.
这很容易解决。
如果您已经编写了settings.py(最有可能),只需导航到包含“settings.py”文件的目录并执行它。
1]蟒蛇
2]导入设置
这些命令应该可以解决问题。然后转到任意文件夹并继续执行。
This is easy to solve.
If you have already written the settings.py (most probable) just navigate into the directory which contains the "settings.py" file and execute it.
1] python
2] import settings
These commands should do the trick. Then go to any folder and continue with execution.