首页 > 解决方案 > AttributeError:模块“x”没有属性“y”

问题描述

我正在尝试将 python 函数移至 Google Cloud Functions,但我似乎遇到了别名问题。

我有一个模块,它的 init 中有以下内容;

import tweetBot.generator, tweetBot.poster

# Add aliases when this module is imported
tweetBot.ebooksGen = tweetBot.generator.ebooksGen
tweetBot.genreGen = tweetBot.generator.genreGen
tweetBot.variantGen = tweetBot.generator.variantGen

tweetBot.postTweet = tweetBot.poster.postTweet

然后用一个简单的import tweetBot.

在我的本地机器上运行它,它运行良好,并且该功能绝对没有问题。输出都是正确的,一切都很好。

但是,使用 Google Cloud Functions 运行此程序时,出现以下错误;

AttributeError: module 'tweetBot' has no attribute 'genreGen'

这对我来说绝对没有意义,因为它是在导入中绝对定义的。此外,我知道这是可行的,因为它在我的本地机器上从完全相同的仓库中运行得非常好。

https://github.com/Jademalo/JadeBots/tree/gcp-functions

忽略代码很糟糕,至少它可以工作。我根本不明白为什么我在 Google Cloud 上运行它时会出现属性错误,而它在本地绝对没问题。

谢谢


编辑:用原件替换别名的调用后(例如tweetBot.generator.genreGen,我仍然遇到同样的问题;

  File "/workspace/JadeBots.py", line 18, in postGenreDefining
    tweetText, altGenreGameDebug, altGenreExtraDebug, gameText, genreText, altPostDebug = tweetBot.generator.genreGen(gameFile, genreFile, genreExtraFile, altPostFreq, altGenreGameFreq, altGenreExtraFreq)
AttributeError: module 'tweetBot' has no attribute 'generator' 

标签: pythongoogle-cloud-platform

解决方案


创建函数时,Google Cloud Functions 似乎没有下载子模块。

下载源代码导致子模块文件夹为空,这就解释了为什么会发生 arrtibute 错误。


推荐阅读