0
def main(s):
    with open('pipe.dat', 'rb') as fp:
        pipe = pickle.load(fp)

这个python代码出错

{
  "errorMessage": "Can't get attribute 'tokenize' on <module '__main__' from '/var/runtime/bootstrap'>",
  "errorType": "AttributeError",
  "stackTrace": [
    "  File \"/var/lang/lib/python3.8/imp.py\", line 234, in load_module\n    return load_source(name, filename, file)\n",
    "  File \"/var/lang/lib/python3.8/imp.py\", line 171, in load_source\n    module = _load(spec)\n",
    "  File \"<frozen importlib._bootstrap>\", line 702, in _load\n",
    "  File \"<frozen importlib._bootstrap>\", line 671, in _load_unlocked\n",
    "  File \"<frozen importlib._bootstrap_external>\", line 783, in exec_module\n",
    "  File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed\n",
    "  File \"/var/task/hand.py\", line 2, in <module>\n    import SentimentModeling\n",
    "  File \"/var/task/SentimentModeling.py\", line 72, in <module>\n    event = main(s)\n",
    "  File \"/var/task/SentimentModeling.py\", line 37, in main\n    pipe = pickle.load(fp)\n"
  ]
}

我无法处理这些错误。

我制作了关于模块的层:sklearn numpy joblib 使用辣和 numpy(由 lambda 提供)lambda python 和模块的版本是 3.8

并使 pipe.dat 为

pipe = Pipeline([('vect', tfidf), ('cif', logistic)])
pipe.fit(train_x, train_y)

predict_y = pipe.predict(test_x)
print(accuracy_score(test_y, predict_y)*100)
print(classification_report(test_y, predict_y))

with open('pipe.dat', 'wb') as fp:
    pickle.dump(pipe, fp)
4

1 回答 1

0

我在使用 python 3.8 和 pickle 和 lambda 时遇到了同样的问题。

我将运行时更改为 3.6,pickle 开始工作。

我还使用了 .pkl 文件,而不是 .dat。

def load_pickle_tfidf_vocab():
  with open("tfidf_vocab.pkl", "rb") as f:
    raw_data_tfidf = f.read()
  tfidf_vocab = pickle.loads(raw_data_tfidf)
  return tfidf_vocab

我还在为部署依赖打包而苦苦挣扎,所以我切换到了 cloud9 IDE。我 pip 将所有内容安装到文件夹中,然后从 IDE 本身自动部署。

于 2020-06-28T21:13:57.847 回答