2

当我尝试在__init__.py文件中导入变量时,我遇到了一个烦人的导入错误。我附上了所涉及的文件和我的目录结构:

#/home/me/app/app/__init__.py
from flaskext.sqlalchemy import SQLAlchemy

db = SQLAlchemy(app)

#/home/me/app/app/models/__init__.py
from datetime import datetime
from app import db

#shell
[me@archlinux app]$ pwd
/home/me/app
[me@archlinux app]$ ./manage.py 
/home/me/app/app/__init__.pyc
Traceback (most recent call last):
  File "./manage.py", line 7, in <module>
    from app import app
  File "/home/me/app/app/__init__.py", line 3, in <module>
    from app.views.post import post
  File "/home/me/app/app/views/post.py", line 4, in <module>
    from app.models import Post
  File "/home/me/app/app/models/__init__.py", line 5, in <module>
    from app import db
ImportError: cannot import name db

[me@archlinux app]$ tree 
.
├── apikey.txt
├── manage.py
├── app
│   ├── forms
│   │   ├── __init__.py
│   │   └── __init__.py~
│   ├── __init__.py
│   ├── __init__.py~
│   ├── __init__.pyc
│   ├── models
│   │   ├── __init__.py
│   │   ├── __init__.py~
│   │   └── __init__.pyc
│   ├── static
│   │   ├── css
│   │   │   └── style.css
│   │   ├── images
│   │   │   ├── favicon.png
│   │   │   ├── logo.png
│   │   │   ├── text_logo.png
│   │   │   ├── thumb_down_active.png
│   │   │   ├── thumb_down_inactive.png
│   │   │   ├── thumb_up_active.png
│   │   │   └── thumb_up_inactive.png
│   │   ├── js
│   │   │   └── index.js
│   │   └── sitemap.xml
│   ├── templates
│   │   ├── 404.html
│   │   ├── 500.html
│   │   ├── about.html
│   │   ├── base.html
│   │   ├── feedback
│   │   │   └── feedback_form.html
│   │   ├── form.html
│   │   ├── posts
│   │   │   ├── comment.html
│   │   │   ├── post.html
│   │   │   └── posts.html
│   │   ├── spam.html
│   │   ├── terms.html
│   │   └── users
│   │       ├── login_form.html
│   │       └── sign_up_form.html
│   ├── util
│   │   ├── forms.py
│   │   ├── honeypot.py
│   │   ├── __init__.py
│   │   ├── __init__.py~
│   │   ├── json_http.py
│   │   ├── models.py
│   │   └── spam.py
│   └── views
│       ├── feedback.py
│       ├── __init__.py
│       ├── __init__.pyc
│       ├── post.py
│       ├── post.pyc
│       └── user.py
├── settings.py
├── settings.pyc
└── TiddlyWiki.html

13 directories, 49 files

可能是什么问题?

一些在导入之前放置的 pdb_trace() 玩弄:

(Pdb) import app
(Pdb) app
<module 'app' from '/home/ramin/app/app/__init__.pyc'>
(Pdb) dir(app)
['Flask', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'views']

应用程序中没有数据库 :)

4

5 回答 5

4

中可能存在问题app/__init__.py,从而引发错误,不知何故,可能是语法错误。这些错误往往会在以后隐藏为导入错误。

放一个

import pdb;pdb.set_trace()

在它尝试导入的模块的开头。然后,您可以单步执行该模块以查看真正的错误是什么。

于 2010-12-31T20:51:58.210 回答
0

这通常是由于 Python 发现一个与您认为的名称不同的同名包或模块引起的。当您尝试从包空间中运行它时,我也会发生这种情况。首先尝试将默认目录更改为主目录,然后重试。如果仍然失败,请尝试添加到模块的顶部:

from __future__ import absolute_import

这可以防止使用来自包空间的相对导入的旧默认行为。

此外,在调试器会话中,打印出__file__属性并检查它是否符合您的预期。

于 2010-12-31T21:27:49.700 回答
0

“app/app”是自找麻烦,这两个目录都在搜索路径中。

于 2010-12-31T21:49:23.630 回答
0

从堆栈跟踪看来,以下情况正在发生:

  • 在您导入views.post的应用程序内
  • 这从模型中导入 Post
  • 模型从应用程序导入数据库
  • 这将导入应用程序,这将导入视图...

那么,看起来像循环导入?

于 2011-12-06T09:34:41.213 回答
0

我看到“ImportError: No module named”是因为我的模块没有执行权限。

于 2014-02-06T14:51:11.707 回答