Heroku app crashed after pushing small change. Very confused - python

I am using Heroku with python and Flask. My app was working fine until I updated a few lines in my python application file. The app runs fine locally, but I now have the following error when I try to access my app:
"An error occurred in the application and your page could not be served. Please try again in a few moments.
If you are the application owner, check your logs for details."
My logs look something like this:
2012-10-03T17:40:26+00:00 heroku[web.1]: Process exited with status 1
2012-10-03T17:40:26+00:00 heroku[web.1]: State changed from starting to crashed
2012-10-03T17:51:25+00:00 heroku[web.1]: State changed from crashed to starting
2012-10-03T17:51:26+00:00 heroku[web.1]: Starting process with command `python presentation.py`
2012-10-03T17:51:26+00:00 app[web.1]: ImportError: No module named site
I am also no longer able to run python through heroku:
Cinnas-MacBook-Pro:infinite-fortress-4866 cinna$ heroku run python
Running `python` attached to terminal... up, run.1
ImportError: No module named site
The next thing I have tried to do is check my environment variables:
Cinnas-MacBook-Pro:infinite-fortress-4866 cinna$ heroku config
=== infinite-fortress-4866 Config Vars
LANG: en_US.UTF-8
LD_LIBRARY_PATH: /app/.heroku/vendor/lib
LIBRARY_PATH: /app/.heroku/vendor/lib
PATH: /app/.heroku/venv/bin:/bin:/usr/local/bin:/usr/bin
PYTHONHASHSEED: random
PYTHONHOME: /app/.heroku/venv/
PYTHONPATH: /app/
PYTHONUNBUFFERED: true
However, when I try to look inside the library directories, I get something like this:
Cinnas-MacBook-Pro:infinite-fortress-4866 cinna$ heroku run ls /app/.heroku/vendor/lib
Running `ls /app/.heroku/vendor/lib` attached to terminal... up, run.1
ls: cannot access /app/.heroku/vendor/lib: No such file or directory
I am not sure where to proceed at this moment. I miss my app, please help!
Additional information:
The problems all started when I added the following lines to my app.py code:
#app.route('/my_fb_graph',methods=['GET','POST'])
def my_fb_graph():
return render_template('my_fb_graph.html')
When I pushed the code and the app no longer worked. I then removed these lines of code, pushed the code again, and still got the same errors. The next thing I did was to completely remove the app.py file and try to a small test code which still did not work.
The root of the problem seems to be the error:
2012-10-03T17:51:26+00:00 app[web.1]: ImportError: No module named site

I was able to fix the problem, but still dont know why it occurred in the first place!
After a lot of experimentation, I ended up setting up a completely new app on Heroku. I checked the environment variables in the new app and got the following:
Cinnas-MacBook-Pro:thawing-temple-4323 cinna$ heroku config
=== thawing-temple-4323 Config Vars
FACEBOOK_APP_ID: ***
FACEBOOK_SECRET: ***
PATH: bin:/usr/local/bin:/usr/bin:/bin
PYTHONUNBUFFERED: true
Checking my original app (the broken one), I realized that new environment variables were somehow added in my last push as indicated by my logs:
2012-10-04T04:20:04+00:00 heroku[api]: Add PYTHONUNBUFFERED, PYTHONPATH, PYTHONHOME, LANG, LD_LIBRARY_PATH, PATH, PYTHONHASHSEED, LIBRARY_PATH config by ***#***
and by checking my environment variables:
Cinnas-MacBook-Pro:infinite-fortress-4866 cinna$ heroku config
=== infinite-fortress-4866 Config Vars
LANG: en_US.UTF-8
LD_LIBRARY_PATH: /app/.heroku/vendor/lib
LIBRARY_PATH: /app/.heroku/vendor/lib
PATH: /app/.heroku/venv/bin:/bin:/usr/local/bin:/usr/bin
PYTHONHASHSEED: random
PYTHONHOME: /app/.heroku/venv/
PYTHONPATH: /app/
PYTHONUNBUFFERED: true
I removed these new variables with the command:
heroku config:remove PYTHONPATH PYTHONHOME LANG LD_LIBRARY_PATH PYTHONHASHSEED LIBRARY_PATH
and my app started to work again. I've been pushing more code, and this problem has not occurred again.
I am still really curious why/how these variables were added in the first place since all I did was do a git push.

