python - ImportError 和 Django 让我抓狂

标签 python django django-models importerror

好的,我有以下目录结构(这是一个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/

相关文章:

python - Python 3.4 中 +--+ 盒子的尺寸

python - 使用 csv 阅读器从文件中删除行并列出 python

python - 将 pandas DataFrame 插入现有的带有样式的 excel 工作表中

python - 如何通过匹配内容列表搜索 Pandas 中的特定单元格

python - hadoop中的数据沿袭报告

python - Django 上传并显示多张图片

django - 如何从 DRF 中删除自动生成的字段 - Swagger UI

django-models - 如何修复 '' Follow.s_id' 不是 'Users' 的外键。”在此模型中

django - 如何在 Django Rest 框架中显示外键数据?

django - 根据同一表单django字段中的所选项目过滤表单字段中的选择