好的,我有以下目录结构(这是一个django项目):
-> project --> app
and within the app folder, there is a scraper.py file which needs to reference a class defined within models.py
I'm trying to do the following:
import urllib2
import os
import sys
import time
import datetime
import re
import BeautifulSoup
sys.path.append('/home/userspace/Development/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
from project.app.models import ClassName
而且这段代码无法正常工作。我收到以下错误:
Traceback (most recent call last): File "scraper.py", line 14, in from project.app.models import ClassName ImportError: No module named project.app.models
上面的这段代码曾经有效,但在某处出错了,我对为什么会遇到问题感到非常困惑。在 SnowLeopard 上使用 python2.5。
最佳答案
import sys
sys.path.append ('/path/to/the/project')
from django.core.management import setup_environ
import settings
setup_environ(settings)
from app.models import MyModel
关于python - ImportError 和 Django 让我抓狂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2575859/