I experienced a very similar problem yesterday (6th Dec. 2012). Out of the blue, every python invocation died with 'ImportError: No module named site'. Heroku support got back to me today and they say it's fixed on their end, so the following workaround shouldn't be required. I'll leave this here in case it helps someone else diagnose.
I checked my heroku config vars though, and there were no PYTHON* variables set. They were set as env vars at the shell level though:
$ heroku run set | grep PYTHON
PYTHONHASHSEED=random
PYTHONHOME=/app/.heroku/venv/
PYTHONPATH=/app/
PYTHONUNBUFFERED=true
/app/.heroku/venv was a non-existent directory. If I overrode PYTHONHOME with a config var, and pointed to where my virtualenv actually was, it all started working again:
$ heroku config:set PYTHONHOME=/app
/app appears to be a mount point for the project root directory. Digging through the history of the Python buildpack, it looks like when I started my project, everyone made their virtualenvs in the project root. Now new projects make virtualenvs in a venv/ subdirectory. Support said they were gradually rolling out a buildpack change, and I guess the checks for the old way of doing things didn't kick in for me.
Here's where to look for the buildpack internals:
https://github.com/heroku/heroku-buildpack-python/blob/master/bin/compile

This bit me, too. I'm not sure when it started. I don't believe it was in response to any change on my part, I just noticed Application Error on my site today, and found this in the logs:
2012-12-12T16:02:06+00:00 heroku[web.1]: State changed from crashed to starting
2012-12-12T16:02:09+00:00 heroku[web.1]: Starting process with command `aspen --network_address=:40856 --www_root=doc/ --project_root=doc/.aspen`
2012-12-12T16:02:10+00:00 app[web.1]: ImportError: No module named site
2012-12-12T16:02:11+00:00 heroku[web.1]: Process exited with status 1
2012-12-12T16:02:11+00:00 heroku[web.1]: State changed from starting to crashed
I had another release ready to go, so I just deployed as usual. After a git push heroku, the site is back up. My heroku config doesn't have the extra envvars listed above:
$ heroku config
=== aspen-io Config Vars
ASPEN_IO_SHOW_GA: yes
PATH: bin:/usr/local/bin:/usr/bin:/bin
PYTHONUNBUFFERED: true
Update: #kennethreitz pointed me to this:
Cannot import module site
This occurs when the configured environment variables don't match the
paths of the installed Python. When this occurs, it is because someone
purged an app's cache without understanding the above implications.
To fix, either purge the cache and update the configuration, or
restore the expected configurations (preferred).

Related

Python: "Cannot import flask"

I tried everything but couldn't import app.py. It gives me the following error
set FLASK_APP = "C:\Users\Hp\PycharmProjects\sakshi.py\app.py"
(env) C:\Users\Hp\Documents\flask_app>flask run
Serving Flask app " app.py"
Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
Debug mode: off
Usage: flask run [OPTIONS]
Error: Could not import " app".
Your help would be much appreciated!
Change the sever environment to development env.
export FLASK_APP=yourapp
export FLASK_ENV=development
and then start the server with:
flask run
If you still get error try this way:
set FLASK_ENV=development
flask run
And then run your python file.
At least one problem: spaces in the set command. From the syntax in the microsoft doc:
set [<Variable>=[<String>]]
set [/p] <Variable>=[<PromptString>]
set /a <Variable>=<Expression>
There is no space on either side of the = in the set command.
And, the path name "C:\Users\Hp\PycharmProjects\sakshi.py\app.py" is questionable. It is, at the very least, a bad idea to name a directory sakshi.py.
I could see the problem is with space between the variable and value in set command,
In windows,
set var=value
In *nix family,
export var=value
Your script name is app.py which by default flask recognizes as entry point for the app.
You only have to explicitly set the FLASK_APP variable when you are running from another directory (which you are) or the name of the script is something other than app.py. You can directly run the flask run command from the project base directory itself.

Heroku does not really seem support Python and Django on Windows

Heroku seems to have a dependency on Gunicorn when it comes to Python / Django apps. Gunicorn is not supported on Windows. Has anybody had success or know of a work around?
My app runs fine but not under Heroku or Heroku local
Error:
...site-packages\gunicorn\util.py", line 9, in <module>
import fcntl
ModuleNotFoundError: No module named 'fcntl'
Exited with exit code null
It seems unfair to blame Heroku for this. Gunicorn doesn't support Windows. Heroku has nothing to do with Windows.
There are other WSGI web servers that may work. For example, uWSGI has documentation for running on Heroku.
A quick summary:
Make sure that uwsgi and werkzeug are in your requirements.txt or Pipfile / Pipfile.lock and that these files are tracked by Git
Create and track a uwsgi.ini file containing something like
[uwsgi]
http-socket = :$(PORT)
master = true
processes = 4
die-on-term = true
module = werkzeug.testapp:test_app
memory-report = true
making sure to set the module appropriately for your application.
Update your Procfile to contain
web: uwsgi uwsgi.ini
Make sure it works with heroku local, then push to Heroku.

