2018-04-03T19:15:17Z

The Flask Mega-Tutorial Part XVIII: Deployment on Heroku

This is the eighteenth installment of the Flask Mega-Tutorial series, in which I'm going to deploy Microblog to the Heroku cloud platform.

For your reference, below is a list of the articles in this series.

Note 1: If you are looking for the legacy version of this tutorial, it's here.

Note 2: If you would like to support my work on this blog, or just don't have patience to wait for weekly articles, I am offering the complete version of this tutorial packaged as an ebook or a set of videos. For more information, visit courses.miguelgrinberg.com.

In the previous article I showed you the "traditional" way to host a Python application, and I gave you two actual examples of deployment to Linux based servers. If you are not used to manage a Linux system, you probably thought that the amount of effort that needs to be put into the task was big, and that surely there must be an easier way.

In this chapter I'm going to show you a completely different approach, in which you rely on a third-party cloud hosting provider to perform most of the administration tasks, freeing you to spend more time working on your application.

Many cloud hosting providers offer a managed platform on which applications can run. All you need to provide to have your application deployed on these platforms is the actual application, because the hardware, operating system, scripting language interpreters, database, etc. are all managed by the service. This type of service is called Platform as a Service, or PaaS.

Sounds too good to be true, right?

I will look at deploying Microblog to Heroku, a popular cloud hosting service that is also very friendly for Python applications. I picked Heroku not only because it is popular, but also because it has a free service level that will allow you to follow me and do a complete deployment without spending any money.

The GitHub links for this chapter are: Browse, Zip, Diff.

Hosting on Heroku

Heroku was one of the first platform as a service providers. It started as a hosting option for Ruby based applications, but then grew to support many other languages like Java, Node.js and of course Python.

Deploying a web application to Heroku is done through the git version control tool, so you must have your application in a git repository. Heroku looks for a file called Procfile in the application's root directory for instructions on how to start the application. For Python projects, Heroku also expects a requirements.txt file that lists all the module dependencies that need to be installed. After the application is uploaded to Heroku's servers through git, you are essentially done and just need to wait a few seconds until the application is online. It's really that simple.

The different service tiers Heroku offers allow you to choose how much computing power and time you get for your application, so as your user base grows you will need to buy more units of computing, which Heroku calls "dynos".

Ready to try Heroku? Let's get started!

Creating Heroku account

Before you can deploy to Heroku you need to have an account with them. So visit heroku.com and create a free account. Once you have an account and log in to Heroku, you will have access to a dashboard, where all your applications are listed.

Installing the Heroku CLI

Heroku provides a command-line tool for interacting with their service called Heroku CLI, available for Windows, Mac OS X and Linux. The documentation includes installation instructions for all the supported platforms. Go ahead and install it on your system if you plan on deploying the application to test the service.

The first thing you should do once the CLI is installed is login to your Heroku account:

$ heroku login

Heroku CLI will ask you to enter your email address and your account password. Your authenticated status will be remembered in subsequent commands.

Setting Up Git

The git tool is core to the deployment of applications to Heroku, so you must install it on your system if you don't have it yet. If you don't have a package available for your operating system, you can visit the git site to download an installer.

There are a lot of reasons why using git for your projects makes sense. If you plan to deploy to Heroku, you have one more, because to deploy to Heroku, your application must be in a git repository. If you are going to do a test deployment for Microblog, you can clone the application from GitHub:

$ git clone https://github.com/miguelgrinberg/microblog
$ cd microblog
$ git checkout v0.18

The git checkout command selects the specific commit that has the application at the point in its history that corresponds to this chapter.

If you prefer to work with your own code instead of mine, you can transform your own project into a git repository by running git init . on the top-level directory (note the period after init, which tells git that you want to create the repository in the current directory).

Creating a Heroku Application

To register a new application with Heroku, you use the apps:create command from the root directory of the application, passing the application name as the only argument:

$ heroku apps:create flask-microblog
Creating flask-microblog... done
http://flask-microblog.herokuapp.com/ | https://git.heroku.com/flask-microblog.git

