2016-03-16 44 views
0

現在我正在向Heroku部署一個Flask應用程序,即自定義表單模塊。如何在部署到Heroku時使用Procfile將Gunicorn的自定義模塊導入?

當我檢查heroku日誌時,它會在嘗試導入註冊表單時收到導入錯誤。

2016-03-16T18:40:08.254753+00:00 heroku[web.1]: State changed from crashed to starting 
2016-03-16T18:40:12.022731+00:00 heroku[web.1]: Starting process with command `gunicorn app.register:app` 
2016-03-16T18:40:14.912795+00:00 app[web.1]: [2016-03-16 18:40:14 +0000] [3] [INFO] Starting gunicorn 19.4.5 
2016-03-16T18:40:14.913529+00:00 app[web.1]: [2016-03-16 18:40:14 +0000] [3] [INFO] Listening at: http://0.0.0.0:39309 (3) 
2016-03-16T18:40:14.913654+00:00 app[web.1]: [2016-03-16 18:40:14 +0000] [3] [INFO] Using worker: sync 
2016-03-16T18:40:14.917468+00:00 app[web.1]: [2016-03-16 18:40:14 +0000] [7] [INFO] Booting worker with pid: 7 
2016-03-16T18:40:14.951783+00:00 app[web.1]: [2016-03-16 18:40:14 +0000] [8] [INFO] Booting worker with pid: 8 
2016-03-16T18:40:15.665966+00:00 heroku[web.1]: State changed from starting to up 
2016-03-16T18:40:15.568052+00:00 app[web.1]: [2016-03-16 18:40:15 +0000] [8] [ERROR] Exception in worker process: 
2016-03-16T18:40:15.568074+00:00 app[web.1]: Traceback (most recent call last): 
2016-03-16T18:40:15.568076+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/arbiter.py", line 515, in spawn_worker 
2016-03-16T18:40:15.568076+00:00 app[web.1]:  worker.init_process() 
2016-03-16T18:40:15.568077+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/workers/base.py", line 122, in init_process 
2016-03-16T18:40:15.568078+00:00 app[web.1]:  self.load_wsgi() 
2016-03-16T18:40:15.568078+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/workers/base.py", line 130, in load_wsgi 
2016-03-16T18:40:15.568079+00:00 app[web.1]:  self.wsgi = self.app.wsgi() 
2016-03-16T18:40:15.568080+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/app/base.py", line 67, in wsgi 
2016-03-16T18:40:15.568081+00:00 app[web.1]:  self.callable = self.load() 
2016-03-16T18:40:15.568081+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 65, in load 
2016-03-16T18:40:15.568082+00:00 app[web.1]:  return self.load_wsgiapp() 
2016-03-16T18:40:15.568083+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp 
2016-03-16T18:40:15.568083+00:00 app[web.1]:  return util.import_app(self.app_uri) 
2016-03-16T18:40:15.568084+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/util.py", line 357, in import_app 
2016-03-16T18:40:15.568085+00:00 app[web.1]:  __import__(module) 
2016-03-16T18:40:15.568086+00:00 app[web.1]:  from registration_form import RegistrationForm 
2016-03-16T18:40:15.568086+00:00 app[web.1]: File "/app/app/register.py", line 2, in <module> 
2016-03-16T18:40:15.568087+00:00 app[web.1]: ImportError: No module named 'registration_form' 

這就是目錄的樣子。

app 
├── register.py 
├── registration_form.py 
Procfile 
requirements.txt 
runtime.txt 
virtual 

這在我的Procfile中。

web: gunicorn app.register:app 

這是我的應用程序/文件register.py

from flask import Flask, render_template, request 
from registration_form import RegistrationForm 

app = Flask(__name__) 

@app.route('/register', methods=['GET', 'POST']) 
def register(): 
    form = RegistrationForm(request.form) 
    #if request.method == 'POST' and form.validate(): 
    return render_template('register.html', form=form) 

if __name__ == '__main__': 
    app.run(debug=True) 

這是我的應用程序/文件registration_form.py

from wtforms import Form, BooleanField, TextField, PasswordField, validators 

class RegistrationForm(Form): 
    first_name = TextField('First Name', [validators.Length(min=2, max=25)]) 
    last_name = TextField('Last Name', [validators.Length(min=4, max=35)]) 
    email = TextField('Email', [validators.Email()]) 
    zip_code = TextField('Zip Code', [validators.Length(min=4, max=35)]) 
    accept_tos = BooleanField('I accept the TOS', [validators.Required()]) 

我應該添加到我的Procfile進口登記形成?當我在本地運行應用程序時,它工作正常,所以我覺得它與Gunicorn的配置方式有關。

回答

1

Python 3.x不支持相對的導入方式。您需要更新您的導入:

from .registration_form import RegistrationForm 

或者,你可以做一個絕對的導入:

from app.registration_form import RegistrationForm 
+0

真棒!那就是訣竅。謝謝你這麼快回答我的問題 –

相關問題