Heroku not reading requirement.txt file

I am trying to deploy my python flask app to heroku but it keeps crashing and complaining about bash: gunicorn: command not found. I have my requirement.txt file in the root folder where my Procfile is also located. My python code is located in src/server/
Procfile contains: web: gunicorn --pythonpath src/server/ route:app --preload
I have gunicorn in my requirements file:
Is there something i'm missing?
gunicorn==19.8.1
Flask==0.12.2
Flask-Cache==0.13.1
Flask-Cors==3.0.2
Flask-MongoAlchemy==0.5.1
flask-mongoengine==0.9.5
Flask-PyMongo==0.5.2
Flask-WTF==0.14.2
gevent==1.2.1
greenlet==0.4.12
pymongo==3.6.1
folder structure;
I faced a similar problem with my app. I just reset all my requirements by sending in an empty requirements.txt file. Then built my app. Then sent the original file. I don't understand why, but it worked.
I am also having a similar problem like this on Heroku. I created an IDENTICAL new project and magically it works. Heroku has some bug related to ignoring requirements.txt
I fixed it by creating a new environment in Heroku. The previous environment seems not to be reading the requirement.txt file, I couldn't figure why.

Heroku: ImportError: No module named site

After some changes in my repo and deploy to heroku I am receiving the following error:
ImportError: No module named site
I not have idea what can cause the problem because I only change some Django templates in the last 2 commits.
Best Regards
Take a look at your Procfile. It should show something like this:
web: gunicorn site:app
Make sure site is the name of your app.
Heroku has an article for this: https://help.heroku.com/BWJ7QYTF/why-am-i-seeing-importerror-no-module-named-site-when-deploying-a-python-app
Updates to the Python buildpack mean that the PYTHONPATH and PYTHONHOME config vars being set on the app may introduce this issue.
Firstly, check if these are present with
heroku config
To fix the issue, you can unset them like so:
heroku config:unset PYTHONHOME -a appname
heroku config:unset PYTHONPATH -a appname
Add the following to your proc file
web: gunicorn site:app
Also check your gitignore and make sure site isnt included

Django Deploy using Heroku - [Errno 2] No such file or directory

I get this error ([Errno 2] No such file or directory) after I push the repo to heroku master. Here are my logs.
2012-04-17T18:24:53+00:00 app[web.1]: python: can't open file '/test/project/manage.py': [Errno 2] No such file or directory
2012-04-17T18:24:54+00:00 heroku[web.1]: Process exited with status 2
2012-04-17T18:24:54+00:00 heroku[web.1]: State changed from starting to crashed
2012-04-17T18:24:54+00:00 heroku[web.1]: State changed from crashed to created
2012-04-17T18:24:54+00:00 heroku[web.1]: State changed from created to starting
2012-04-17T18:24:57+00:00 heroku[web.1]: Starting process with command python /test/project/manage.py runserver 0.0.0.0:4473 --noreload 2012-04-17T18:24:57+00:00 app[web.1]: python: can't open file '/test/project/manage.py': [Errno 2] No such file or directory
My Procfile looks like the following:
web: python /test/project/manage.py runserver 0.0.0.0:$PORT --noreload
I don't know why it can't open the file. It opens fine when I am using my development server. Any ideas? Thanks for reading.
Your current setup in your Procfile references an absolute path '/test/project/manage.py' that doesn't exist on Heroku. The '/test/ is the root of the instance you're running in and is incorrect. You should first change this to be the relative path, this is likely something like:
web: python project/manage.py runserver 0.0.0.0:$PORT --noreload
If this does not work you can explore the location of the project by running:
heroku run bash
This should place you in '/app' from here you can see what the path to start your project is.
Since your initial push likely failed to start the process you'll likely need to scale a web process. You can then do this with:
heroku scale web=1
you can attach an ls to heroku to find out the actual structure of the file system.
> heroku run ls /
Running ls / attached to terminal... up, run.1
app dev home lib64 mnt sbin usr
bin etc lib lost+found proc tmp var
It might be the case that they wrap your app inside an app directory
Try using os.path to join the elements of the path., btw have you try it to change the ProcFile to read from worker? python hellodjango/manage.py
Edit later:
Try to run this three commands in order to make heroku master:
pip install -r ./requirements.txt
foreman start
heroku create mempy-demo --stac=cedar
git push heroku master
Now test a simple 'Hello World':
$ curl mempy-demo.herokuapp.com
You could do something like this to change the environment variables (e.g add it in your wsgi.py file if that's where the error originates):
os.environ["DJANGO_SETTINGS_MODULE"] = "myblog.settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myblog.settings")

Categories

Resources