Heroku requires that applications have a unique name. The name flask-microblog that I used above is not going to be available to you because I'm using it, so you will need to pick a different name for your deployment.

The output of this command will include the URL that Heroku assigned to the application, and also its git repository. Your local git repository will be configured with an extra remote, called heroku. You can verify that it exists with the git remote command:

$ git remote -v
heroku  https://git.heroku.com/flask-microblog.git (fetch)
heroku  https://git.heroku.com/flask-microblog.git (push)

Depending on how you created your git repository, the output of the above command could also include another remote called origin.

The Ephemeral File System

The Heroku platform is different to other deployment platforms in that it features an ephemeral file system that runs on a virtualized platform. What does that mean? It means that at any time, Heroku can reset the virtual server on which your server runs back to a clean state. You cannot assume that any data that you save to the file system will persist, and in fact, Heroku recycles servers very often.

Working under these conditions introduces some problems for my application, which uses a few files:

  • The default SQLite database engine writes data in a disk file
  • Logs for the application are also written to the file system
  • The compiled language translation repositories are also written to local files

The following sections will address these three areas.

Working with a Heroku Postgres Database

To address the first problem, I'm going to switch to a different database engine. In Chapter 17 you saw me use a MySQL database to add robustness to the Ubuntu deployment. Heroku has a database offering of its own, based on the Postgres database, so I'm going to switch to that to avoid the file-based SQLite.

Databases for Heroku applications are provisioned with the same Heroku CLI. In this case I'm going to create a database on the free tier:

$ heroku addons:add heroku-postgresql:hobby-dev
Creating heroku-postgresql:hobby-dev on flask-microblog... free
Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pg:copy
Created postgresql-parallel-56076 as DATABASE_URL
Use heroku addons:docs heroku-postgresql to view documentation

The URL for the newly created database is stored in a DATABASE_URL environment variable that will be available when the application runs. This is very convenient, because the application already looks for the database URL in that variable.

Logging to stdout

Heroku expects applications to log directly to stdout. Anything the application prints to the standard output is saved and returned when you use the heroku logs command. So I'm going to add a configuration variable that indicates if I need to log to stdout or to a file like I've been doing. Here is the change in the configuration:

config.py: Option to log to stdout.

class Config(object):
    # ...
    LOG_TO_STDOUT = os.environ.get('LOG_TO_STDOUT')

Then in the application factory function I can check this configuration to know how to configure the application's logger:

app/__init__.py: Log to stdout or file.

def create_app(config_class=Config):
    # ...
    if not app.debug and not app.testing:
        # ...

        if app.config['LOG_TO_STDOUT']:
            stream_handler = logging.StreamHandler()
            stream_handler.setLevel(logging.INFO)
            app.logger.addHandler(stream_handler)
        else:
            if not os.path.exists('logs'):
                os.mkdir('logs')
            file_handler = RotatingFileHandler('logs/microblog.log',
                                               maxBytes=10240, backupCount=10)
            file_handler.setFormatter(logging.Formatter(
                '%(asctime)s %(levelname)s: %(message)s '
                '[in %(pathname)s:%(lineno)d]'))
            file_handler.setLevel(logging.INFO)
            app.logger.addHandler(file_handler)

        app.logger.setLevel(logging.INFO)
        app.logger.info('Microblog startup')

    return app

So now I need to set the LOG_TO_STDOUT environment variable when the application runs in Heroku, but not in other configurations. The Heroku CLI makes this easy, as it provides an option to set environment variables to be used at runtime:

$ heroku config:set LOG_TO_STDOUT=1
Setting LOG_TO_STDOUT and restarting flask-microblog... done, v4
LOG_TO_STDOUT: 1

Compiled Translations

The third aspect of Microblog that relies on local files is the compiled language translation files. The more direct option to ensure those files never disappear from the ephemeral file system is to add the compiled language files to the git repository, so that they become part of the initial state of the application once it is deployed to Heroku.

A more elegant option, in my opinion, is to include the flask translate compile command in the start up command given to Heroku, so that any time the server is restarted those files are compiled again. I'm going to go with this option, since I know that my start up procedure is going to require more than one command anyway, since I also need to run the database migrations. So for now, I will set this problem aside, and will revisit it later when I write the Procfile.

