Flask with Heroku, Import Error: No module named Flask - python

So I'm trying to create an app with Flask and Heroku. I can run it with Foreman just fine, but after deploying to Heroku, the application error comes up and the heroku logs show:
heroku[web.1]: State changed from crashed to starting
heroku[web.1]: Starting process with command `python app.py`
app[web.1]: File "app.py", line 2, in <module>
app[web.1]: from flask import Flask, send_from_directory
app[web.1]: ImportError: No module named flask
Any idea on how this could happen? Thanks!
EDIT: Flask is in the requirements file and I see that it gets installed during the push to Heroku.

You probably need to add Flask (and any other external dependencies) to a requirements.txt and include it in your repo.
You can use 'pip freeze > requirements.txt" to create it with what ever packages you have installed in your environment at the moment.

Related

No module named <module name>, deploying Worker container in Heroku

I'm trying to use Heroku to deploy a simple python script that I have written and dockerized. I also added a heroku.yml.
Since this is a python script that I would like to run daily and not an API or a website, I concluded that the script should be deployed in Heroku worker container and not as a web container.
The containerized python script works locally on my own PC, but on heroku, the logs say:
2022-02-25T06:09:13.899148+00:00 heroku[worker.1]: State changed from up to crashed
2022-02-25T06:09:13.930822+00:00 heroku[worker.1]: State changed from crashed to starting
2022-02-25T06:09:15.148500+00:00 heroku[worker.1]: Starting process with command `python -u main.py`
2022-02-25T06:09:15.790174+00:00 heroku[worker.1]: State changed from starting to up
2022-02-25T06:09:16.033414+00:00 app[worker.1]: Traceback (most recent call last):
2022-02-25T06:09:16.033431+00:00 app[worker.1]: File "/app/main.py", line 3, in <module>
2022-02-25T06:09:16.033533+00:00 app[worker.1]: from venmo_api import Client
2022-02-25T06:09:16.033552+00:00 app[worker.1]: ModuleNotFoundError: No module named 'venmo_api'
2022-02-25T06:09:16.155312+00:00 heroku[worker.1]: Process exited with status 1
2022-02-25T06:09:16.232899+00:00 heroku[worker.1]: State changed from up to crashed
I don't understand how the module isn't found when my Dockerfile says COPY requirements.txt and RUN pip install -r requirements.txt
For context, here is my Dockerfile:
FROM python:3.9.10-alpine3.15
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "-u", "main.py"]
, my requirements.txt
certifi==2021.10.8
charset-normalizer==2.0.11
idna==3.3
requests==2.27.1
urllib3==1.26.8
venmo-api==0.3.1
get-docker-secret==1.0.1
and my heroku.yml:
build:
docker:
worker: Dockerfile
All 3 of these files are in the same directory, and you can see venmo-api is in my requirements.txt.
Prior to deploying, I set the Heroku stack to container with: heroku stack:set container
I used heroku config:set [redacted] to set runtime environment variables like my API key
, and finally I ran heroku container:push worker and heroku container:release worker to build and push the python script image to Heroku's container registry and deploy it.
Can someone please tell me where I went wrong? I just want this script to be able to run on the cloud and be able to schedule it to run daily with Heroku Scheduler?
Thanks so much!!

Flask: Cannot Deploy App to Heroku, No Module named 'app'

I'm trying to deploy my app to Heroku. My folder structure is this:
All my code is written inside __init__.py, and I run this by just typing flask run while in the root directory. This works fine. However, I tried deploying this app to Heroku and I got the following error:
remote: -----> Discovering process types
remote:
remote: ! Push failed: cannot parse Procfile.
remote: ! Please try pushing again.
remote: ! If the problem persists, see https://help.heroku.com/ and provide Request ID 266c0fb8-18bd-a262-5d3a-4355cd83bb02.
remote:
remote:
remote: Verifying deploy...
remote:
remote: ! Push rejected to submission-form.
remote:
My Procfile contains this: web: gunicorn app:app and my app.py contains this:
from app import app
app.run(debug=True)
If I try running python app.py, my modules are reported missing:
Traceback (most recent call last):
File "app.py", line 1, in <module>
from app.app import app
File "D:\Gatsby\submission\flask-tailwindcss-starter\app\__init__.py", line 4, in <module>
from contentful_management import Client
ModuleNotFoundError: No module named 'contentful_management'
This is despite them being there and running perfectly when I use flask run. What is going wrong here? How do I fix this so I can push this app to deploy on Heroku? I'm guessing this has something to do with my procfile.
Edit: My repo is attached here, if it helps
You are calling app from app.app module. First there is no module app.app and there is no app defined.
try this :
from app import create_app
app = create_app()
app.run(debug=True)

