0

I am trying to run the quickstart code snippet from Google Cloud tutorials and somehow the line

from google.cloud import documentai_v1 as documentai

I get this error:

Cannot find reference 'documentai_v1' in 'imported module google.cloud'

The only module which is accessible for import is Storage. Same issue with Pub/Sub, BigQuery, etc.

The Google imports in requirements.txt are:

google-api-core==2.10.2
google-cloud-core==2.3.2
google-auth==2.12.0
google-cloud-documentai==2.0.0
googleapis-common-protos==1.56.4
from google.api_core.client_options import ClientOptions
from google.cloud import documentai_v1 as documentai

from tabulate import tabulate

def quickstart(project_id: str, location: str, processor_id: str, file_path: str, mime_type: str):

    # You must set the api_endpoint if you use a location other than 'us', e.g.:
    opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")

    client = documentai.DocumentProcessorServiceClient(client_options=opts)

    # The full resource name of the processor, e.g.:
    # projects/project_id/locations/location/processor/processor_id
    name = client.processor_path(project_id, location, processor_id)

    # Read the file into memory
    with open(file_path, "rb") as image:
        image_content = image.read()

    # Load Binary Data into Document AI RawDocument Object
    raw_document = documentai.RawDocument(content=image_content, mime_type=mime_type)

    # Configure the process request
    request = documentai.ProcessRequest(name=name, raw_document=raw_document)

    result = client.process_document(request=request)

    # For a full list of Document object attributes, please reference this page:
    # https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.types.Document
    document = result.document

    # Read the text recognition output from the processor
    print("The document contains the following text:")
    print(document.text)

double-beep
  • 5,031
  • 17
  • 33
  • 41
Rohan
  • 1

2 Answers2

0

It seems you are importing the module in the wrong way. Instead of importing from google.cloud import documentai_v1 as documentai try importing this module: from google.cloud import documentai or if you are importing documentai_v1 refer this import statement:

from google.cloud import documentai_v1

In code where ever you want to use call the documentai_v1 functions like below:

documentai_v1.Document.to_json(my_document_object)
0

The code samples have been updated recently, you can get the most up to date version here.

You shouldn't need to use:

from google.cloud import documentai_v1 as documentai

You can just import

from google.cloud import documentai

(It will default to the v1 endpoint)

It doesn't seem like the requirements were installed correctly.

Try running

pip install --upgrade google-cloud-documentai

Or

pip install --upgrade -r requirements.txt

(Be sure to update your requirements.txt to use the latest Python Client Library version 2.15.0

double-beep
  • 5,031
  • 17
  • 33
  • 41
Holt Skinner
  • 1,692
  • 1
  • 8
  • 21