unable to deploy the python pyramid web apps to heroku - python

This is the Error logs I find in the heroku when deploying the python pyramid application. I have followed the each and every steps the python pyramid documentation has. Where have I exactly missed not able to figure out.
I doubt if my way of creating run file is incorrect. I have created a run.py
file and added the following code into it.
#!/bin/bash
set -e
python setup.py develop
python runapp.py

You cannot host a web app like that. You need a proper server, for example gunicorn - see the Pyramid docs on how to run with gunicorn, that is what needs to go in your Procfile. You don't need a run.py.

Try this:
Profile
web: ./run
run
#!/bin/bash
set -e
python setup.py develop
python runapp.py
runapp.py
#Heroku Startup
import os
from paste.deploy import loadapp
from waitress import serve
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app = loadapp('config:production.ini', relative_to='.')
serve(app, host='0.0.0.0', port=port)
requirements.txt
pyramid
pyramid_chameleon
pyramid_debugtoolbar
waitress
<add other dependencies here>
runtime.txt
python-3.3.0 #or whatever version you are running. Take this out

Related

How to deploy python application to evennode?

After reading following docs and examples about deploying python app to evennode I've tried to do it with Flask application, but didn't succeed
https://www.evennode.com/docs/git-deployment
https://github.com/evennode/python-getting-started
Here is my main.py module's code:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello, World!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8000)
Also I'have created conf.py file with configuration for gunicorn:
workers = 4
bind = "0.0.0.0:8000"
Then I'm running the application with gunicorn --config=conf.py main:app and all works well on my local machine. To run it on evennode I populated requirements.txt and committed above files. Then run following commands:
git remote add evennode git#git.evennode.com:your_app_here
git push evennode master
The output looks next way and I don't know what to do with it:
ssh: connect to host git.evennode.com port 8000: No route to host
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I have added my public ssh key to evennode app settings as well, so that's can't be an issue
Any help is appreciated

Flask deployment using twistd

In the flask doco the following description is shown of deploying a flask app under twistd.
twistd web --wsgi myproject.app
I have a foo.py which looks like this
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)
So I expected to be able to run that under twistd like this
twistd web --wsgi foo.app
but twistd doesn't like that (just spits out the help text).
What am I doing wrong ?
BTW in case it matters I'm running this in a virtualenv (in which I have installed both flask and twisted) and the current directory when I issue the twistd command contains foo.py .
EDIT: The version of twistd I am using is 18.7.0
I had failed to notice (until prompted to by Peter Gibson's comment ) that after the help text appears the message "No such WSGI application: 'foo.app'" appears.
You need to add the current directory to the PYTHONPATH environment variable. Try
PYTHONPATH=. twistd web --wsgi foo.app
Or on Windows (untested)
set PYTHONPATH=.
twistd web --wsgi foo.app

Running flask as package in production

I am trying to deploy my flask app. Usually I would have an app.py and put all code in it.
app.py
templates/
|
|--index.html
for my really small projects. But then I have a slightly larger app and follow the larger app guide by flask.
So I have this:
setup.py
app/
__init__.py
views.py
models.py
forms.py
templates/
| ---index.html
I now have all my routes and views in views.py and running the app in __init__.py:
from flask import Flask
app = Flask(__name__)
import app.views # Name in setup.py
if __name__ == "__main__":
app.run()
(This is just an example)
So now I follow the guide by running it with pip install -e . and running with:
>set FLASK_APP=app(name I set in setup.py) flask run and it works. Except I do not know how to run it with one command. Since there is no one file to run I can not use gunicorn or anything like that. I am not sure how to go about executing this app. How would I run pip install . on the cloud server heroku?
My problem is because I have to import the app from __init__.py and views using import blog.[insert import] (models, views etc.) Any help is appreciated. Thank you.
EDIT: I do not want to use blueprints though. That might be too much. My app is medium, not small but not large either
You absolutely can use Gunicorn to run this project. Gunicorn is not limited to a single file, it imports Python modules just the same as flask run can. Gunicorn just needs to know the module to import, an the WSGI object to call within that module.
When you use FLASK_APP, all that flask run does is look for module.app, module.application or instances of the Flask() class. It also supports a create_app() or make_app() app factory, but you are not using such a factory.
Gunicorn won't search, if you only give it a module, it'll expect the name application to be the WSGI callable. In your case, you are using app so all you have to do is explicitly tell it what name to use:
gunicorn app:app
The part before the : is the module to import (app in your case), the part after the colon is the callable object (also named app in your module).
If you have set FLASK_APP as a Heroku config var and want to re-use that, you can reference that on the command line for gunicorn:
gunicorn $FLASK_APP:app
As for heroku, it can handle requirement.txt or setup.py
c.f. https://devcenter.heroku.com/articles/python-pip#local-file-backed-distributions
If your Python application contains a setup.py file but excludes a requirements.txt file, python setup.py develop will be used to install your package and resolve your dependencies.
If you already have a requirements file, but would like to utilize this feature, you can add the following to your requirements file:
-e .
And about run command, i think you cat put Procfile like
web: FLASK_APP=app flask run
or
web: FLASK_APP=app python -m flask run

Not able to deploy pyramid application to the heroku running well in local

This is the error I get checking the logs on HEROKU.
The documentation of pyramid says "Create run with the following command."
So I create a file named as run.py and saved below codes. I dont know if this is the right way to create a run if not help me with that.
Here is the line of code in run.py
#!/bin/bash
set -e
python setup.py develop
python runapp.py
Here is the runapp.py
import os
from paste.deploy import loadapp
from paste import httpserver
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app = loadapp('config:development.ini', relative_to='.')
httpserver.serve(app, host='0.0.0.0', port=port)
Finally here is the procfile
$ echo "web: ./run" > Procfile
The Pyramid Community Cookbook has a deployment recipe for Heroku, assuming that is what you meant by "the documentation of Pyramid" (it's not official documentation, just a collection of recipes from the community).
In Step 1, make sure that the four files you create are done so locally in the root of your project directory.
requirements.txt
The file you named run.py should be named run.
Check that the file named Procfile was created and contains not the command to generate Procfile, but just the result of the command, specifically:
web: ./run
runapp.py

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 ?

Categories

Resources