Reputation: 11159
I am getting an annoying import error when I try to import a variable in an __init__.py
file. I have attached the files involved and my directory structure:
#/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
What might be the problem?
Some playing around with pdb_trace() placed right before the import:
(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']
no db in app :)
Upvotes: 2
Views: 1031
Reputation: 1220
I was seeing "ImportError: No module named" because my modules didn't have execute permissions.
Upvotes: 0
Reputation:
From the stacktrace it seems the following is going on:
so, looks like a cyclic import?
Upvotes: 0
Reputation: 20641
"app/app" is asking for trouble, it both directories are in the search path.
Upvotes: 0
Reputation: 43024
This is most often caused by Python finding a package or module with the same name that is different than the one you think it is. I can also happen when you try to run it from withing the package space. First try to change default directory to home, and try again. If it still fails, try adding to the top of your modules:
from __future__ import absolute_import
That prevents the old default behavior of using relative imports from the package space.
Also, in the debugger session, print out the __file__
attribute and check that it is what you expect.
Upvotes: 0
Reputation: 172179
There is likely a problem in the app/__init__.py
, that raises an error there, somehow, perhaps a syntax error. These errors have a tendency to be hidden as import errors later.
Put a
import pdb;pdb.set_trace()
At the beginning of the module that it tries to import. You can then step through that module to see what the real error is.
Upvotes: 4