【问题标题】:ImportError: No module named numpy - Google Cloud Dataproc when using Jupyter NotebookImportError:没有名为 numpy 的模块 - 使用 Jupyter Notebook 时的 Google Cloud Dataproc
【发布时间】:2016-11-29 01:31:12
【问题描述】:

在 Google Dataproc 上启动 Jupyter Notebook 时,导入模块失败。我尝试使用不同的命令安装模块。一些例子:

import os
os.sytem("sudo apt-get install python-numpy")
os.system("sudo pip install numpy") #after having installed pip
os.system("sudo pip install python-numpy") #after having installed pip

import numpy

以上示例均无效并返回导入错误:

enter image description here

使用命令行时,我可以安装模块,但导入错误仍然存​​在。我想我在错误的位置安装了模块。

有什么想法吗?

【问题讨论】:

  • 如何在 Cloud Dataproc 上安装 Jupyter?您使用的是 Jupyter 初始化操作 (github.com/GoogleCloudPlatform/dataproc-initialization-actions/…) 还是其他方式?
  • 我使用了 github 上描述的初始化操作。我尝试了 Jupyter 和 Jupyter 以及 conda-bootstrap 的初始化操作。两者都有相同的问题。注意:我使用的是 PySpark 内核。也许我将模块安装在错误的位置? Jupyter 中使用的 python (pyspark) 的路径是什么?如何安装此版本的模块?我的 gcloud 创建命令:
  • gcloud dataproc clusters create test --zone=europe-west1-d --master-machine-type n1-standard-2 --master-boot-disk-size 100 --num-workers 2 --worker-machine-type n1-standard-2 --worker-boot-disk-size 50 --project myproject --bucket mybucket --initialization-actions gs://dataproc-initialization-actions/jupyter/jupyter.sh
  • 使用os.system("sudo apt-get install python-pip -y")os.system("sudo pip install numpy")时,两行都返回0,表示执行成功。仍然 import numpy 返回相同的导入错误。

标签: python importerror jupyter-notebook google-cloud-dataproc


【解决方案1】:

我找到了解决办法。

import sys

sys.path.append('/usr/lib/python2.7/dist-packages')

os.system("sudo apt-get install python-pandas -y")
os.system("sudo apt-get install python-numpy -y")
os.system("sudo apt-get install python-scipy -y")
os.system("sudo apt-get install python-sklearn -y")

import pandas
import numpy
import scipy
import sklearn

如果有人有更优雅的解决方案,请告诉我。

【讨论】:

  • 您好 Stijn,您的上述方法是否适用于 dataproc 集群中的所有工作节点或仅适用于主节点?
  • 我猜这只会在主节点上安装模型。我现在安装模块的方式是将它们保存在谷歌存储桶中,然后使用初始化操作将它们复制到属于路径的位置。 if gsutil -q stat "gs://$DATAPROC_BUCKET/modules/**"; thenecho "Pulling modules directory to cluster master and worker nodes..."gsutil -m cp -r gs://$DATAPROC_BUCKET/modules/* /usr/local/bin/miniconda/lib/python2.7
  • 您的初始化脚本是否包含 jupyter notebook,您愿意分享吗?
  • 我将脚本放在下面的答案中。它只是 Juoyter 的标准初始化脚本,对将 google 存储内容复制到 dataproc 机器进行了一些修改。请记住,您应该将要使用的模块复制到“gs://$DATAPROC_BUCKET/modules/”中。
【解决方案2】:

尝试conda install numpy,因为 Google 的 jupyter 初始化脚本正在使用 conda。 我个人更喜欢有自己的初始化脚本,这样我可以有更多的控制权。

【讨论】:

  • 不,它应该使用 --initialization-actions 选项在您的自定义 init-action 脚本中运行。
