victor
victor

Reputation: 6768

Cannot import file in Python/Django

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

Upvotes: 0

Views: 3416

Answers (4)

leninyee
leninyee

Reputation: 356

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

Upvotes: 2

twneale
twneale

Reputation: 2946

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.

Upvotes: 0

Fragsworth
Fragsworth

Reputation: 35497

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.

Upvotes: 0

Alex Martelli
Alex Martelli

Reputation: 881635

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.

Upvotes: 5

Related Questions