There is no module named cloud when using google.cloud import bigquery

I have an application for an application that loads data into a bigquery table using the google apps launcher, but when I run it on a local host or in the cloud, I get a "No" module named "cloud" when using the error message with google.cloud error import bigquery log file error. I installed the Google Cloud Client Library, but it still gives me the same error. see below the code i use

--- the main.py file contains

import argparse import time import uuid from google.cloud import bigquery def load_data_from_gcs(dataset_name, table_name, source): bigquery_client = bigquery.Client() dataset = bigquery_client.dataset(dataset_name) table = dataset.table(table_name) job_name = str(uuid.uuid4()) job = bigquery_client.load_table_from_storage( job_name, table, source) job.begin() wait_for_job(job) print('Loaded {} rows into {}:{}.'.format( job.output_rows, dataset_name, table_name)) def wait_for_job(job): while True: job.reload() if job.state == 'DONE': if job.error_result: raise RuntimeError(job.error_result) return time.sleep(1) if __name__ == '__main__': parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('Test') parser.add_argument('mytable') parser.add_argument('gs://week/geninfo.csv') args = parser.parse_args() load_data_from_gcs( args.dataset_name, args.table_name, args.source) 

- The app.yaml file contains the following code

 application: mycloudproject version: 1 runtime: python27 api_version: 1 threadsafe: yes handlers: - url: /favicon\.ico static_files: favicon.ico upload: favicon\.ico - url: .* script: main.app 

Please let me know what is missing, or if I am doing something wrong here?

+6
source share
4 answers

It can be a little tricky. Google Cloud uses the new Python namespace format (if you look at the source, you will notice that there is no __init__.py in the directory structure).

This has been changed in Python 3.3 with PEP-420

Fortunately, in Python 2.7 you can easily fix this by avoiding implicit imports. Just add this to the top of the file:

from __future__ import absolute_import

Hope this helps.

+3
source

Find the directory containing google/cloud/... and add this directory to PYTHONPATH so python can find it. For more information on how to add to PYTHONPATH, see this post . It outlines two general ways to do this:

Here's how to do it with the bash command:

 export PYTHONPATH=$PYTHONPATH:/<path_to_modules> 

Or you can add it to the path in the script:

 # if the google/ directory is in the directory /path/to/directory/ path_to_look_for_module = '/path/to/directory/' import sys if not path_to_look_for_module in sys.path: sys.path.append(path_to_look_for_module) 

If that doesn't work, here is the code I found in one of my projects for importing Google Appengine modules:

 def fixup_paths(path): """Adds GAE SDK path to system path and appends it to the google path if that already exists.""" # Not all Google packages are inside namespace packages, which means # there might be another non-namespace package named `google` already on # the path and simply appending the App Engine SDK to the path will not # work since the other package will get discovered and used first. # This emulates namespace packages by first searching if a `google` package # exists by importing it, and if so appending to its module search path. try: import google google.__path__.append("{0}/google".format(path)) except ImportError: pass sys.path.insert(0, path) # and then call later in your code: fixup_paths(path_to_google_sdk) from google.cloud import bigquery 
0
source

It looks like you're trying to use the Cloud Datastore Client Library in the Google App Engine Standard Environment . As documented in the Google documentation , you should not do this. Instead, use the NDB Client Library or do not use a standard environment.

0
source

Are you sure you updated to the latest version of the library? The version installed in pip may be outdated. Previously, the module was imported as:

 from gcloud import bigquery 

If this works, you are using an older version. To install the latest version, I recommend pulling github from the wizard into the project .

-1
source

Source: https://habr.com/ru/post/1266055/


All Articles