【解决方案3】:
    #!/usr/bin/env bash
    set -e

    ROLE=$(curl -f -s -H Metadata-Flavor:Google http://metadata/computeMetadata/v1/instance/attributes/dataproc-role)
    INIT_ACTIONS_REPO=$(curl -f -s -H Metadata-Flavor:Google http://metadata/computeMetadata/v1/instance/attributes/INIT_ACTIONS_REPO || true)
    INIT_ACTIONS_REPO="${INIT_ACTIONS_REPO:-https://github.com/GoogleCloudPlatform/dataproc-initialization-actions.git}"
    INIT_ACTIONS_BRANCH=$(curl -f -s -H Metadata-Flavor:Google http://metadata/computeMetadata/v1/instance/attributes/INIT_ACTIONS_BRANCH || true)
    INIT_ACTIONS_BRANCH="${INIT_ACTIONS_BRANCH:-master}"
    DATAPROC_BUCKET=$(curl -f -s -H Metadata-Flavor:Google http://metadata/computeMetadata/v1/instance/attributes/dataproc-bucket)

    echo "Cloning fresh dataproc-initialization-actions from repo $INIT_ACTIONS_REPO and branch $INIT_ACTIONS_BRANCH..."
    git clone -b "$INIT_ACTIONS_BRANCH" --single-branch $INIT_ACTIONS_REPO
    # Ensure we have conda installed.
    ./dataproc-initialization-actions/conda/bootstrap-conda.sh
    #./dataproc-initialization-actions/conda/install-conda-env.sh

    source /etc/profile.d/conda_config.sh

    if [[ "${ROLE}" == 'Master' ]]; then
        conda install jupyter

        if gsutil -q stat "gs://$DATAPROC_BUCKET/notebooks/**"; then
           echo "Pulling notebooks directory to cluster master node..."
           gsutil -m cp -r gs://$DATAPROC_BUCKET/notebooks /root/
        fi

        ./dataproc-initialization-actions/jupyter/internal/setup-jupyter-kernel.sh
        ./dataproc-initialization-actions/jupyter/internal/launch-jupyter-kernel.sh
     fi

     if gsutil -q stat "gs://$DATAPROC_BUCKET/scripts/**"; then
         echo "Pulling scripts directory to cluster master and worker nodes..."
         gsutil -m cp -r gs://$DATAPROC_BUCKET/scripts/*     /usr/local/bin/miniconda/lib/python2.7
     fi 

     if gsutil -q stat "gs://$DATAPROC_BUCKET/modules/**"; then
        echo "Pulling modules directory to cluster master and worker nodes..."
        gsutil -m cp -r gs://$DATAPROC_BUCKET/modules/*    /usr/local/bin/miniconda/lib/python2.7   
     fi 

     echo "Completed installing Jupyter!"

# Install Jupyter extensions (if desired)
# TODO: document this in readme
if [[ ! -v $INSTALL_JUPYTER_EXT ]]
    then
    INSTALL_JUPYTER_EXT=false
fi
if [[ "$INSTALL_JUPYTER_EXT" = true ]]
then
    echo "Installing Jupyter Notebook extensions..."
    ./dataproc-initialization-actions/jupyter/internal/bootstrap-jupyter-ext.sh
    echo "Jupyter Notebook extensions installed!"
fi

【讨论】:

  • 你应该添加一些关于你用这个脚本做什么的解释
  • 实际上,这只是 Jupyter 初始化脚本,可以在 github.com/GoogleCloudPlatform/dataproc-initialization-actions/… 上找到。我唯一添加的是将内容从 gs 存储桶复制到机器的三个 gsutil -m -cp -r 行。这由例如echo "Pulling modules directory to cluster master and worker nodes..." 表示
【解决方案4】:

您是否尝试执行以下命令?

pip install ipython[numpy]

【讨论】:

  • 我刚刚在 master 的命令行中测试了“sudo pip install ipython[numpy]”,在 Jupyter 中通过 os.system() 都失败了。命令行中的错误消息:
  • 异常:回溯(最近一次调用最后一次):文件“/usr/lib/python2.7/dist-packages/pip/basecommand.py”,第 122 行,主要状态 = self.run (选项,参数)文件“/usr/lib/python2.7/dist-packages/pip/commands/install.py”,第 290 行,在运行中 require_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.捆绑)...
  • 文件“/usr/lib/python2.7/dist-packages/pip/req.py”,第 1266 行,在 prepare_files req_to_install.extras 中):文件“/usr/lib/python2.7 /dist-packages/pkg_resources.py", line 2409, in requires "%s has no such extra feature %r" % (self, ext) UnknownExtra: ipython 5.0.0 has no such extra feature 'numpy' 存储调试日志/root/.pip/pip.log 失败
猜你喜欢
  • 2017-08-04
  • 2017-06-29
  • 2016-07-10
  • 2020-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多