Heroku cannot import name 'Session' from 'flask_session'

115 Views Asked by At

Attempting to deploy my website with heroku is leading to this error. My website works fine in flask, so it must just be some cli issue or something to do with my virtual environment. I'm not really an expert in that stuff, so I don't know how to solve the issue.

Here is the error log.

2023-07-24T02:31:34.494523+00:00 app[web.1]: ImportError: cannot import name 'Session' from 'flask_session' (unknown location)
2023-07-24T02:31:34.494654+00:00 app[web.1]: [2023-07-24 02:31:34 +0000] [8] [INFO] Worker exiting (pid: 8)
2023-07-24T02:31:34.526827+00:00 app[web.1]: [2023-07-24 02:31:34 +0000] [2] [ERROR] Worker (pid:7) exited with code 3
2023-07-24T02:31:34.528740+00:00 app[web.1]: [2023-07-24 02:31:34 +0000] [2] [ERROR] Worker (pid:8) was sent SIGTERM!
2023-07-24T02:31:34.627270+00:00 app[web.1]: [2023-07-24 02:31:34 +0000] [2] [ERROR] Shutting down: Master
2023-07-24T02:31:34.627284+00:00 app[web.1]: [2023-07-24 02:31:34 +0000] [2] [ERROR] Reason: Worker failed to boot.
2023-07-24T02:31:35.098046+00:00 heroku[web.1]: Process exited with status 3
2023-07-24T02:31:35.130639+00:00 heroku[web.1]: State changed from starting to crashed
2023-07-24T02:31:48.794602+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=flasker888-950c07d40a01.herokuapp.com request_id=7d5cb524-3382-4408-b53d-992e3382cb82 fwd="114.181.123.229" dyno= connect= service= status=503 bytes= protocol=https
2023-07-24T02:31:48.979619+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=flasker888-950c07d40a01.herokuapp.com request_id=a74969fb-4dbf-44e0-be3d-a21dd1c2d064 fwd="114.181.123.229" dyno= connect= service= status=503 bytes= protocol=https

As you can see, the error occurs on the index page,(path="/") and the favicon for some reason? But anyways, it's not able to import Session from flask_session and I'm not sure why. I know this question has been asked before, but the given answer was not helpful and I still don't know how to fix this issue. I'm really a beginner at using virtual environments so any help on fixing this would be greatly appreciated.

EDIT: The issue seems to be with the requirements page missing the necessary information. I used pip freeze to get this up and now flask session is working but for some reason it won't recognize flask bcrypt. I don't know any other way to install packages other than pip freeze so I'm stuck with this page and heroku can't deploy it until I add the missing packages.

blinker==1.6.2
cachelib==0.10.2
click==8.1.3
colorama==0.4.6
Flask==2.3.2
Flask-Login==0.6.2
Flask-Session==0.5.0
Flask-SQLAlchemy==3.0.5
greenlet==2.0.2
gunicorn==21.1.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
numpy==1.25.1
packaging==23.1
pandas==2.0.3
psycopg2==2.9.6
python-dateutil==2.8.2
pytz==2023.3
six==1.16.0
SQLAlchemy==2.0.17
typing_extensions==4.7.1
tzdata==2023.3
Werkzeug==2.3.6

2

There are 2 best solutions below

2
Anay On

Make sure you have installed and correctly importing flask_session.

If the issue persists when deploying it on Heroku, you can log into your Heroku instance and run the Python shell command heroku run python. Try importing Session from flask_session within the Python shell to see if the error occurs

1
Dragynjump On

I still don't know how to install packages when pip freeze doesn't work, but I just manually put the names of the missing packages into the requirements.txt file. After that I had an issue with sqlalchemy that was solved by changing postgres to postgresql. My website is now up and running.