python打包导致导入错误

标签 python linux package importerror

当我尝试在 __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']

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

最佳答案

app/__init__.py 中可能存在问题,会以某种方式引发错误,可能是语法错误。这些错误稍后可能会作为导入错误被隐藏。

放一个

import pdb;pdb.set_trace()

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

关于python打包导致导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4572235/

相关文章:

python - 如何使用原始数据包在python3.5中嗅探Windows 10中的ospf数据包

linux - 如何在 Linux (Gnome) 中多路复用多个视频流

r - 安装 devtools 包的问题

java - Java中的包和它的子包是什​​么关系?

python - 如何扩展和修改 PyUnit

python - 如何防止matplotlib中左x轴延伸到右x轴?

python - 按复合类名搜索时 BeautifulSoup 返回空列表

linux - 为什么 strace -f 在 | 之后无法跟踪 child 的进度?

linux - 删除目录中的所有文件,除了列出的符合特定条件的文件

java - 如何使用 import javax.microedition.io.HttpConnection; Java 中的 SE 应用程序