【问题标题】:ImportError: No module named main in GAE FlexibleImportError:GAE Flexible 中没有名为 main 的模块
【发布时间】:2018-08-18 00:36:59
【问题描述】:

我正在为 python 使用 apache beam(python 版本 2.7),当我将代码上传到 Google App Engine Flexible 时,我总是收到错误:ImportError: No module named main。当我调用端点/server 时,我可以在数据流控制台中看到此错误。 当我在本地执行我的代码时,它在我的 gcloud 数据流中完美运行,但是当我在 GAE Flex 中执行它时,我得到了上面指定的错误。

这是我的代码:

import apache_beam as beam
import logging
logging.basicConfig(level=logging.DEBUG)

from flask import Flask

from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.options.pipeline_options import StandardOptions, SetupOptions
from apache_beam.options.pipeline_options import GoogleCloudOptions

from apache_beam.io import WriteToText
from apache_beam.io import ReadFromText


PROJECT_ID = 'PROJECT_ID'
JOB_NAME = 'test-job-name-l'
BUCKET_URL = 'gs://backup-bucket'
app = Flask(__name__)

@app.route('/')
def start():
    return "Welcome to datamigration"

@app.route('/server')
def start1():
    run()
    return "It works"

class FindWords(beam.DoFn):
    def process(self, element):
        import re as regex
        return regex.findall(r"[A-Za-z\']+", element)

class CountWordsTransform(beam.PTransform):
    def expand(self, p_collection):
        return (p_collection
                | "Split" >> (beam.ParDo(FindWords()).with_input_types(unicode))
                | "PairWithOne" >> beam.Map(lambda word: (word, 1))
                | "GroupBy" >> beam.GroupByKey()
                | "AggregateGroups" >> beam.Map(lambda (word, ones): (word, sum(ones))))

def run():
    pipeline_options = PipelineOptions()
    pipeline_options.view_as(SetupOptions).save_main_session = True
    pipeline_options.view_as(
        SetupOptions).requirements_file = "requirements.txt"
    google_cloud_options = pipeline_options.view_as(GoogleCloudOptions)
    google_cloud_options.project = PROJECT_ID
    google_cloud_options.job_name = JOB_NAME
    google_cloud_options.staging_location = BUCKET_URL + '/staging'
    google_cloud_options.temp_location = BUCKET_URL + '/temp'
    pipeline_options.view_as(StandardOptions).runner = 'DataflowRunner'
    pipeline = beam.Pipeline(options=pipeline_options)

    (pipeline
     | "Load" >> ReadFromText(BUCKET_URL + "/file.txt")
     | "Count Words" >> CountWordsTransform()
     | "Save" >> WriteToText(BUCKET_URL + '/result/test')
     )

    pipeline.run()

if __name__ == '__main__':
    app.run(port=8080, debug=True)

这是我总是得到的完整错误:

Error:
Dataflow pipeline failed. State: FAILED, Error:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/dataflow_worker/batchworker.py", line 642, in do_work
    work_executor.execute()
  File "/usr/local/lib/python2.7/dist-packages/dataflow_worker/executor.py", line 156, in execute
    op.start()
  File "apache_beam/runners/worker/operations.py", line 351, in apache_beam.runners.worker.operations.DoOperation.start
    def start(self):
  File "apache_beam/runners/worker/operations.py", line 352, in apache_beam.runners.worker.operations.DoOperation.start
    with self.scoped_start_state:
  File "apache_beam/runners/worker/operations.py", line 357, in apache_beam.runners.worker.operations.DoOperation.start
    pickler.loads(self.spec.serialized_fn))
  File "/usr/local/lib/python2.7/dist-packages/apache_beam/internal/pickler.py", line 232, in loads
    return dill.loads(s)
  File "/usr/local/lib/python2.7/dist-packages/dill/dill.py", line 277, in loads
    return load(file)
  File "/usr/local/lib/python2.7/dist-packages/dill/dill.py", line 266, in load
    obj = pik.load()
  File "/usr/lib/python2.7/pickle.py", line 864, in load
    dispatch[key](self)
  File "/usr/lib/python2.7/pickle.py", line 1096, in load_global
    klass = self.find_class(module, name)
  File "/usr/local/lib/python2.7/dist-packages/dill/dill.py", line 423, in find_class
    return StockUnpickler.find_class(self, module, name)
  File "/usr/lib/python2.7/pickle.py", line 1130, in find_class
    __import__(module)
ImportError: No module named main

我的 app.yaml:

runtime: python
env: flex
service: ms-somename
threadsafe: true

entrypoint: gunicorn -b :$PORT main:app

runtime_config:
  python_version: 2

manual_scaling:
  instances: 1

resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

还有我的 requirements.txt

google-cloud-datastore==1.3.0
google-cloud-dataflow==2.5.0
google-apitools==0.5.16
googledatastore==7.0.1
apache-beam==2.5.0
apache-beam[gcp]==2.5.0
Flask==0.12.2
gunicorn==19.9.0

【问题讨论】:

    标签: python google-cloud-platform apache-beam dataflow app-engine-flexible


    【解决方案1】:

    实际上我在几天前遇到了这个问题,但我试图在 GAE 标准 python 3.7 上运行它。话虽如此,我通过在我的 requirements.txt 文件中包含 gunicorn 解决了我的问题。最初我没有,因为我误解了文档中的这一行:

    Do not include gunicorn in your requirements.txt file unless you are specifying the entrypoint.

    https://cloud.google.com/appengine/docs/standard/python3/runtime

    这同样适用于 GAE 标准。

    【讨论】:

      【解决方案2】:

      您的代码在main.py 文件中吗?

      在您的 yaml 文件中,

      entrypoint: gunicorn -b :$PORT main:app
      

      告诉 gunicorn 在main 模块中寻找@​​987654324@ 变量(更多信息here)。如果没有main.py,会报错。

      【讨论】:

      • 是的,我的代码在一个名为 main.py 的文件中。但我注意到,当我上传它时,控制台总是给我下一个错误,我认为这可能是问题所在: 步骤 #1:您使用的是 pip 版本 9.0.3,但是版本 18.0 可用。第 1 步:您应该考虑通过“pip install --upgrade pip”命令进行升级。但是当我看到 pip 版本时,它已更新(版本 18.0),所以我不知道为什么会显示此消息。我确信这可能是问题所在。我正在寻找如何在 GAE Flex 中安装所有软件包时升级 pip 版本,但我找不到任何东西
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-04
      • 1970-01-01
      • 2013-03-21
      相关资源
      最近更新 更多