Daniel Vieira
Daniel Vieira

Reputation: 471

error when importing google-cloud-bigquery python module

so i replaced the google-cloud package installation with the google-cloud-bigquery one as google-cloud has been depreacated :

Requirement already up-to-date: google-cloud-bigquery in /usr/local/lib/python3.5/dist-packages (1.5.0)

Now the issue is that when I try to import the package I get a syntax error which really I do not understand:

    import google-cloud-bigquery as bq
             ^SyntaxError: invalid syntax

This is doing my head in, can someone please help, what is the problem with importing this package?

Upvotes: 1

Views: 1424

Answers (3)

Armin_SC
Armin_SC

Reputation: 2260

The google-cloud-bigquery syntax has to be implemented during the Client Library installation phase; however, the correct way to import the Google Cloud client library is by using the from google.cloud import bigquery format. You can use the following Google's official example as a reference:

# Imports the Google Cloud client library
from google.cloud import bigquery

# Instantiates a client
bigquery_client = bigquery.Client()

# The name for the new dataset
dataset_id = 'my_new_dataset'

# Prepares a reference to the new dataset
dataset_ref = bigquery_client.dataset(dataset_id)
dataset = bigquery.Dataset(dataset_ref)

# Creates the new dataset
dataset = bigquery_client.create_dataset(dataset)

print('Dataset {} created.'.format(dataset.dataset_id))

Upvotes: 1

Martin Stone
Martin Stone

Reputation: 13007

The reason for the syntax error is that the minus sign is an illegal character in a package or module name. Typically packages will use underscores in the actual package names, or have a nested structure, as in this case: import google.cloud.bigquery as bq

Upvotes: 1

Graham Polley
Graham Polley

Reputation: 14781

Use:

from google.cloud import bigquery

Upvotes: 3

Related Questions