Heroku (Python)

Getting Started with Python on Heroku

Heroku Toolbet

Prerequisite

help

>heroku --help

Prepare the app

clone project

git clone git@github.com:navicester/spreading.git
cd spreading

check version

git remote -v
#log
origin  git@github.com:navicester/spreading.git (fetch)
origin  git@github.com:navicester/spreading.git (push)

Deploy the app

login

>heroku login

create

>heroku create
#Log
Creating app... done, stack is cedar-14
https://thawing-tor-1782.herokuapp.com/ | https://git.heroku.com/thawing-tor-1782.git

Heroku generates a random name (in this case thawing-tor-1782) for your app, or you can pass a parameter to specify your own app name.

>heroku create lwc

下面会创建两个git

>git remote -v
#log
heroku  https://git.heroku.com/thawing-tor-1782.git (fetch)
heroku  https://git.heroku.com/thawing-tor-1782.git (push)
origin  git@github.com:navicester/spreading.git (fetch)
origin  git@github.com:navicester/spreading.git (push)

常见问题

  1. 先建立git,然后再执行heroku create,否则无法push
  2. 连接超时(代理,未验证)
    https://devcenter.heroku.com/articles/using-the-cli#using-an-http-proxy

deploy your code

git push heroku master
>heroku ps:scale web=1
>heroku ps
(lwc_test) D:\virtualdir\lwc_test\lwc>heroku ps:scale web=1
# log
Scaling dynos... done, now running web at 1:Free.

(lwc_test) D:\virtualdir\lwc_test\lwc>heroku ps
#log
=== web (Free): gunicorn lwc.wsgi
web.1: up 2016/01/17 15:05:27 (~ 4m ago)

View logs

View information about your running app using one of the logging commands, heroku logs:

heroku logs --tail

Define a Procfile

Use a Procfile, a text file in the root directory of your application, to explicitly declare what command should be executed to start your app.

The Procfile in the example app you deployed looks like this:

web: gunicorn spreading.wsgi --log-file -

if wsgi in a deep subfolder, need xx.spreading.wsgi to get it work, xx is python package

Declare app dependencies

requirements.txt, and it looks something like this:

Django==1.9.1
gunicorn==19.4.5
psycopg2==2.6.1
whitenoise==2.0.6
dj-database-url==0.3.0
below 2 tools need to be added, otherwise, the css won't work
dj-static==0.0.5
static==0.4

gunicorn : Gunicorn 'Green Unicorn' is a Python WSGI HTTP Server for UNIX.
psycopg2 : Psycopg is the most popular PostgreSQL database adapter for the Python programming language.
whitenoise : Radically simplified static file serving for WSGI applications
dj-database-url : This simple Django utility allows you to utilize the 12factor inspired DATABASE_URL environment variable to configure your Django application.
dj-static : This is a simple Django middleware utility that allows you to properly serve static assets from production with a WSGI server like Gunicorn.
static : Serve static or templated content via WSGI or stand-alone.
wsgiref : WSGI (PEP 333) Reference Library
wheel : A built-package format for Python.

Install the dependencies

pip freeze > requirements.txt
#添加其他没有生成的package
pip install -r requirements.txt

make sure you are working in the right environment, if you work in virtualenv, run
'Scripts\activate' before 'pip freeze'

in wsgi.py

try:
    from dj_static import Cling

    application = Cling(get_wsgi_application())
except:
    pass

Run the app locally

  • use python manage.py collectstaticto collect statistic
    ( when push to Heroku, it will run this automaticly)

  • Edit the requirements.txt file. Remove the line containing gunicorn. Or add a new file **requirements_windows.txt", then run pip install -r requirement_windows.txt

  • Now start your application locally using heroku local, which was installed as part of the Toolbelt. Just like Heroku, heroku local examines the Procfile to determine what to run. Instead of using the default Procfile, use the Windows version which starts a simple Python web server instead. It reads something like web: python manage.py runserver 0.0.0.0:5000. To start your app locally, run: heroku local web -f Procfile.windows

  • Open http://localhost:5000 with your web browser

Provision add-ons

Provision a database

heroku database
Migrating from MySQL to Postgres on Heroku
heroku-postgresql
在该目录下可以看到db,或者直接从apps进入add-on中的数据库

Connection Settings
Host
    ec2-54-225-165-132.compute-1.amazonaws.com
Database
    dbv6n78stkmeqg
User
    ntccscinrvwneo
Port
    5432
Password
    Show 
Psql
    heroku pg:psql --app intense-depths-2863 DATABASE
URL
    Show

修改settings中的数据库配置

import dj_database_url
# Parse database configuration from $DATABASE_URL
DATABASES['default'] =  dj_database_url.config()

it's not needed to have explicit configuration as below:

if len(DATABASES['default']) == 0:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'dbv6n78stkmeqg',
            'USER': 'ntccscinrvwneo',
            'PASSWORD': '5432',
            'HOST': '', 
            'PORT': '', 
        }
    }

if I continue to use sqlite3, it will report error

Exception Type: DatabaseError
Exception Value:    no such table: django_session

A database is an add-on, and so you can find out a little more about the database provisioned for your app using the addons command in the CLI:

heroku addons

Listing the config vars for your app will display the URL that your app is using to connect to the database, DATABASE_URL:

heroku config

Heroku also provides a pg command that shows a lot more:

heroku pg

常见问题

Issue 1: Django. South. Heroku. KeyError: 'default'

添加了DATABASES['default'] = dj_database_url.config()
, local运行python manage.oy runserver时,报如下错误

  File "D:\PythonWebSW\Python27\lib\site-packages\south-0.8.4-py2.7.egg\south\db\__init__.py", line 83, in <module>
    db = dbs[DEFAULT_DB_ALIAS]
KeyError: 'default'

Solution : add

import os
from django.conf import settings

参考:http://stackoverflow.com/questions/23964807/django-south-heroku-keyerror-default
evernote

Issue 2 : Cannot run more than 1 Free size dynos.

You would do this via heroku ps and then heroku ps:stop to stop the process.
Eg.

(lwc_test) D:\virtualdir\lwc_test\lwc>heroku ps
=== web (Free): gunicorn lwc.wsgi
web.1: idle 2016/01/20 00:48:46 (~ 21h ago)
=== run: one-off processes
run.2351 (Free): up 2016/01/19 23:36:22 (~ 22h ago): python manage.py shell

(lwc_test) D:\virtualdir\lwc_test\lwc>heroku ps:stop run.5656
Stopping run.5656 dyno... done

(lwc_test) D:\virtualdir\lwc_test\lwc>heroku ps:stop web.1
Stopping web.1 dyno... done

database

如果是sqlite3,不需要把db文件传到服务器上(猜测系统会根据setting文件自动创建)

heroku run python manage.py syncdb

Exception Type:
DatabaseError

Exception Value:
no such table: joins_join

no-such-table-error-on-heroku-after-django-syncdb-passed
no-such-table-error-on-heroku-after-django-syncdb-passed

Run Command in Heroku

heroku run python manage.py runserver

OR

heroku run bash
$python manage.py runserver
$exit

关于下面公众号获取更多信息

优质内容筛选与推荐>>
1、背包 http://blog.csdn.net/insistgogo/article/details/8579597
2、HTML 5 <li> 标签
3、MyEclipse下开发Web Service
4、分布式锁的几种实现方法:redis实现分布式锁
5、面试的经过


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号