ModuleNotFoundError:在将简单应用程序(通过与Atlas的pymongo连接)部署到Heroku时,烧瓶中没有名为“ 0”的模块

时间:2020-07-21 10:20:06

标签: python flask heroku pymongo

我收到ModuleNotFoundError:没有名为'0'的模块。当我在Heroku上部署一个简单的应用程序(具有与Atlas的pymongo连接)时,我不知道为什么。
任何帮助将不胜感激

我的app.py是:

from flask import Flask
from pymongo import MongoClient

app = Flask(__name__)

client = MongoClient('mongodb+srv://XXXX:XXX@XXX.wmqfv.mongodb.net/XXX?retryWrites=true&w=majority')

db = client.two

@app.route('/')
def hello_world():
    dic = {"num": 5}
    result = db.XXXX.insert_one(dic)
    print(test)
    return 'Hello, World!'
    
    
if __name__ == "__main__":
    app.run()

PROC:

网络:gunicorn 0.0.0.0/0 app:app

requirements.txt:

click == 7.1.2
dnspython == 2.0.0
Flask == 1.1.2
gunicorn == 20.0.4
itsdangerous == 1.1.0
Jinja2 == 2.11.2
MarkupSafe == 1.1.1
pymongo == 3.10.1
Werkzeug == 1.0.1

Application Logs
----------------



2020-07-21T09:53:01.292132+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-07-21T09:53:01.292132+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
2020-07-21T09:53:01.292173+00:00 app[web.1]: ModuleNotFoundError: No module named '0'
2020-07-21T09:53:01.292439+00:00 app[web.1]: [2020-07-21 09:53:01 +0000] [10] [INFO] Worker exiting (pid: 10)
2020-07-21T09:53:01.344153+00:00 app[web.1]: [2020-07-21 09:53:01 +0000] [4] [INFO] Shutting down: Master
2020-07-21T09:53:01.344425+00:00 app[web.1]: [2020-07-21 09:53:01 +0000] [4] [INFO] Reason: Worker failed to boot.
2020-07-21T09:53:01.439377+00:00 heroku[web.1]: Process exited with status 3
2020-07-21T09:53:01.479539+00:00 heroku[web.1]: State changed from starting to crashed
2020-07-21T10:00:15.789793+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=damp-sands-34174.herokuapp.com request_id=bdd1aa3b-ca94-43eb-9308-6b128d972aec fwd="176.229.243.55" dyno= connect= service= status=503 bytes= protocol=https
2020-07-21T10:00:16.399866+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=damp-sands-34174.herokuapp.com request_id=557ad907-034f-4130-833a-cf670d88e4d8 fwd="176.229.243.55" dyno= connect= service= status=503 bytes= protocol=https
2020-07-21T10:15:46.007045+00:00 heroku[web.1]: State changed from crashed to starting
2020-07-21T10:15:48.983369+00:00 heroku[web.1]: Starting process with command `gunicorn 0.0.0.0/0 app:app`
2020-07-21T10:15:51.764314+00:00 heroku[web.1]: Process exited with status 3
2020-07-21T10:15:51.795932+00:00 heroku[web.1]: State changed from starting to crashed
2020-07-21T10:15:51.648442+00:00 app[web.1]: [2020-07-21 10:15:51 +0000] [4] [INFO] Starting gunicorn 20.0.4
2020-07-21T10:15:51.648983+00:00 app[web.1]: [2020-07-21 10:15:51 +0000] [4] [INFO] Listening at: http://0.0.0.0:10888 (4)
2020-07-21T10:15:51.649088+00:00 app[web.1]: [2020-07-21 10:15:51 +0000] [4] [INFO] Using worker: sync
2020-07-21T10:15:51.653204+00:00 app[web.1]: [2020-07-21 10:15:51 +0000] [10] [INFO] Booting worker with pid: 10
2020-07-21T10:15:51.658648+00:00 app[web.1]: [2020-07-21 10:15:51 +0000] [10] [ERROR] Exception in worker process
2020-07-21T10:15:51.658649+00:00 app[web.1]: Traceback (most recent call last):
2020-07-21T10:15:51.658649+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-07-21T10:15:51.658650+00:00 app[web.1]:     worker.init_process()
2020-07-21T10:15:51.658650+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 119, in init_process
2020-07-21T10:15:51.658650+00:00 app[web.1]:     self.load_wsgi()
2020-07-21T10:15:51.658650+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
2020-07-21T10:15:51.658651+00:00 app[web.1]:     self.wsgi = self.app.wsgi()
2020-07-21T10:15:51.658651+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-07-21T10:15:51.658651+00:00 app[web.1]:     self.callable = self.load()
2020-07-21T10:15:51.658651+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2020-07-21T10:15:51.658652+00:00 app[web.1]:     return self.load_wsgiapp()
2020-07-21T10:15:51.658652+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
2020-07-21T10:15:51.658652+00:00 app[web.1]:     return util.import_app(self.app_uri)
2020-07-21T10:15:51.658653+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 358, in import_app
2020-07-21T10:15:51.658653+00:00 app[web.1]:     mod = importlib.import_module(module)
2020-07-21T10:15:51.658653+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
2020-07-21T10:15:51.658653+00:00 app[web.1]:     return _bootstrap._gcd_import(name[level:], package, level)
2020-07-21T10:15:51.658653+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2020-07-21T10:15:51.658654+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-07-21T10:15:51.658654+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
2020-07-21T10:15:51.658654+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2020-07-21T10:15:51.658654+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2020-07-21T10:15:51.658654+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-07-21T10:15:51.658655+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
2020-07-21T10:15:51.658655+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2020-07-21T10:15:51.658655+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2020-07-21T10:15:51.658655+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-07-21T10:15:51.658655+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
2020-07-21T10:15:51.658655+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2020-07-21T10:15:51.658655+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2020-07-21T10:15:51.658656+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-07-21T10:15:51.658656+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
2020-07-21T10:15:51.658659+00:00 app[web.1]: ModuleNotFoundError: No module named '0'
2020-07-21T10:15:51.658811+00:00 app[web.1]: [2020-07-21 10:15:51 +0000] [10] [INFO] Worker exiting (pid: 10)
2020-07-21T10:15:51.686011+00:00 app[web.1]: [2020-07-21 10:15:51 +0000] [4] [INFO] Shutting down: Master
2020-07-21T10:15:51.686111+00:00 app[web.1]: [2020-07-21 10:15:51 +0000] [4] [INFO] Reason: Worker failed to boot.
**

PROC FILE(根据建议更新):

网站:gunicorn app:app

Application Logs

2020-07-21T19:29:58.286330+00:00 app[web.1]: [2020-07-21 19:29:58 +0000] [10] [INFO] Worker exiting (pid: 10)
2020-07-21T19:29:58.687285+00:00 app[web.1]: [2020-07-21 19:29:58 +0000] [4] [INFO] Shutting down: Master
2020-07-21T19:29:58.796272+00:00 heroku[web.1]: Process exited with status 0
2020-07-21T19:30:00.333130+00:00 heroku[web.1]: Starting process with command `gunicorn app:app`
2020-07-21T19:30:03.005143+00:00 app[web.1]: [2020-07-21 19:30:03 +0000] [4] [INFO] Starting gunicorn 20.0.4
2020-07-21T19:30:03.011621+00:00 app[web.1]: [2020-07-21 19:30:03 +0000] [4] [INFO] Listening at: http://0.0.0.0:28266 (4)
2020-07-21T19:30:03.011794+00:00 app[web.1]: [2020-07-21 19:30:03 +0000] [4] [INFO] Using worker: sync
2020-07-21T19:30:03.020734+00:00 app[web.1]: [2020-07-21 19:30:03 +0000] [10] [INFO] Booting worker with pid: 10
2020-07-21T19:30:03.022633+00:00 app[web.1]: [2020-07-21 19:30:03 +0000] [11] [INFO] Booting worker with pid: 11
2020-07-21T19:30:03.450232+00:00 heroku[web.1]: State changed from starting to up
2020-07-21T19:30:36.257561+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/" host=gentle-hamlet-65639.herokuapp.com request_id=84e1107d-7dbe-4353-b560-e0d4523d1ce1 fwd="176.229.243.55" dyno=web.1 connect=1ms service=30004ms status=503 bytes=0 protocol=https
2020-07-21T19:30:36.336291+00:00 app[web.1]: [2020-07-21 19:30:36,331] ERROR in app: Exception on / [GET]
2020-07-21T19:30:36.336299+00:00 app[web.1]: Traceback (most recent call last):
2020-07-21T19:30:36.336300+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
2020-07-21T19:30:36.336301+00:00 app[web.1]:     response = self.full_dispatch_request()
2020-07-21T19:30:36.336301+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
2020-07-21T19:30:36.336301+00:00 app[web.1]:     rv = self.handle_user_exception(e)
2020-07-21T19:30:36.336302+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
2020-07-21T19:30:36.336302+00:00 app[web.1]:     reraise(exc_type, exc_value, tb)
2020-07-21T19:30:36.336303+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
2020-07-21T19:30:36.336303+00:00 app[web.1]:     raise value
2020-07-21T19:30:36.336320+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
2020-07-21T19:30:36.336321+00:00 app[web.1]:     rv = self.dispatch_request()
2020-07-21T19:30:36.336321+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
2020-07-21T19:30:36.336322+00:00 app[web.1]:     return self.view_functions[rule.endpoint](**req.view_args)
2020-07-21T19:30:36.336322+00:00 app[web.1]:   File "/app/app.py", line 14, in hello_world
2020-07-21T19:30:36.336323+00:00 app[web.1]:     result = db.Mirzakandov.insert_one(dic)
2020-07-21T19:30:36.336323+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/pymongo/collection.py", line 698, in insert_one
2020-07-21T19:30:36.336324+00:00 app[web.1]:     session=session),
2020-07-21T19:30:36.336324+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/pymongo/collection.py", line 612, in _insert
2020-07-21T19:30:36.336325+00:00 app[web.1]:     bypass_doc_val, session)
2020-07-21T19:30:36.336325+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/pymongo/collection.py", line 600, in _insert_one
2020-07-21T19:30:36.336325+00:00 app[web.1]:     acknowledged, _insert_command, session)
2020-07-21T19:30:36.336326+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1490, in _retryable_write
2020-07-21T19:30:36.336326+00:00 app[web.1]:     with self._tmp_session(session) as s:
2020-07-21T19:30:36.336327+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/contextlib.py", line 81, in __enter__
2020-07-21T19:30:36.336327+00:00 app[web.1]:     return next(self.gen)
2020-07-21T19:30:36.336328+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1823, in _tmp_session
2020-07-21T19:30:36.336328+00:00 app[web.1]:     s = self._ensure_session(session)
2020-07-21T19:30:36.336329+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1810, in _ensure_session
2020-07-21T19:30:36.336329+00:00 app[web.1]:     return self.__start_session(True, causal_consistency=False)
2020-07-21T19:30:36.336330+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1763, in __start_session
2020-07-21T19:30:36.336330+00:00 app[web.1]:     server_session = self._get_server_session()
2020-07-21T19:30:36.336330+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1796, in _get_server_session
2020-07-21T19:30:36.336331+00:00 app[web.1]:     return self._topology.get_server_session()
2020-07-21T19:30:36.336331+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/pymongo/topology.py", line 490, in get_server_session
2020-07-21T19:30:36.336331+00:00 app[web.1]:     None)
2020-07-21T19:30:36.336332+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/pymongo/topology.py", line 209, in _select_servers_loop
2020-07-21T19:30:36.336332+00:00 app[web.1]:     self._error_message(selector))
2020-07-21T19:30:36.336339+00:00 app[web.1]: pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed
2020-07-21T19:30:36.337739+00:00 app[web.1]: 10.65.248.108 - - [21/Jul/2020:19:30:36 +0000] "GET / HTTP/1.1" 500 290 "https://dashboard.heroku.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"

1 个答案:

答案 0 :(得分:0)

您没有正确使用Gunicorn。这些是其用法说明:

usage: gunicorn [OPTIONS] [APP_MODULE]

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  -c CONFIG, --config CONFIG
                        The Gunicorn config file. [None]
  -b ADDRESS, --bind ADDRESS
                        The socket to bind. [['127.0.0.1:8000']]
  --backlog INT         The maximum number of pending connections. [2048]
  -w INT, --workers INT
                        The number of worker processes for handling requests.
                        [1]
  -k STRING, --worker-class STRING
                        The type of workers to use. [sync]
  --threads INT         The number of worker threads for handling requests.
                        [1]
  --worker-connections INT
                        The maximum number of simultaneous clients. [1000]
  --max-requests INT    The maximum number of requests a worker will process
                        before restarting. [0]
  --max-requests-jitter INT
                        The maximum jitter to add to the *max_requests*
                        setting. [0]
  -t INT, --timeout INT
                        Workers silent for more than this many seconds are
                        killed and restarted. [30]
  --graceful-timeout INT
                        Timeout for graceful workers restart. [30]
  --keep-alive INT      The number of seconds to wait for requests on a Keep-
                        Alive connection. [2]
  --limit-request-line INT
                        The maximum size of HTTP request line in bytes. [4094]
  --limit-request-fields INT
                        Limit the number of HTTP headers fields in a request.
                        [100]
  --limit-request-field_size INT
                        Limit the allowed size of an HTTP request header
                        field. [8190]
  --reload              Restart workers when code changes. [False]
  --reload-engine STRING
                        The implementation that should be used to power
                        :ref:`reload`. [auto]
  --reload-extra-file FILES
                        Extends :ref:`reload` option to also watch and reload
                        on additional files [[]]
  --spew                Install a trace function that spews every line
                        executed by the server. [False]
  --check-config        Check the configuration. [False]
  --preload             Load application code before the worker processes are
                        forked. [False]
  --no-sendfile         Disables the use of ``sendfile()``. [None]
  --reuse-port          Set the ``SO_REUSEPORT`` flag on the listening socket.
                        [False]
  --chdir CHDIR         Chdir to specified directory before apps loading.
                        [/home/kkinder]
  -D, --daemon          Daemonize the Gunicorn process. [False]
  -e ENV, --env ENV     Set environment variable (key=value). [[]]
  -p FILE, --pid FILE   A filename to use for the PID file. [None]
  --worker-tmp-dir DIR  A directory to use for the worker heartbeat temporary
                        file. [None]
  -u USER, --user USER  Switch worker processes to run as this user. [1000]
  -g GROUP, --group GROUP
                        Switch worker process to run as this group. [1000]
  -m INT, --umask INT   A bit mask for the file mode on files written by
                        Gunicorn. [0]
  --initgroups          If true, set the worker process's group access list
                        with all of the [False]
  --forwarded-allow-ips STRING
                        Front-end's IPs from which allowed to handle set
                        secure headers. [127.0.0.1]
  --access-logfile FILE
                        The Access log file to write to. [None]
  --disable-redirect-access-to-syslog
                        Disable redirect access logs to syslog. [False]
  --access-logformat STRING
                        The access log format. [%(h)s %(l)s %(u)s %(t)s
                        "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"]
  --error-logfile FILE, --log-file FILE
                        The Error log file to write to. [-]
  --log-level LEVEL     The granularity of Error log outputs. [info]
  --capture-output      Redirect stdout/stderr to specified file in
                        :ref:`errorlog`. [False]
  --logger-class STRING
                        The logger you want to use to log events in Gunicorn.
                        [gunicorn.glogging.Logger]
  --log-config FILE     The log config file to use. [None]
  --log-config-dict LOGCONFIG_DICT
                        The log config dictionary to use, using the standard
                        Python [{}]
  --log-syslog-to SYSLOG_ADDR
                        Address to send syslog messages. [udp://localhost:514]
  --log-syslog          Send *Gunicorn* logs to syslog. [False]
  --log-syslog-prefix SYSLOG_PREFIX
                        Makes Gunicorn use the parameter as program-name in
                        the syslog entries. [None]
  --log-syslog-facility SYSLOG_FACILITY
                        Syslog facility name [user]
  -R, --enable-stdio-inheritance
                        Enable stdio inheritance. [False]
  --statsd-host STATSD_ADDR
                        ``host:port`` of the statsd server to log to. [None]
  --dogstatsd-tags DOGSTATSD_TAGS
                        A comma-delimited list of datadog statsd (dogstatsd)
                        tags to append to statsd metrics. []
  --statsd-prefix STATSD_PREFIX
                        Prefix to use when emitting statsd metrics (a trailing
                        ``.`` is added, []
  -n STRING, --name STRING
                        A base to use with setproctitle for process naming.
                        [None]
  --pythonpath STRING   A comma-separated list of directories to add to the
                        Python path. [None]
  --paste STRING, --paster STRING
                        Load a PasteDeploy config file. The argument may
                        contain a ``#`` [None]
  --proxy-protocol      Enable detect PROXY protocol (PROXY mode). [False]
  --proxy-allow-from PROXY_ALLOW_IPS
                        Front-end's IPs from which allowed accept proxy
                        requests (comma separate). [127.0.0.1]
  --keyfile FILE        SSL key file [None]
  --certfile FILE       SSL certificate file [None]
  --ssl-version SSL_VERSION
                        SSL version to use. [_SSLMethod.PROTOCOL_TLS]
  --cert-reqs CERT_REQS
                        Whether client certificate is required (see stdlib ssl
                        module's) [VerifyMode.CERT_NONE]
  --ca-certs FILE       CA certificates file [None]
  --suppress-ragged-eofs
                        Suppress ragged EOFs (see stdlib ssl module's) [True]
  --do-handshake-on-connect
                        Whether to perform SSL handshake on socket connect
                        (see stdlib ssl module's) [False]
  --ciphers CIPHERS     SSL Cipher suite to use, in the format of an OpenSSL
                        cipher list. [None]
  --paste-global CONF   Set a PasteDeploy global config variable in
                        ``key=value`` form. [[]]
  --strip-header-spaces
                        Strip spaces present between the header name and the
                        the ``:``. [False]

因此,gunicorn的第一个参数是您要运行的模块。如果您想听所有地址,则可以使用--bind-b

gunicorn -b 0.0.0.0:8080 app:app

但这在Heroku中并不是真正必要的,因为环境变量告诉gunicorn要绑定的地址。您可以这样做:

gunicorn app:app