Skip to content

Error: No module named 'google' (protobuf) #137

@twkevinzhang

Description

@twkevinzhang

I can't install protobuf on Android Pixel 7.

i used flask and protobuf lib in main.py

import sys
import traceback

from flask import Flask, jsonify
from werkzeug.exceptions import InternalServerError

from utils import succeed_protobuf

app = Flask(__name__)

@app.errorhandler(InternalServerError)
def handle_500_error(e):
    exc_type, exc_value, exc_traceback = sys.exc_info()
    trace = traceback.extract_tb(exc_traceback)

    # Extract relevant information
    error_info = {
        'error': str(e),
        'type': exc_type.__name__,
        'message': str(exc_value),
        'filename': trace[-1].filename,
        'line_number': trace[-1].lineno,
        'function': trace[-1].name,
        'traceback': traceback.format_exc()
    }
    return jsonify(error_info), 500

@app.route("/sites/<site_id>", methods=["GET"])
async def site(site_id):
    import extension_api_pb2 as pb2 # generated by protocol buffer compiler
    site = pb2.Site(
        id="1",
        icon="https://komica1.org/favicon.ico",
        name="komica1.org",
        description="A description of komica1.org",
        url="https://komica1.org"
    )
    response = pb2.GetSiteRes(site=site)
    return succeed_protobuf(response)

port = 55001
print("Trying to run a socket server on:", port)
app.run(port=port)

requirements.txt

flask[async]==3.1.0
protobuf==5.28.3

a part of extension_api_pb2.py

# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# NO CHECKED-IN PROTOBUF GENCODE
# source: extension_api.proto
# Protobuf Python Version: 5.29.0
"""Generated protocol buffer code."""
from google.protobuf import descriptor as _descriptor
from google.protobuf import descriptor_pool as _descriptor_pool
from google.protobuf import runtime_version as _runtime_version
from google.protobuf import symbol_database as _symbol_database
from google.protobuf.internal import builder as _builder
...

then i build it to twkevinzhang_komica.zip

dart run serious_python:main package app/src -p Android --requirements -r,app/src/requirements.txt --asset app/dist/twkevinzhang_komica.zip --verbose

in dart code, i catch the 500 status_code and print server_internal_error

Future<void> main() async {
    final directory = await getApplicationSupportDirectory();
    final appPath = "${directory.path}/from_github/twkevinzhang_komica"
    Directory.current = path.dirname(appPath);
    final program = "$appPath/main.py";
    print("run $program");
    SeriousPython.runProgram(program, environmentVariables: {});
    await Future.delayed(const Duration(seconds: 10)); // wait flask server launched

    final path = "sites/1";
    try {
      final res = await _dio.get("http://127.0.0.1:55001");
      print("site res $res");
    } on DioException catch (e) {
      print("site error");
      if (e.response != null) {
        print(e.response!.data);
        print(e.response!.headers);
        print(e.response!.requestOptions);
      } else {
        print(e.requestOptions);
        print(e.message);
      }
      throw e;
    }
  }

i get:

I/flutter (  884): {error: 500 Internal Server Error: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application., 

filename: /data/user/0/tw.kevinzhang.news_hub/files/flasks/twkevinzhang_komica/extension_api_pb2.py, function: <module>, 
line_number: 7, 
message: **No module named 'google'**,
traceback: Traceback (most recent call last):
I/flutter (  884):   File "/data/user/0/tw.kevinzhang.news_hub/files/flet/python_site_packages/flask/app.py", line 1511, in wsgi_app
I/flutter (  884):     response = self.full_dispatch_request()
I/flutter (  884):                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I/flutter (  884):   File "/data/user/0/tw.kevinzhang.news_hub/files/flet/python_site_packages/flask/app.py", line 919, in full_dispatch_request
I/flutter (  884):     rv = self.handle_user_exception(e)
I/flutter (  884):          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I/flutter (  884):   File "/data/user/0/tw.kevinzhang.news_hub/files/flet/python_site_packages/flask/app.py", line 917, in full_dispatch_request
I/flutter (  884):     rv = self.dispatch_request()
I/flutter (  884):          ^^^^^^^^^^^^^^^^^^^^^^^
I/flutter (  884):   File "/data/us
I/flutter (  884): connection: close
I/flutter (  884): content-type: application/json
I/flutter (  884): date: Mon, 10 Feb 2025 18:24:48 GMT
I/flutter (  884): content-length: 2290
I/flutter (  884): server: Werkzeug/3.1.3 Python/3.12.9
I/flutter (  884): Instance of 'RequestOptions'
E/flutter (  884): [ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: DioException [bad response]: This exception was thrown because the response has a status code of 500 and RequestOptions.validateStatus was configured to throw for this status code.
E/flutter (  884): The status code of 500 has the following meaning: "Server error - the server failed to fulfil an apparently valid request"
E/flutter (  884): Read more about status codes at https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
E/flutter (  884): In order to resolve this exception you typically have either to verify and fix your request code or you have to fix the server code.
E/flutter (  884): 
E/flutter (  884): #0      ExtensionHttpApiServiceImpl.site (package:news_hub/app/extension/extension_http_api_service_impl.dart:66:7)
E/flutter (  884): <asynchronous suspension>
E/flutter (  884): #1      ExtensionHttpApiServiceImpl.run (package:news_hub/app/extension/extension_http_api_service_impl.dart:37:5)
E/flutter (  884): <asynchronous suspension>
E/flutter (  884): #2      testApiService (package:news_hub/main.dart:51:3)
E/flutter (  884): <asynchronous suspension>

then i check my Android device, no google or protobuf folders:

$ adb exec-out run-as tw.kevinzhang.news_hub ls /data/data/tw.kevinzhang.news_hub/files/flet/python_site_packages

MarkupSafe-2.1.5.dist-info  frozenlist
aiohttp                     frozenlist-1.5.0.dist-info
aiohttp-3.9.5.dist-info     idna
aiosignal                   idna-3.10.dist-info
aiosignal-1.3.2.dist-info   itsdangerous
asgiref                     itsdangerous-2.2.0.dist-info
asgiref-3.8.1.dist-info     jinja2
attr                        jinja2-3.1.5.dist-info
attrs                       markupsafe
attrs-25.1.0.dist-info      multidict
bin                         multidict-6.1.0.dist-info
blinker                     propcache
blinker-1.9.0.dist-info     propcache-0.2.1.dist-info
click                       werkzeug
click-8.1.8.dist-info       werkzeug-3.1.3.dist-info
flask                       yarl
flask-3.1.0.dist-info       yarl-1.18.3.dist-info

how can I install protobuf lib?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions