0xRLA
0xRLA

Reputation: 3369

Heroku flask application deploy: ImportError: No module named

I am following this guide to deploy a python flask application on heroku: https://github.com/datademofun/heroku-basic-flask

I setup my environment by this guide: http://docs.python-guide.org/en/latest/dev/virtualenvs/

The problem is that my custom module (platforms/hitta.py) fail to import when running the application with gunicorn instead of just python3 app.py. Why is there a difference and how do I solve this?

I have only problem importing my own modules, not from requirements.txt:

flask
flask_restplus
colorama
requests
gunicorn
selenium
apiclient
http
oauth2client

File structure (simplified):

Project
 |_app.py
 |_platforms/hitta.py

Command:

Sunes-MacBook-Pro:my-app x$ heroku local web

Output:

[OKAY] Loaded ENV .env File as KEY=VALUE Format
11:41:09 PM web.1 |  [2018-07-12 23:41:09 +0200] [67332] [INFO] Starting 
gunicorn 19.9.0
11:41:09 PM web.1 |  [2018-07-12 23:41:09 +0200] [67332] [INFO] 
Listening at: http://0.0.0.0:5000 (67332)
11:41:09 PM web.1 |  [2018-07-12 23:41:09 +0200] [67332] [INFO] Using 
worker: sync
11:41:09 PM web.1 |  [2018-07-12 23:41:09 +0200] [67335] [INFO] Booting 
worker with pid: 67335
11:41:09 PM web.1 |  [2018-07-12 23:41:09 +0200] [67335] [ERROR] 
Exception in worker process
11:41:09 PM web.1 |  Traceback (most recent call last):
11:41:09 PM web.1 |    File "/Library/Python/2.7/site- 
packages/gunicorn/arbiter.py", line 583, in spawn_worker
11:41:09 PM web.1 |      worker.init_process()
11:41:09 PM web.1 |    File "/Library/Python/2.7/site-                
packages/gunicorn/workers/base.py", line 129, in init_process
11:41:09 PM web.1 |      self.load_wsgi()
11:41:09 PM web.1 |    File "/Library/Python/2.7/site- 
packages/gunicorn/workers/base.py", line 138, in load_wsgi
11:41:09 PM web.1 |      self.wsgi = self.app.wsgi()
11:41:09 PM web.1 |    File "/Library/Python/2.7/site- 
packages/gunicorn/app/base.py", line 67, in wsgi
11:41:09 PM web.1 |      self.callable = self.load()
11:41:09 PM web.1 |    File "/Library/Python/2.7/site- 
packages/gunicorn/app/wsgiapp.py", line 52, in load
11:41:09 PM web.1 |      return self.load_wsgiapp()
11:41:09 PM web.1 |    File "/Library/Python/2.7/site- 
packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
11:41:09 PM web.1 |      return util.import_app(self.app_uri)
11:41:09 PM web.1 |    File "/Library/Python/2.7/site- 
packages/gunicorn/util.py", line 350, in import_app
11:41:09 PM web.1 |      __import__(module)
11:41:09 PM web.1 |    File "/Users/x/Dev/projects/my-app/app.py", 
line 9, in <module>
11:41:09 PM web.1 |      from platforms.hitta import Hitta
11:41:09 PM web.1 |  ImportError: No module named platforms.hitta
11:41:09 PM web.1 |  [2018-07-12 23:41:09 +0200] [67335] [INFO] Worker 
exiting (pid: 67335)
11:41:09 PM web.1 |  [2018-07-12 23:41:09 +0200] [67332] [INFO] Shutting 
down: Master
11:41:09 PM web.1 |  [2018-07-12 23:41:09 +0200] [67332] [INFO] Reason: 
Worker failed to boot.
11:41:09 PM web.1 Exited with exit code 3

Upvotes: 1

Views: 822

Answers (1)

Grey Li
Grey Li

Reputation: 12762

You need to create a __init__.py in a folder to make it become a Python package, then you can import modules inside it with from platforms.hitta import Hitta. The file structure may like this:

Project / 
    | app.py
    | platforms / 
        | __init__.py
        | hitta.py

P.S. The __init__.py's content can be empty.

Upvotes: 2

Related Questions