0

I encounter a weird import error when I try to deploy an app built on flask, sqlalchemy on a server. The app works well when I run it on my localhost. However, it raises an import error when I run the app on a server. I suspect the cause of my problem is a circular import. But it's really weird that it works on a local environment but fails on a server environment.

I have file structure looks like the following:

.
├── __main__.py
├── instance
│   ├── config.cfg
├── theapp
│   ├── __init__.py
│   ├── models.py
│   └── views.py

init.py

# File for initializing the application and set up global variables(app, db)


...
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
CONFIG_FILE = os.path.join(app.instance_path, 'config.cfg')

# Instantiate the application and initializes the login manager.
def create_app(app, config):
    app.config.update(SECRET_KEY=config['key']['SECRET_KEY'], SQLALCHEMY_DATABASE_URI=config['app']['SQLALCHEMY_DATABASE_URI'],
                      DEBUG=config['app']['DEBUG'])

# Read configuration
config = ConfigParser()
config.read(CONFIG_FILE)
create_app(app, config)
db = SQLAlchemy(app)

# These imports are useful. They help to avoid cyclic import
from lemur import views, models

views.py

from theapp import (app, db)
...

models.py

from theapp import db
...

main.py

from theapp import app
if __name__ == '__main__':
    app.run()

The error is(in views.py):

cannot import name models

I have tried a method suggested here but it doesn't work.

2
  • Can you post your $PYTHONPATH? Commented Aug 14, 2016 at 22:20
  • @rfkortekaas when I ` echo $PYTHONPATH `, I get nothing. Commented Aug 15, 2016 at 15:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.