Elasticsearch Hosting

Elasticsearch is one of the many services that can be added to a Heroku project, but unlike Postgres, this is not a service provided by Heroku, but by third parties that partner with Heroku to provide add-ons. At the time I'm writing this, there are three different providers of an integrated Elasticsearch service.

Before you configure Elasticsearch, be aware that Heroku requires your account to have a credit card on file before any third party add-on is installed, even if you stay within their free tiers. If you prefer not to provide your credit card to Heroku, then skip this section. You will still be able to deploy the application, but the search functionality is not going to work.

Out of the Elasticsearch options that are available as add-ons, I decided to try SearchBox, which comes with a free starter plan. To add SearchBox to your account, you have to run the following command while being logged in to Heroku:

$ heroku addons:create searchbox:starter

This command will deploy an Elasticsearch service and leave the connection URL for the service in a SEARCHBOX_URL environment variable associated with your application. Once more keep in mind that this command will fail unless you add your credit card to your Heroku account.

If you recall from Chapter 16, my application looks for the Elasticsearch connection URL in the ELASTICSEARCH_URL variable, so I need to add this variable and set it to the connection URL assigned by SearchBox:

$ heroku config:get SEARCHBOX_URL
<your-elasticsearch-url>
$ heroku config:set ELASTICSEARCH_URL=<your-elasticsearch-url>

Here I first asked Heroku to print the value of SEARCHBOX_URL, and then I added a new environment variable with the name ELASTICSEARCH_URL set to that same value.

Updates to Requirements

Heroku expects the dependencies to be in the requirements.txt file, exactly like I defined it in Chapter 15. But for the application to run on Heroku I need to add two new dependencies to this file.

Heroku does not provide a web server of its own. Instead, it expects the application to start its own web server on the port number given in the environment variable $PORT. Since the Flask development web server is not robust enough to use for production, I'm going to use gunicorn again, the server recommended by Heroku for Python applications.

The application will also be connecting to a Postgres database, and for that SQLAlchemy requires the psycopg2 package to be installed.

Both gunicorn and psycopg2 need to be added to the requirements.txt file.

The Procfile

Heroku needs to know how to execute the application, and for that it uses a file named Procfile in the root directory of the application. The format of this file is simple, each line includes a process name, a colon, and then the command that starts the process. The most common type of application that runs on Heroku is a web application, and for this type of application the process name should be web. Below you can see a Procfile for Microblog:

Procfile: Heroku Procfile.

web: flask db upgrade; flask translate compile; gunicorn microblog:app

Here I defined the command to start the web application as three commands in sequence. First I run a database migration upgrade, then I compile the language translations, and finally I start the server.

Because the first two sub-commands are based on the flask command, I need to add the FLASK_APP environment variable:

$ heroku config:set FLASK_APP=microblog.py
Setting FLASK_APP and restarting flask-microblog... done, v4
FLASK_APP: microblog.py

The application also relies on other environment varialbes, such as those that configure the email server or the token for the live translations. Those need to be added with addition heroku config:set commands.

The gunicorn command is simpler than what I used for the Ubuntu deployment, because this server has a very good integration with the Heroku environment. For example, the $PORT environment variable is honored by default, and instead of using the -w option to set the number of workers, heroku recommends adding a variable called WEB_CONCURRENCY, which gunicorn uses when -w is not provided, giving you the flexibility to control the number of workers without having to modify the Procfile.

Deploying the Application

All the preparatory steps are complete, so now it is time to run the deployment. To upload the application to Heroku's servers for deployment, the git push command is used. This is similar to how you push changes in your local git repository to GitHub or other remote git server.

And now I have reached the most interesting part, where I push the application to our Heroku hosting account. This is actually pretty simple, I just have to use git to push the application to the master branch of the Heroku git repository. There are a couple of variations on how to do this, depending on how you created your git repository. If you are using my v0.18 code, then you need to create a branch based on this tag, and push it as the remote master branch, as follows:

$ git checkout -b deploy
$ git push heroku deploy:master

