2

I have been banging my head against the wall trying to get this Flask web app running , which is really frustrating because it is only a preliminary to make sure all the modules and such are working correctly to be able to run a Flask app using SQLAlchemy.

I am running: Windows 10, python 3.6(latest) and latest pip install of SQLalchemy and flask

I am able to connect to the Heroku database that is listed under DATABASE_URI by using the CMD command: " psql [database_location] "

Here is the code I am running:

import os

from flask import Flask, session
from flask_session import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

app = Flask(__name__)
app.config['DATABASE_URL'] = "postgres://jqnlclvhxpacdh:edc91a01d4ac2 ETC ETC ETC"
# Check for environment variable
if not os.getenv("DATABASE_URL"):
    raise RuntimeError("DATABASE_URL is not set")

# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)

# Set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))


@app.route("/")
def index():
    return "Project 1: TODO"
#

and here is the dump that python takes all over my screen when I try to run:

set FLASK_APP=application.py 

(I made sure I am in the correct directory)

#
Traceback (most recent call last):
  File "c:\program files\python36\lib\site-packages\flask\cli.py", line 330, in __call__
    rv = self._load_unlocked()
  File "c:\program files\python36\lib\site-packages\flask\cli.py", line 317, in _load_unlocked
    self._app = rv = self.loader()
  File "c:\program files\python36\lib\site-packages\flask\cli.py", line 372, in load_app
    app = locate_app(self, import_name, name)
  File "c:\program files\python36\lib\site-packages\flask\cli.py", line 242, in locate_app
    '\n\n{tb}'.format(name=module_name, tb=traceback.format_exc())
flask.cli.NoAppException: While importing "application", an ImportError was raised:

Traceback (most recent call last):
  File "c:\program files\python36\lib\site-packages\flask\cli.py", line 235, in locate_app
    __import__(module_name)
  File "C:\Program Files\Python36\learningPython\web_CS50\project1\application.py", line 4, in <module>
    from flask_session import Session
  File "c:\program files\python36\lib\site-packages\flask_session\__init__.py", line 16, in <module>
    from .sessions import NullSessionInterface, RedisSessionInterface, \
  File "c:\program files\python36\lib\site-packages\flask_session\sessions.py", line 23, in <module>
    from itsdangerous import Signer, BadSignature, want_bytes
ImportError: cannot import name 'want_bytes'

#

I have tried running this by doing the setting the FLASK_APP[DATABASE_URL] in the command line to no avail, and have spent several hours on stackoverflow and here looking for some fix to this issue. I have reinstalled my python installation from anaconda to the latest python 3.6 and reinstalled all of the pip packages to no avail. Any advice on this point?

4
  • It's difficult to tell. Have you taken care of all Heroku deployment requirements? – pankaj Oct 19 '18 at 10:32
  • And try to paste the dump in using braces to prettify code next time. It looks nice. – pankaj Oct 19 '18 at 10:33
  • I can access the Heroku database using the key I was given when I signed up for Heroku, using the CLI in postgresql, so I think it has to do with the python code on my end. – lkilgoretrout Oct 19 '18 at 21:21
  • I was able to somewhat fix the problem. On stackoverflow someone suggested that I use an older version of the itsdangerous module because the new 1.0.0 has problems doing top level public imports (or something). I installed the 0.24 version and the the import problem went away, now I am having trouble with setting the DATABASE_URL in the CLI (or anywhere for that matter) – lkilgoretrout Oct 20 '18 at 22:28
3

Try to delete the following code in line 17.

Session(app)

At least it works for me in Mac.

3
  • worked for me too! – Afonso Lopez Apr 22 '20 at 16:13
  • worked for me too! – Arvind GK May 9 '20 at 11:03
  • It worked for me too but wouldn't it compromise the project in terms of creating user sessions? I'm having the same problem and this is the closest to a fix I've been able to find – DaryllCS May 12 '20 at 6:05
0

Try define the path of file while setting FLASK_APP : "set FLASK_APP=F:/path/application.py. Its working for me!!

2
  • This doesn't work for me. – Hoi Fei Kwok Mar 17 '20 at 9:17
  • It worked after I changed to use windows powershell (from ubuntu) for some reasons. – Hoi Fei Kwok Mar 17 '20 at 9:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .