从 google.cloud 导入 bigquery ModuleNotFoundError:没有名为“google”的模块

Posted

技术标签:

【中文标题】从 google.cloud 导入 bigquery ModuleNotFoundError:没有名为“google”的模块【英文标题】:from google.cloud import bigquery ModuleNotFoundError: No module named 'google' 【发布时间】:2019-09-17 03:02:30 【问题描述】:

按照基本的 Google 云运行教程 (https://cloud.google.com/run/docs/quickstarts/build-and-deploy),

我一直在尝试部署到更复杂的东西但没有成功..

我的 app.py 以这些导入开始:

import os
import uuid
import requests
import json
from google.cloud import bigquery
from flask import Flask

但是当我尝试运行它时,我得到以下日志:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker 
    worker.init_process()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 104, in init_process 
    super(ThreadWorker, self).init_process() 
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi() 
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi 
    self.callable = self.load()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load 
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp 
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app 
    __import__(module)
  File "/app/app.py", line 6, in <module> 
    from google.cloud import bigquery
ModuleNotFoundError: No module named 'google'

Dockerfile 看起来像这样:

# Use the official Python image.
# https://hub.docker.com/_/python
FROM python:3.7-slim

# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./

# Install production dependencies.
RUN pip install Flask gunicorn requests uuid google

# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:app

【问题讨论】:

【参考方案1】:

看起来要为 BigQuery 安装的正确 python 模块是 google-cloud-bigquery

RUN pip install Flask gunicorn requests uuid google-cloud-bigquery

【讨论】:

以上是关于从 google.cloud 导入 bigquery ModuleNotFoundError:没有名为“google”的模块的主要内容,如果未能解决你的问题,请参考以下文章

ImportError:无法从“google.cloud”(未知位置)导入名称“bigquery”

从 google.cloud 导入 bigquery ModuleNotFoundError:没有名为“google”的模块

如何从 google.cloud 导入 bigquery 模块

ImportError:无法从“google.cloud”导入名称“tasks_v2”

ImportError:没有名为 google.cloud 的模块

google.cloud 导入存储:无法导入存储