ModuleNotFoundError: No module named 'fcntl' when I try to deploy my django project on heroku

I try to deploy my django project on heroku, and follow commands below
heroku login
git init
git add .
git commit -m "first commit"
heroku create
heroku git:remote -a name
pip install gunicorn
gunicorn application.wsgi
when it comes to the latest command, error occurs:
ModuleNotFoundError: No module named 'fcntl'
How can I solve it?
Is the underlying operating system Windows? fcntl is not available on Windows system and Gunicorn does not work on windows
Run a WSGI web app (like Django) on Windows uing Waitress
Basically all you have to do is replace the gunicorn call with:
waitress-serve --listen=*:8000 myapp.wsgi:application
For typical apps this will give you the same result as running gunicorn. :) Good luck!
The fcntl module is not available on Windows. you should use waitress for http and for https use django-sslserver.

My discord.py bot hosted on heroku does not work

I hosted my discord.py bot on heroku, Build Succeeded, and Deployed.
The code works on my VSC but not on heroku.
requirements.txt :
discord.py==1.3.4
PyNaCl==1.3.0
pandas
dnspython==1.16.0
async-timeout==3.0.1
Procfile :
worker: python bot.py
I don't know what is the problem.
Can please anyone tell me what to revise or what's wrong?
I appreciate your help.
I did :
$ heroku logs --tail
and I got :
2020-07-30T03:30:00.853064+00:00 app[worker.1]: File "bot.py", line 3, in <module>
2020-07-30T03:30:00.853284+00:00 app[worker.1]: import requests
2020-07-30T03:30:00.853316+00:00 app[worker.1]: ModuleNotFoundError: No module named 'requests'
2020-07-30T03:30:00.964329+00:00 heroku[worker.1]: Process exited with status 1
2020-07-30T03:30:01.011352+00:00 heroku[worker.1]: State changed from up to crashed
Sorry for the fool question.
To fix your issue it sounds like you need to add the requests module to your requirements.txt. To obtain this you can run the following command in your console:
pip show requests
Make a note of the version number and append the following to your requirements.txt file:
requests==x.y.z
Where x.y.z is replaced with the version you obtained from using pip show requests.
The general process of hosting a bot on Heroku:
If you want to host your bot on Heroku the steps I take are:
requirements.txt
You can obtain this by running the following command in your console:
pip freeze > requirements.txt
This will write all the required Python modules into the file for you with their respective versions. (This should also find and add your missing "requests" module)
Procfile
Inside this file, you will only need:
worker: python bot.py
"python bot.py" is what will be run on Heroku to start your bot. Although you have done this fine.
Heroku Resources
On the Heroku website access the dashboard for your app. Then select the "Resources" tab.
Next, there should be the contents of your Procfile. Click on the pen on the right hand side, enable the switch to allow Heroku to start the worker.
If you have any issues with hosting your bot, please reply to my suggestion and I'll be happy to help.

Deploying a python flask web application on Heroku with Windows

I am trying to deploy a flask app I made to Heroku with success.
The app is generated but I get errors when I push the code to the Heroku repository.
My flask app is inside a module called server.py and the variable is named app.
At first I tried using gunicorn and writing
web: gunicorn server:app
and deplying but no web dynos were up and I get an error stating it is the Procfile file.
Red about it about and saw that Gunicorn is not really working on windows so I tried installing Waitress and deploying without success. this time my profcile was written as all of these (tried several times):
web: waitress-serve --listen=*:8000 server.wsgi:application
web: waitress-serve --listen=*:8000 app.wsgi:application
And so on.
to add a web dyno I should scale it because heroku ps: showes that there is no dynos.
When I try to run heroku ps:scale web=1 I get:
Scaling dynos... !
▸ Couldn't find that process type.
What am i doing wrong?
I was having the same problem. Particularly, waitress works locally in Windows (inside a Procfile.windows file web: waitress-serve index:server, then with heroku CLI heroku local -f Procfile.windows), but failed after Heroku deployment. Workaround for me was to locally test with waitress (like explained), but deploy with gunicorn (web: gunicorn index:server inside Procfile). Let me know if this works for you.

Categories