If instead, you are working with your own repository, then your code is already in a master branch, so you first need to make sure that your changes are committed:

$ git commit -a -m "heroku deployment changes"

And then you can run the following to start the deployment:

$ git push heroku master

Regardless of how you push the branch, you should see the following output from Heroku:

$ git push heroku deploy:master
Counting objects: 247, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (238/238), done.
Writing objects: 100% (247/247), 53.26 KiB | 3.80 MiB/s, done.
Total 247 (delta 136), reused 3 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing python-3.6.2
remote: -----> Installing pip
remote: -----> Installing requirements with pip
...
remote:
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 57M
remote: -----> Launching...
remote:        Released v5
remote:        https://flask-microblog.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/flask-microblog.git
 * [new branch]      deploy -> master

The label heroku that we used in the git push command is the remote that was automatically added by the Heroku CLI when the application was created. The deploy:master argument means that I'm pushing the code from the local repository referenced by the deploy branch to the master branch on the Heroku repository. When you work with your own projects, you will likely be pushing with the command git push heroku master, which pushes your local master branch. Because of the way this project is structured, I'm pushing a branch that is not master, but the destination branch on the Heroku side always needs to be master as that is the only branch that Heroku accepts for deployment.

And that is it, the application should now be deployed at the URL that you were given in the output of the command that created the application. In my case, the URL was https://flask-microblog.herokuapp.com, so that is what I need to type to access the application.

If you want to see the log entries for the running application, use the heroku logs command. This can be useful if for any reason the application fails to start. If there were any errors, those will be in the logs.

Deploying Application Updates

To deploy a new version of the application, you just need to run a new git push command with the new code. This will repeat the deployment process, take the old deployment offline, and then replace it with the new code. The commands in the Procfile will run again as part of the new deployment, so any new database migrations or translations will be updated during the process.

