7
votes

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 ?

3
I've read this question, could this be because Client is imported twice ? once in C:\Python27\lib\site-packages\django\test\__init__.py and again in my test C:/Users/benjamin/workspace/BookIt/src/BookIt/tests\basic_flow.py.If this is it, then how can import the Client to my test ?Benjamin K.
It would only matter if importing Client creates a circular import, i.e. file_A imports file_B and file_B imports file_A. It doesn't matter if the imports are not full. Python must still parse the module to pull out the individual item(s). Sometimes circular imports are necessary, though, so if that's your case, you can wrap the conflicting imports in a try block, using ImportError as your exception.Chris Pratt
I encountered this problem when, from a python shell, I tried to import to import my django models. It failed (because of no settings module set), I handled that, and then tried to import them again, and I got this error. Somehow, this process got my module namespace into an unclean state, but @BenjaminK.'s thought that something was getting imported twice made me realize this was going on.Brian Peterson

3 Answers

5
votes

@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:

from django.core import management;
import BookIt.settings as settings;
management.setup_environ(settings)

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.

3
votes

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.

-1
votes

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.