ImportError 和 Django 让我抓狂

发布于 2024-08-27 20:18:05 字数 696 浏览 13 评论 0原文

好的,我有以下目录结构(它是一个 django 项目):

-> project
--> app

在 app 文件夹中,有一个 scraper.py 文件需要引用 models.py 中定义的类

我正在尝试执行以下操作


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。

OK, I have the following directory structure (it's a django project):

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

and this code just isn't working. I get an error of:

Traceback (most recent call last):
  File "scraper.py", line 14, in 
    from project.app.models import ClassName
ImportError: No module named project.app.models

This code above used to work, but broke somewhere along the line and I'm extremely confused as to why I'm having problems. On SnowLeopard using python2.5.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阪姬 2024-09-03 20:18:05
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
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
街角迷惘 2024-09-03 20:18:05

哇哇哇。您永远不应该将项目名称放入任何应用代码中。您应该能够在多个项目中重用应用程序代码而不进行任何更改。 Pinax 在这方面做得非常好,我强烈建议您查看它以获取许多 django 最佳实践。

您在这里可以做的最糟糕的事情是将您的绝对路径硬编码到您的应用程序或设置中。您不应该这样做,因为除非您进行一些 import local_settings 黑客攻击,否则它会在部署过程中中断。

如果您必须访问项目根目录,请尝试 pinax 在 settings.py 中提供的内容...

import os.path
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

问题是,您似乎正在尝试访问同一个应用程序中的模型模块,这更容易。

要在同一目录中的 scraper.py 中导入 models.py,只需使用 import modelsimport models as app_models 如果你在 scraper.py 中已经有名为 models 的东西(django.py)。例如 db.models)。您熟悉 Python 模块约定吗?

然而,最好的方法可能是坚持使用 django 习惯用法, from ... import ... 语句:

from app import models

如果这不能自动工作,那么你的 settings.py 中就有问题。

Whoa whoa whoa. You should never ever have to put your project name in any of your app code. You should be able to reuse app code across multiple projects with no changes. Pinax does this really well and I highly recommend checking it out for a lot of django best practices.

The worst thing you could do here is to hard code your absolute path into your app or settings. You shouldn't do this because it will break during deployment unless you do some import local_settings hacking.

If you have to access the project root directory, try what pinax has in settings.py...

import os.path
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

The thing is that it looks like you are trying to access the models module within the same app and this is waaay easier.

To import models.py inside scraper.py in the same directory just use import models or import models as app_models if you already have something named models in scraper.py (django.db.models for instance). Are you familiar with Python module conventions?

However, the best way is probably to stick with the django idiom, from ... import ... statement:

from app import models

If this doesn't work automatically, then something is wrong in your settings.py.

笑咖 2024-09-03 20:18:05

您没有指明 project 是否位于 /home/userspace/Development/ 中。我假设是这样。

确保 project 中有一个名为 __init__.py 的文件(默认为空),app 中有另一个文件。

编辑:接下来要尝试的事情:在脚本目录中启动Python命令行并尝试以下操作:

import project
import project.app as app
import project.app.models as models
models.__dict__.keys()

它们都有效吗?如果是这样,最后一行的输出是什么?如果不是,哪个先死?

You don't indicate if project is located in /home/userspace/Development/. I'll assume that it is.

Make sure there's an (empty by default) file named __init__.py in project and another one in app.

EDIT: Next thing to try: Fire up the Python command line in the script's directory and try the following:

import project
import project.app as app
import project.app.models as models
models.__dict__.keys()

Do they all work? If so, what is the last line's output? If not, which dies first?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文