tags:

views:

172

answers:

4

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

A: 

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.

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

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.

Alex Martelli
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.
victor
A: 

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.

twneale
nope, it's set properly.
victor
+1  A: 

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')
lenin