Heroku does not really seem support Python and Django on Windows - python

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.

Related

No module named flask with waitress-serve and virtualenv (App engine flex)

I'm working on a project which used to be run on Linux for test. It's an App Engine flex project, run with gunicorn. Gunicorn does not work on Windows if I understood well, so I've been adviced to use waitress.
I also use virtualenv in my project.
So when I'm in my virtualenv, I run waitress-serve main:app (the gunicorn cmd was gunicorn -b :8080 main:app). I get an error: It had these arguments:
1. No module named flask.
I use flask. I can see the flask folder in my virtualenv folder. And when I run python then from flask import Flask I have no error.
Is there compat issue between waitress and virtualenv ? Or I'm doing something else wrong ? (already tried to delete virtualenv folder and install all the things again)
Python modules are case sensitive
Try Flask not flask.

How to start Gunicorn server from python script

I've built a Django project that works, even after I freeze it using Cx_Freeze and Py2exe.
Now I'm trying to set up the project for distribution, which requires a real webserver. I'm going for Gunicorn (will add Nginx once it works). I managed to run the Gunicorn server properly through the command line using :
gunicorn wsgi:application
However, I need to be able to run the server from my Python script, as the server is ment to be localhost. Gunicorn used to be shipped with a command 'run_gunicorn' designed for Django, but this command is now deprecated.
I tried understanding the following method :
How to use Flask-Script and Gunicorn
But I can't figure out how to make it work with Django.
The following doesn't work:
from django.core.wsgi import get_wsgi_application
from gunicorn.app.base import Application
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
application = get_wsgi_application()
Application().run(application)
Could someone tell me how to start the gunicorn server from my Python script ?

Run Falcon APP with uwsgi

I just started to learn Falcon (http://falcon.readthedocs.org/en/latest/user/quickstart.html)
but it need a web server running and docs suggesting use uwsgi or gunicorn.
though they have mentioned that how to use it with gunicorn
$ pip install gunicorn #install
$ gunicorn things:app #and run app through gunicorn.
But I want to run this sample app with uwsgi. but I have no clue how to.
I have installed it pip install uwsgi also gevent as suggested here http://falcon.readthedocs.org/en/latest/user/install.html
but what now. somebody guide me.
You'll probably find your answer on the uWSGI documentation site, specifically try this page:
http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html
I've never used Falcon or uWSGI, but it looks like you can probably get away with:
uwsgi --wsgi-file things.py --callable app
You can use uwsgi.ini to store configuration and easy run. Good way to setup uwsgi as services. Configuration with virtualenv
[uwsgi]
http = :8000
chdir = /home/user/www/uwsgi-ini
virtualenv = /home/user/www/uwsgi-ini/venv/
wsgi-file = main.py
callable = app
processes = 4
threads = 2
stats = 127.0.0.1:9191
and run in app folder:
uwsgi uwsgi.ini
command with virtualenv
uwsgi --http :8000 --wsgi-file main.py --callable app -H $(pwd)/venv/

Heroku app crashed after pushing small change. Very confused

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).

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

Categories

Resources