0

I'm not sure what's going on, but on my own laptop, everything works okay. When I upload to my host with Python 2.3.5, my views.py can't find anything in my models.py. I have:

from dtms.models import User from dtms.item_list import * 

where my models, item_list, and views files are in /mysite/dtms/

It ends up telling me it can't find User. Any ideas?

Also, when I use the django shell, I can do "from dtms.models import *" and it works just fine.

Okay, after doing the suggestion below, I get a log file of:

syspath = ['/home/victor/django/django_projects', '/home/victor/django/django_projects/mysite'] DEBUG:root:something <module 'dtms' from '/home/victor/django/django_projects/mysite/dtms/__init__.pyc'> DEBUG:root:/home/victor/django/django_projects/mysite/dtms/__init__.pyc DEBUG:root:['/home/victor/django/django_projects/mysite/dtms'] 

I'm not entirely sure what this means - my file is in mysite/dtms/item_list.py. Does this mean it's being loaded? I see the dtms module is being loaded, but it still can't find dtms.models

1
  • Okay, so one of my models is defined as: class Item_list(): blah blah When I do this, on my laptop, python2.5 is able to do: from dtms.item_list import Item_list when I take out the () after Item_list, because of compatibility issues with python2.4, that line doesn't work in python2.5. Could something be going on with this on python2.4? Commented Sep 14, 2009 at 5:00

4 Answers 4

5

The fact that from X import * works does not guarantee that from X import Wowie will work too, you know (if you could wean yourself away from that import * addiction you'd be WAY happier on the long run, but, that's another issue;-).

My general advice in import problems is to bracket the problematic import with try/except:

try: from blah import bluh except ImportError, e: import sys print 'Import error:', e print 'sys.path:', sys.path blah = __import__('blah') print 'blah is %r' % blah try: print 'blah is at %s (%s)' % (blah.__file__, blah.__path__) except Exception, e: print 'Cannot give details on blah (%s)' % e 

and the like. That generally shows you pretty quickly that your sys.path isn't what you thought it would be, and/or blah is at some weird place or with weird path, and the like.

Sign up to request clarification or add additional context in comments.

1 Comment

Do you know how I might be able to get this output when I'm running django on a fastcgi setup? I have no idea where it would actually print anything to.
2

To check your sys.path you can do what Alex said, but instead of using print you can use the logging module:

 import logging LOG_FILENAME = '/tmp/logging_example.out' logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,) logging.debug('This message should go to the log file') 

Comments

0

Make sure your project (or the folder above your "dtms" app) is in python's module search path.

This is something you may need to set in your web server's configuration. The reason it works in the django shell is probably because you are in your project's folder when you run the shell.

This is explained here if you're using apache with mod_python.

4 Comments

You don't have any special local configuration settings do you? (e.g. "local_settings.py" that gets called by settings.py)
I'm using FastCGI at the moment. And my python path contains my django_projects folder, which also contains /mysite/dtms.
You need the folder directly above "dtms" to be in the python path in order for "from dtms.whatever" to work.
Nevermind, looks like the problem still persists however now it says global name is not defined, essentially the same thing. Any ideas?
0

I could be way off with this, but did you set the DJANGO_SETTINGS_MODULE environment variable yet? It affects what you can import. Set it to ".settings". It's also something that gets set when you fire up manage.py, so things work there that won't work in other situations with setting the variable beforehand.

Here's what I do on my system:

export DJANGO_SETTINGS_MODULE=<project name>.settings 

or

import os os.environ['DJANGO_SETTINGS_MODULE']='<project name>.settings' 

Sorry if this misses the point, but when I hear of problems importing models.py, I immediately thing of environment variables. Also, the project directory has to be on PYTHONPATH, but you probably already know that.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.