devcenter | toolbelt | apps | database | tutorial
My Apps :
lwc-ws |
ecommerce-cfe |
Heroku (Python)
Getting Started with Python on Heroku
Heroku Toolbet
Prerequisite
- a free Heroku account.
- Python installed locally. Windows,Linux,OS X
- Setuptools and Pip installed locally.
- Virtualenv installed locally.
- Postgres installed locally, if running the app locally.
帮助 help
>heroku --help
准备应用 Prepare the app
clone project
git clone git@github.com:navicester/spreading.git
cd spreading
check version
git remote -v
origin git@github.com:navicester/spreading.git (fetch)
origin git@github.com:navicester/spreading.git (push)
部署应用 Deploy the app
登陆 login
>heroku login
Enter your Heroku credentials.
Email: navicester@qq.com
Password (typing will be hidden):
Logged in as navicester@qq.com
创建 create
>heroku create
Creating app... done, stack is cedar-14
https://thawing-tor-1782.herokuapp.com/ | https://git.heroku.com/thawing-tor-1782.git
如果不指定app名字,系统会自动生成一个随机名字 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 trydjango18course
下面会创建两个git
>git remote -v
heroku https://git.heroku.com/trydjango18course.git (fetch)
heroku https://git.heroku.com/trydjango18course.git (push)
origin git@github.com:navicester/trydjango18course.git (fetch)
origin git@github.com:navicester/trydjango18course.git (push)
常见问题
- 先建立git,然后再执行heroku create,否则无法push
- 连接超时(代理,未验证) https://devcenter.heroku.com/articles/using-the-cli#using-an-http-proxy
部署代码 deploy your code
(trydjango18) D:\virtualenv\trydjango18\src>git push heroku master
scale
安装正确后程序会运行
> (trydjango18) D:\virtualenv\trydjango18\src>heroku ps:scale web=1
> Scaling dynos... done, now running web at 1:Free
> (trydjango18) D:\virtualenv\trydjango18\src>heroku ps
> === web (Free): gunicorn trydjango18.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
如果wsgi是子目录的话,需要用xx.trydjango18.wsgi,xx是python package
声明依赖 Declare app dependencies
requirements.txt, and it looks something like this:
Django==1.8.4
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.
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.
- whitenoise : Radically simplified static file serving for WSGI applications
之前版本如果下面两个不安装,css不能正常工作,新版本中好像没这个问题了
dj-static==0.0.5
static==0.4
2017-9-16 我运行了collectstatic命令之后也解决了这个问题
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 collectstatic
to 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 likeweb: python manage.py runserver 0.0.0.0:5000
. To start your app locally, run:heroku local web -f Procfile.windows
(trydjango18) D:\virtualenv\trydjango18\src>heroku local web -f Procfile.windows
web: python manage.py runserver 0.0.0.0:5000
- Open http://localhost:5000 with your web browser
Provision add-ons
配置数据库 Provision a database
sqlite3
Python老版本执行python manage.py syncdb会在heroku server上创建db文件,但是django==1.8好像不行,得传上去
Psycopg2
安装psycopg2
psycopg2==2.6.1
psycopg2 : Psycopg is the most popular PostgreSQL database adapter for the Python programming language.
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中的数据库配置
from django.conf import settings
DATABASES = settings.DATABASES
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 <DYNO> 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
或者
heroku run bash
$python manage.py runserver
$exit
或者
打开控制台
(trydjango18) D:\virtualenv\trydjango18\src>heroku run python manage.py shell
打开网址
https://trydjango18course.herokuapp.com
(trydjango18) D:\virtualenv\trydjango18\src>heroku open
常见问题
- 2016-09-17T08:36:03.029030+00:00 app[web.1]: bash: gunicorn: command not found
安装guicorn
gunicorn : Gunicorn ‘Green Unicorn’ is a Python WSGI HTTP Server for UNIX.
评论
留言请先登录或注册! 并在激活账号后留言!