Forums

Can not deploy flask app

Hi,

I am new to pythonanywhere and I have a problem with deploying my flask app. error log:

2018-08-27 08:31:14,236: Error running WSGI application
2018-08-27 08:31:14,242: ImportError: cannot import name 'db'
2018-08-27 08:31:14,243:   File "/var/www/lukaszgg_pythonanywhere_com_wsgi.py", line 16, in <module>
2018-08-27 08:31:14,243:     from todo_app import app as application  # noqa
2018-08-27 08:31:14,243: 
2018-08-27 08:31:14,243:   File "/home/Lukaszgg/todo_list/todo_app.py", line 3, in <module>
2018-08-27 08:31:14,243:     import views
2018-08-27 08:31:14,243: 
2018-08-27 08:31:14,244:   File "/home/Lukaszgg/todo_list/views.py", line 1, in <module>
2018-08-27 08:31:14,244:     import models
2018-08-27 08:31:14,244: 
2018-08-27 08:31:14,244:   File "/home/Lukaszgg/todo_list/models.py", line 1, in <module>
2018-08-27 08:31:14,244:     from todo_app import db
2018-08-27 08:31:14,244: ***************************************************
2018-08-27 08:31:14,245: If you're seeing an import error and don't know why,
2018-08-27 08:31:14,245: we have a dedicated help page to help you debug: 
2018-08-27 08:31:14,245: https://help.pythonanywhere.com/pages/DebuggingImportError/
2018-08-27 08:31:14,245: ***************************************************

db var is defined in todo_app. My lukaszgg_pythonanywhere_com_wsgi.py:

import sys

# add your project directory to the sys.path
project_home = u'/home/Lukaszgg/todo_list/'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
from todo_app import app as application  # noqa

if I try to run in console:

python todo_app.py

from /home/Lukaszgg/todo_list/directory there is no import error. Also, locally is everything ok. But if I try to run in console:

python  lukaszgg_pythonanywhere_com_wsgi.py

from /var/www/ directory above error is occurred. I tried different python versions configured on Web tab with and without virtual env. Please help me with it.

if your database is on the PythonAnywhere disk, make sure you have it as a full (absolute) path instead of a relative path.

I define db this way in todo_app.py and tododb.sqlite3 is in the same directory as todo_app.py:

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///tododb.sqlite3'
app.config['SECRET_KEY'] = "random string"
db = SQLAlchemy(app)

Project structure:

11:53 ~/todo_list (master)$ tree
.
├── __init__.py
├── __pycache__
   ├── flask_app.cpython-36.pyc
   ├── forms.cpython-36.pyc
   ├── models.cpython-34.pyc
   ├── models.cpython-35.pyc
   ├── models.cpython-36.pyc
   ├── todo_app.cpython-34.pyc
   ├── todo_app.cpython-35.pyc
   ├── todo_app.cpython-36.pyc
   ├── views.cpython-34.pyc
   ├── views.cpython-35.pyc
   └── views.cpython-36.pyc
├── flask_app.py
├── forms.py
├── models.py
├── static
   ├── favicon.ico
   └── w3.css
├── templates
   ├── add.html
   ├── edit.html
   ├── index.html
   └── login.html
├── todo_app.py
├── tododb.sqlite3
└── views.py
3 directories, 24 files

Yes. Try using a full path instead of a relative path like what you are doing right now.