I'm writing a Python Flask app to deploy on Heroku. It will use a database. For local development, I'd like to use Sqlite, but when deployed to Heroku I'd like to use Postgresql. How can I achieve this?

I'm stuck because I don't know how to require a different set of packages between my box and the Heroku server.


Were this a Ruby app I would write in my Gemfile

gem "pg", :group => :production
gem "sqlite3", :group => :development

Then Bundler would install the appropriate packages in development and in production. But I don't know any analogous workflow for Python's pip

有帮助吗?

解决方案

Well, you have two things to solve.

First, the requirements.txt which isn't that much of a problem. You can either throw all the requirements in the same requirements.txt file, having both database bindings installed doesn't harm anything. If you want to separate, however, just use requirements.txt for deploying, and requirements-dev.txt for local development.

More important is the DB settings itself, and for that you have a one liner solution:

app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get(
    'DATABASE_URL', 'sqlite:////tmp/test.db')

Since DATABASE_URL is set on Heroku, but not on local (make sure this is the case), os.environ.get will not find it, thus reverting to the default, which is the sqlite connection string .

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top