【问题标题】:Google app engine deployment fails- Error while finding module specification for 'pip' (AttributeError: module '__main__' has no attribute '__file__')Google 应用引擎部署失败 - 查找“pip”的模块规范时出错(AttributeError:模块“__main__”没有属性“__file__”)
【发布时间】:2022-01-06 04:00:18
【问题描述】:

我们正在使用命令提示符c:\gcloud app deploy app.yaml,但收到以下错误:

Running "python3 -m pip install --requirement requirements.txt --upgrade --upgrade-strategy only-if-needed --no-warn-script-location --no-warn-conflicts --force-reinstall --no-compile (PIP_CACHE_DIR=/layers/google.python.pip/pipcache PIP_DISABLE_PIP_VERSION_CHECK=1)"
Step #2 - "build": /layers/google.python.pip/pip/bin/python3: Error while finding module specification for 'pip' (AttributeError: module '__main__' has no attribute '__file__')
Step #2 - "build": Done "python3 -m pip install --requirement requirements.txt --upgr..." (34.49892ms)
Step #2 - "build": Failure: (ID: 0ea8a540) /layers/google.python.pip/pip/bin/python3: Error while finding module specification for 'pip' (AttributeError: module '__main__' has no attribute '__file__')
Step #2 - "build": --------------------------------------------------------------------------------
Step #2 - "build": Running "mv -f /builder/outputs/output-5577006791947779410 /builder/outputs/output"
Step #2 - "build": Done "mv -f /builder/outputs/output-5577006791947779410 /builder/o..." (12.758866ms)
Step #2 - "build": ERROR: failed to build: exit status 1
Finished Step #2 - "build"
ERROR
ERROR: build step 2 "us.gcr.io/gae-runtimes/buildpacks/python37/builder:python37_20211201_3_7_12_RC00" failed: step exited with non-zero status: 145

我们的Requirements.txt 如下。我们目前使用的是 Python 3.7 标准应用引擎

firebase_admin==3.0.0
sendgrid==6.9.3
google-auth==1.35.0
google-auth-httplib2==0.1.0
jinja2==3.0.3
MarkupSafe==2.0.1
pytz==2021.3
Flask==2.0.2
twilio==6.46.0
httplib2==0.20.2
requests==2.24.0
requests_toolbelt==0.9.1
google-cloud-tasks==2.7.1
google-cloud-logging==1.15.1
googleapis-common-protos==1.54.0

请帮忙。在更新 requirements.txt 文件之前,上面的代码运行良好。根据文档here,我们尝试删除 gunicorn 以允许系统获取最新版本。

我们有一个子目录结构,它将所有 .py 文件存储在控制器中,并将 db 定义存储在模型中。我们的 main.py 有以下 -

sys.path.append(os.path.join(os.path.dirname(__file__), '../controllers'))
sys.path.append(os.path.join(os.path.dirname(__file__), '../models'))

有谁知道如何调试此错误 - Error while finding module specification for 'pip' (AttributeError: module '__main__' has no attribute '__file__')。这是什么意思?

【问题讨论】:

  • 同时分享您的 main.py、app.yaml 文件,以便我重现您的问题。也尝试重新安装 pip 版本并再次运行应用程序。

标签: python google-app-engine google-app-engine-python


【解决方案1】:

setuptools 的 60.3.0 版本存在导致上述 AttributeError 的错误,请参阅:https://github.com/pypa/setuptools/issues/3002

从那时起它就被 Pypi 拉走了:

Yanked 文件总是被忽略,除非它们是唯一匹配版本说明符的文件,该说明符使用 ==(没有任何使其成为范围的修饰符,例如 .*)或 ==“固定”到确切版本=。否则,应按照 PEP 440 对本地版本、零填充等内容匹配此版本说明符。

来源:https://www.python.org/dev/peps/pep-0592/

到目前为止,您可以做的是明确传递 setuptools 包的版本,以使用例如版本 60.2.0 或 60.3.1。

【讨论】:

  • 那么最新版的setuptools有bug,还是怎么回事?
  • 我将如何在 virtualenv 中执行此操作(即在 tox 中)?环境一创建,pip 就坏掉了……
  • @Alex,是的 setuptools 版本 60.3.0 有一个 bug => github.com/pypa/setuptools/issues/3002,你需要升级到 60.3.1
【解决方案2】:

我在部署 Google Cloud Function 时遇到了同样的问题。错误

云函数在查找“pip”的模块规范时出错(AttributeError:模块“ma​​in”没有属性“file”);错误 ID:c84b3231

在注释掉 requirements.txt 中的一些包后出现,但这并不重要,很可能不是导致它的原因。我想这更多是 Google 存储不稳定的问题,因为我正在处理的同一个 Cloud Function 在一段时间之前已经丢失了它的存档,突然间,不知从何而来,显示:

在存储位置云功能中找不到存档

我没有删除或更改任何可能解释这一点的内容,正如Archive not found in the storage location: Google Function 所建议的那样。 虽然这个答案有一个非常有趣的猜测,至少可以解释第一次出现“找不到存档”错误并因此导致 CF 不稳定:我可能在浏览 Google 时更改了存储桶的时区城市贮存。太久以前了,但我知道我浏览过 GS,因此,我不能排除这一点。 Quote:“如果GCS存储桶的区域与您的云功能区域不匹配,也可能发生[未找到存档错误]。”

在“找不到存档”崩溃后,我手动添加了main.pyrequirements.txt,并用备份中的代码再次填充它们。这工作了一段时间,但谷歌存储似乎存在一些普遍的不稳定性。因此,请始终保留已部署脚本的备份。

然后,在那个已经不稳定的Cloud Function中得到这个问题的pip错误后,等了一两天,Google Function又出现了

在存储位置云功能中找不到存档

如果您在 Cloud Function 中遇到此 pip 错误,您可能会考虑 updating pip in the "requirements.txt" 但如果您处于如此不稳定的 Cloud Function 中,更好的解决方法似乎是 创建一个新的 Cloud Function 并复制里面的所有东西。

pip 错误可能只是表明源脚本(在本例中为 requirements.txt)无法运行,因为源代码不再完全嵌入或在 Google 存储中丢失了一些嵌入。

或者你给那个云函数第二次机会和edit,转到Source选项卡,点击下拉Source code选择Inline Editor并手动添加main.pyrequirements.txtRuntime:蟒蛇)。

【讨论】:

    【解决方案3】:

    在安装 pip-tools 之前安装 setuptools 的早期版本 60.2.0 帮助我让它运行。

    pip install setuptools==60.2.0

    【讨论】:

    • 如何在tox 中安装特定版本?
    • 我的基于 tox 的测试也有问题...
    【解决方案4】:

    您的 setuptools 版本可能会被删除:

    https://pypi.org/project/setuptools/60.3.0/

    不知道如何在没有工作点的情况下解决这个问题。

    【讨论】:

      【解决方案5】:

      Setuptools 60.3.1 is out now。 GitHub链接here.

      【讨论】:

        猜你喜欢
        • 2021-09-25
        • 2018-11-18
        • 2019-09-14
        • 2022-11-19
        • 1970-01-01
        • 2017-12-19
        • 1970-01-01
        • 1970-01-01
        • 2015-05-16
        相关资源
        最近更新 更多