145 comments

  • #101 Travis said 2020-03-17T02:20:58Z

    Regarding John Smith's post above (#5 and #7), I just wanted to say I was also getting an error relating to the index not existing. I found in the SearchBox documentation that indexes are NOT created automatically. I was able to go do the dashboard for the plugin on the heroku site, and manually create an empty index called 'post'. After that, all app functionality worked.

    Running "heroku run flask shell" I could then use the "Post.reindex()" method to get back to a working state.

    This also revealed a source for an uncaught exception in the search.py module, when elasticsearch exists, but fails to let you add to the index. This causes the main post functionality to break. Adding another check for whether or not the index exists allowed this to be bypassed when posting.

  • #102 SetEx said 2020-04-10T07:55:11Z

    Hi Miguel, really big thanks for your tutorial.

    I have some problem: not long time ago i 've decided to visit http://flask-microblog.herokuapp.com/auth/register from my iPhone (CPU iPhone OS 10_3_4 like Mac OS X) and after submitting form got flash message 'The CSRF tokens do not match'. I am little bit confused because i see in code CSRF-protection...

    By the way when i 've deployed my own app to heroku with one WTF form i noticed the same problem while visiting site from IOS devices.

    Can you explain it?

  • #103 Miguel Grinberg said 2020-04-10T13:35:08Z

    @SetEx: I don't have an iOS device to test this on at hand, unfortunately. Safari on my Mac works just fine though. Maybe you have disabled cookies on your device? They are required for CSRF protection to work.

  • #104 Chris K. said 2020-04-20T23:56:50Z

    Hello Miguel, really appreciate your dedication to this fantastic tutorial, I've been learning to build my application along with it for a couple of months. After deploying on Heroku, the application cannot be opened and I got two errors in the application log.

    1. socketserver.TCPServer.server_bind(self) File "/app/.heroku/python/lib/python3.6/socketserver.py", line 470, in server_bind self.socket.bind(self.server_address) OSError: [Errno 98] Address already in use [INFO] Worker exiting (pid: 16) Shutting down: Master Reason: Worker failed to boot.

    I have no idea what the error is saying.

    2. psycopg2.errors.UndefinedObject: type "datetime" does not exist LINE 9: last_seen DATETIME,

    There must be something wrong with the database migration, so there is no datetime type in postgreSQL, what should I do to make it sucessfully migrate?

    Besides, I feel like the application is still using flask built-in server, not the gunicorn one. Because these are a couple line in the log:

    EDPE startup Serving Flask app "myBlogApp" (lazy loading) Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. Debug mode: off Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) Serving Flask app "myBlogApp" (lazy loading) Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. Debug mode: off [ERROR] Exception in worker process

    I am not sure if I missed something that is necessary to make gunicorn work as my application server.I only include the procfile(web: flask db upgrade; gunicorn runMyBlog:app ).

    Any tips will be appreciated!!!

  • #105 Miguel Grinberg said 2020-04-21T10:41:02Z

    @Chris: are you using the same Procfile I'm using? I agree, it seems your Heroku app is trying to run in the way we run during development, using the dev server and port 5000. Make sure your Procfile is correct, that's probably where the problem is.

    Another thing to check would be that you don't call app.run() somewhere in the global scope of a module, as that would force the Flask dev server to run even if you are starting with gunicorn.

  • #106 Chris K. said 2020-04-22T02:43:56Z

    Hello Miguel, Thank you so much, as you said, I fixed the procfile and deleted the line app.run(), then it worked! But when I tried to register and fill out the username field, an error raised saying:

    sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "user" does not exist LINE 2: FROM "user" ^ [SQL: SELECT "user".id AS user_id, "user".username AS user_username, "user".email AS user_email, "user".image_file AS user_image_file, "user".password_hash AS user_password_hash, "user".about_me AS user_about_me, "user".last_seen AS user_last_seen, "user".last_message_read_time AS user_last_message_read_time

    FROM "user" WHERE "user".username = %(username_1)s LIMIT %(param_1)s] [parameters: {'username_1': '1231', 'param_1': 1}] (Background on this error at: http://sqlalche.me/e/f405) 10.30.127.22 - - [22/Apr/2020:01:53:27 +0000] "POST /validateUsername HTTP/1.1" 500 10226 "https://edpegg.herokuapp.com/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"

    @app.route('/validateUsername', methods=['GET', 'POST']) def validate_username(): username = request.json['username'] user = User.query.filter_by(username=username).first() if user is None: return "true" return "false"

    This is the method where the error raised. Everything works fine before deploying. I doubt if I successfully switched SQLite to PostgreSQL. Because when I check the Heroku-PostgreSQL, it shows there are 0 tables and 0 rows under utilization section. And another thing is that the SQLite3 has installed automatically after git push Heroku master. Is there any conflict between the PostgreSQL and the SQILite3? I did specify "flask db upgrade" in the procfile.

  • #107 Miguel Grinberg said 2020-04-22T10:22:44Z

    @Chris: if your Flask configuration correctly points to the Postgres database, then the flask db upgrade should have created the tables there. Since you found that it didn't, my guess is that your configuration under Heroku still points to a SQLite database. Did you set the DATABASE_URL environment variable as shown in this article?

  • #108 Chris. K said 2020-04-23T05:10:07Z

    Hello Miguel, I fixed the migration problem, and the database works fine now. But I am having trouble with adding posts to the index of elasticsearch.

    elasticsearch.exceptions.NotFoundError: NotFoundError(404, 'index_not_found_exception', 'no such index [post]', post, index_expression)

    I used to add posts to index successfully before deploying, but I forgot how to do it now. Should I use the command ''heroku run flask shell" and then run below lines?

    from app.search import add_to_index, remove_from_index, query_index for post in Post.query.all(): ... add_to_index('posts', post)" somehow I had to press enter button twice to execute the for loop, and it turned out the same error as above.

  • #109 Miguel Grinberg said 2020-04-23T10:57:54Z

    @Chris: Have you sent an Elasticsearch database on Heroku? The index is created automatically when the first entry is added, so I suspect you have some other more basic problem in your Heroku setup.

  • #110 Dominic said 2020-05-02T10:37:24Z

    @Chris I had the same issue - turns out SearchBox does not create indices by default (https://devcenter.heroku.com/articles/searchbox#using-with-python). If you go to your app dashboard in Heroku, you can click into the SearchBox add-on and configure an index there!

  • #111 Kiwi said 2020-05-13T17:51:56Z

    H Miguel,

    thank you for the tutorial - it is great!

    I had the same famous problem which was reported many times in the comments section:

    sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "user" does not exist

    and heroku pg:info DATABASE produced the result (Tables: 0)

    === DATABASE_URL Plan: Hobby-dev Status: Available Connections: 2/20 PG Version: 12.2 Created: 2020-05-13 15:51 UTC Data Size: 7.9 MB Tables: 0 Rows: 0/10000 (In compliance) - refreshing Fork/Follow: Unsupported Rollback: Unsupported Continuous Protection: Off Add-on: postgresql-flexible-07950

    Then I have run in the CLI "heroku run flask db upgrade" and this has fixed the issue :-)!! Any idea how to control that instability? Thank you in advance!

  • #112 Miguel Grinberg said 2020-05-14T22:31:00Z

    @Kiwi: what do you mean by instability?

  • #113 Kiwi said 2020-05-17T08:18:34Z

    Essentially, I used your code from github for the deployment and I run all the procedures you described => the end result was "0 tables" created on the heroku side. This error happened to quite few people but for many more people the standard deployment procedure worked well (tables were created from the first time) . Then I run manually "heroku run flask db upgrade" and tables got created, all the blog started to work really well. I find the standard deploymnet process somehow unstable...

  • #114 Miguel Grinberg said 2020-05-17T13:45:20Z

    @Kiwi: the flask db upgrade command is included in the procfile. If it did not run there, you need to check the output of your deployment to see why it failed.

  • #115 Trem said 2020-05-19T22:08:27Z

    Hello Miguel, and thank you for this tutorial, I also got your (excellent!) books and started an app with them. I just have a quick question: I'm trying to deploy my app on Heroku, but as the postgres database is empty (and my SQLite database has table and data), so when I try to log in there's an 'internal server error' showing up. How can I populate the Heroku database with my SQLite data? Many thanks for your help!

  • #116 Miguel Grinberg said 2020-05-19T23:01:42Z

    @Trem: Moving the data from the development database to a production database is an unusual operation. Typically you start from scratch on the production db, as shown in this article. If you want to move the data you can do it by exporting the sqlite data and then importing it into Postgres. You will likely need to edit the exported file to adapt the SQL code in it to be compatible with Postgres as there are always some minor details that are different between databases.

  • #117 Dan said 2020-05-20T03:45:52Z

    Hi, thanks so much. I made a website using this tutorial for my python class. Was wondering if you knew how to use waitress for this application

  • #118 Miguel Grinberg said 2020-05-20T09:27:08Z

    @Dan: here is a waitress based Procfile for this project:

    web: flask db upgrade; flask translate compile; waitress-serve --port=$PORT microblog:app
  • #119 Richard said 2020-05-31T15:25:04Z

    I am trying to use your tutorial, and I changed the name of microblog.py to greencoin.py, (as I need a different name for it so I could expand on this tutorial as a template for my website). I have been changing the name from microblog to greencoin the whole time, but for some reason when I try to run Heroku it tells me the module 'greencoin' (which was formerly 'microblog.py') does not exist, although I don't know why?

    It gives me this error: 2020-05-31T15:01:36.837456+00:00 heroku[web.1]: Starting process with command gunicorn greencoin:app 2020-05-31T15:01:39.599482+00:00 app[web.1]: [2020-05-31 15:01:39 +0000] [4] [INFO] Starting gunicorn 19.7.1 2020-05-31T15:01:39.606138+00:00 app[web.1]: [2020-05-31 15:01:39 +0000] [4] [INFO] Listening at: http://0.0.0.0:27639 (4) 2020-05-31T15:01:39.606140+00:00 app[web.1]: [2020-05-31 15:01:39 +0000] [4] [INFO] Using worker: sync 2020-05-31T15:01:39.606140+00:00 app[web.1]: [2020-05-31 15:01:39 +0000] [10] [INFO] Booting worker with pid: 10 2020-05-31T15:01:39.611345+00:00 app[web.1]: [2020-05-31 15:01:39 +0000] [10] [ERROR] Exception in worker process 2020-05-31T15:01:39.611346+00:00 app[web.1]: Traceback (most recent call last): 2020-05-31T15:01:39.611357+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 578, in spawn_worker 2020-05-31T15:01:39.611359+00:00 app[web.1]: worker.init_process() 2020-05-31T15:01:39.611359+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 126, in init_process 2020-05-31T15:01:39.611359+00:00 app[web.1]: self.load_wsgi() 2020-05-31T15:01:39.611360+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 135, in load_wsgi 2020-05-31T15:01:39.611360+00:00 app[web.1]: self.wsgi = self.app.wsgi() 2020-05-31T15:01:39.611365+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi 2020-05-31T15:01:39.611366+00:00 app[web.1]: self.callable = self.load() 2020-05-31T15:01:39.611366+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 65, in load 2020-05-31T15:01:39.611366+00:00 app[web.1]: return self.load_wsgiapp() 2020-05-31T15:01:39.611366+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp 2020-05-31T15:01:39.611367+00:00 app[web.1]: return util.import_app(self.app_uri) 2020-05-31T15:01:39.611367+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 352, in import_app 2020-05-31T15:01:39.611368+00:00 app[web.1]: import(module) 2020-05-31T15:01:39.611368+00:00 app[web.1]: ModuleNotFoundError: No module named 'greencoin' 2020-05-31T15:01:39.611508+00:00 app[web.1]: [2020-05-31 15:01:39 +0000] [10] [INFO] Worker exiting (pid: 10) 2020-05-31T15:01:39.648060+00:00 app[web.1]: [2020-05-31 15:01:39 +0000] [4] [INFO] Shutting down: Master 2020-05-31T15:01:39.648173+00:00 app[web.1]: [2020-05-31 15:01:39 +0000] [4] [INFO] Reason: Worker failed to boot. 2020-05-31T15:01:39.737940+00:00 heroku[web.1]: Process exited with status 3 2020-05-31T15:01:39.780819+00:00 heroku[web.1]: State changed from starting to crashed

  • #120 Miguel Grinberg said 2020-06-01T09:02:53Z

    @Richard: does the command that you have in the Procfile run fine locally? Are you sure that you committed to git all the files that you have in your local folder? Any chance greencoin.py was not committed?

  • #121 Richard said 2020-06-01T11:24:08Z

    I've committed all of the local files: https://imgur.com/a/X83C6pT

    Even when I try again it keeps telling me the module 'greencoin' was not found? I also found no need for translations so I modified my procfile to "web:gunicorn greencoin:app".

    I also found a problem where I can't 'flask db migrate -> flask db upgrade' my database as it tells me the 'followers' table has already been defined: https://imgur.com/a/DiU5dDz

    Thanks a lot of your help!

  • #122 Miguel Grinberg said 2020-06-01T14:47:26Z

    @Richard: I can't really help, this is something that you need to debug. Clearly the greencoin.py file cannot be imported, but I can't tell you why. Try importing locally, or try gunicorn locally, as I suggested before.

  • #123 Richard said 2020-06-01T18:39:24Z

    I seem to have fixed the problem by creating my own virtual env instead of using an IDE, thanks a lot! (It seems like IntelliJ IDEA was messing with my files?)

    But I have one last question; every time flask db upgrade is run, it tries to create the follower's table again: https://imgur.com/a/DiU5dDz?

    I don't know if I missed anything in the tutorial, but it keeps giving me an error that the followers table already exists.

    Again thanks for the help!

  • #124 Miguel Grinberg said 2020-06-01T21:36:23Z

    @Richard: do you know why it wants to create this table again? Does it appear in two migrations maybe? Take a look at your scripts, if the table is being created twice remove the second instance. I can't tell you why it was added to a second migration, it shouldn't have been, unless you create that migration using an incomplete database.

  • #125 Vidhi Khathuria said 2020-06-29T14:33:14Z

    Hi Miguel. I have been following your tutorials and I tried to deploy my application on heroku, but it is only running on my local machine. Could you please tell me why ? Also how can i solve it? Thank you

Leave a Comment