简体   繁体   English

gcloud ml-engine local predict RuntimeError:.pyc 文件中的错误幻数

[英]gcloud ml-engine local predict RuntimeError: Bad magic number in .pyc file

My objective is to make predictions on google cloud ml engine.我的目标是对谷歌云机器学习引擎进行预测。

I installed gcloud sdk on linux ubuntu 16.04LT following Google instructions .我按照Google 说明在 linux ubuntu 16.04LT 上安装了 gcloud sdk。 I already have a machine learning trained model.我已经有一个机器学习训练模型。 I using python version anaconda python 3.5.我使用 python 版本 anaconda python 3.5。

I run:我跑:

gcloud ml-engine local predict --model-dir={MY_MODEL_DIR} --json-instances={MY_INPUT_JSON_INSTANCE}

I received the message: ERROR:我收到消息:错误:

(gcloud.ml-engine.local.predict) RuntimeError: Bad magic number in .pyc file (gcloud.ml-engine.local.predict) RuntimeError: .pyc 文件中的错误幻数

Below is all the stack trace:以下是所有堆栈跟踪:

DEBUG: (gcloud.ml-engine.local.predict) RuntimeError: Bad magic number in .pyc file
Traceback (most recent call last):
  File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/calliope/cli.py", line 797, in Execute
    resources = calliope_command.Run(cli=self, args=args)
  File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/calliope/backend.py", line 757, in Run
    resources = command_instance.Run(args)
  File "/usr/lib/google-cloud-sdk/lib/surface/ml_engine/local/predict.py", line 65, in Run
    args.text_instances)
  File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/command_lib/ml_engine/local_utils.py", line 89, in RunPredict
    raise LocalPredictRuntimeError(err)
LocalPredictRuntimeError: RuntimeError: Bad magic number in .pyc file
ERROR: (gcloud.ml-engine.local.predict) RuntimeError: Bad magic number in .pyc file
Evaluation ended**

actually it works with python3, you just need to delete the pyc files in google cloud folders, so the prediction call can compile them with python3.实际上它适用于python3,你只需要删除谷歌云文件夹中的pyc文件,这样预测调用就可以用python3编译它们。

to know the location of the pyc files , i did enable the flag --verbosity debug in the prediction call:要知道 pyc 文件的位置,我确实在预测调用中启用了--verbosity debug标志:

gcloud ml-engine local predict --model-dir=${MODEL_LOCATION} --json-instances=data/new-data2.json --verbosity debug

the trackback will give you info about the path of gcloud ml engine files, in my machine was:引用会给你有关 gcloud ml 引擎文件路径的信息,在我的机器上是:

/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib/googlecloudsdk/command_lib/ml_engine/

go to that directory and delete the pyc files.转到该目录并删除 pyc 文件。

In fact I myself post this question to help people with the same problem, because I couldn't find an easy concise answer.事实上,我自己发布这个问题是为了帮助有同样问题的人,因为我找不到简单简洁的答案。

There are other solutions, in my opinion even better than mine, but this was what solved for me.还有其他解决方案,在我看来甚至比我的更好,但这就是为我解决的问题。

My solution was that google cloud sdk doesn't works with python 3, at least in my configuration.我的解决方案是 google cloud sdk 不适用于 python 3,至少在我的配置中。 To solve:要解决:

  1. install a anaconda virtual environment with python 2 (in my case 2.7.14)使用 python 2 安装 anaconda 虚拟环境(在我的例子中是 2.7.14)
  2. activate the environment激活环境
  3. execute the gcloud command again再次执行 gcloud 命令

If your export ml model and inputs are OK that will work.如果您的导出 ml 模型和输入没问题,那将起作用。

Simple problem, but caused a lot of pain to me, just because I couldn't easily find this pre-requirement or I simply missed it.简单的问题,却给我造成了很大的痛苦,只是因为我不容易找到这个前置要求或者我只是错过了它。

I hope help somebody.我希望帮助某人。

Find and delete all the pyc files in the google SDK.查找并删除google SDK中的所有pyc文件。 They were compiled with the wrong python environment.它们是用错误的 python 环境编译的。 They will be recompiled automatically when next needed.它们将在下次需要时自动重新编译。

%%bash
find "/tools/google-cloud-sdk/lib/" -name '*.pyc' -delete

Below are the steps for fixing this issue in Ubuntu:以下是在 Ubuntu 中修复此问题的步骤:

1.Navigate to ml_engine path 1.导航到ml_engine路径

cd /usr/lib/google-cloud-sdk/lib/googlecloudsdk/command_lib/ml_engine cd /usr/lib/google-cloud-sdk/lib/googlecloudsdk/command_lib/ml_engine

2.Remove files ending with .pyc 2.删除以.pyc结尾的文件

sudo rm -rf *.pyc须藤 rm -rf *.pyc

In Notebook run the following Cell:在 Notebook 中运行以下 Cell:

%%bash
sudo find "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/command_lib/ml_engine" -name '*.pyc' -delete

The above tricks, is working but I found another option which seems to be permanent and which is to use GCP SDK with python 3.上述技巧是有效的,但我发现了另一个似乎是永久性的选项,即使用 GCP SDK 和 python 3。

Create a python 3 env with anaconda for example or use an existing python 3 installation.例如,使用 anaconda 创建一个 python 3 env 或使用现有的 python 3 安装。

Create a file gcp-sdk.yaml:创建文件 gcp-sdk.yaml:

name: env_gcp_sdk
channels:
- defaults
- conda-forge
dependencies:
  # core packages
  - python=3.7.5
  - pip=20.0.2
  - pip:
    - google-cloud-storage
    - google-cloud-bigquery
    - google-cloud-kms
    - google-cloud-pubsub

Then create the env:然后创建环境:

conda env create -f gcp-sdk.yaml 

Now set the following even variables and in my case I don't need anymore to deleted *.pyc files:现在设置以下偶数变量,在我的情况下,我不再需要删除 *.pyc 文件:

os.environ['CLOUDSDK_PYTHON']='path_to_env/env_gcp_sdk/bin/python'
os.environ['CLOUDSDK_GSUTIL_PYTHON']='path_to_env/env_gcp_sdk/bin/python'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM