如何將 Python 模組部署到 Heroku?


假設你已經按照此步驟,在本地設定了 Python 3.6、Pipenv 和 Heroku CLI,並從 CLI 使用 Heroku 登入:https://devcenter.heroku.com/articles/getting-started-with-python#set-up.

你的應用程式需要一個 git 儲存庫,才能部署到 Heroku。你需要 cd 到你的 git repo 程式碼根目錄。現在你需要使用以下命令建立 Heroku 應用程式

$ heroku create
Creating lit-bastion-5032 in organization heroku... done, stack is cedar-14

http://lit-bastion-5032.herokuapp.com/ | https://git.heroku.com/lit-bastion-5032.git

新增 Git 遠端 Heroku

當你建立應用時,也會建立一個 git 遠端(稱為 Heroku)並將其與你的本地 git 儲存庫關聯。Heroku 會為你的應用生成一個隨機名稱(在本例中為 lit-bastion-5032),或者你可以傳遞一個引數來指定自己的應用名稱。

現在已經添加了一個遠端,你可以使用以下命令將你的程式碼推送到 Heroku

$ git push heroku master
Counting objects: 232, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (217/217), done.
Writing objects: 100% (232/232), 29.64 KiB | 0 bytes/s, done.
Total 232 (delta 118), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing python-3.6.0
remote: -----> Installing requirements with latest pipenv...
remote:        Installing dependencies from Pipfile.lock...
remote:      $ python manage.py collectstatic --noinput
remote:        58 static files copied to '/app/gettingstarted/staticfiles', 58 post-processed.
remote:
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 39.3M
remote: -----> Launching...
remote:        Released v4
remote:        http://lit-bastion-5032.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To git@heroku.com:lit-bastion-5032.git
 * [new branch]      master -> master
Note that you need to specify your requirements(third party modules you are importing) with their version numbers(or without if you need latest one) in the requirements.txt. For example,
Flask==0.8
Jinja2==2.6
Werkzeug==0.8.3
certifi==0.0.8
chardet==1.0.1

你可以在 Heroku python 文件中瞭解更多相關資訊:https://devcenter.heroku.com/articles/python-pip

更新時間:2020 年 6 月 15 日

272 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.