From eb45f68d996337f209c01836600816806556bc81 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 27 Dec 2025 00:21:55 +0000 Subject: [PATCH] Fix syntax error in gRPC example Fixed import statements that used hyphens (python-examples) which are not valid Python identifiers. Updated to use relative imports and proper path handling. Changes: - Fixed import statements to work correctly - Updated proto file generation to use script directory - Improved setup command with better path handling - Updated documentation to reflect correct usage - All proto files now generated in python-examples/ directory The example now works correctly when run from python-examples/ directory as documented. --- python-examples/GRPC_README.md | 13 ++++-- python-examples/grpcio-example.py | 68 ++++++++++++++++++++----------- 2 files changed, 53 insertions(+), 28 deletions(-) diff --git a/python-examples/GRPC_README.md b/python-examples/GRPC_README.md index d7c0911..8d59357 100644 --- a/python-examples/GRPC_README.md +++ b/python-examples/GRPC_README.md @@ -21,17 +21,22 @@ pip install grpcio grpcio-tools ## Setup -First, generate the gRPC Python code from the proto file: +First, navigate to the python-examples directory and generate the gRPC Python code: ```bash -python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. python-examples/grpc_example.proto +cd python-examples +python grpcio-example.py setup ``` -Or use the built-in setup command: +This generates: +- `grpc_example_pb2.py` - Protocol buffer message classes +- `grpc_example_pb2_grpc.py` - gRPC service classes + +Alternatively, you can manually generate with: ```bash cd python-examples -python grpcio-example.py setup +python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. grpc_example.proto ``` This generates: diff --git a/python-examples/grpcio-example.py b/python-examples/grpcio-example.py index 85381e8..b7bbf94 100644 --- a/python-examples/grpcio-example.py +++ b/python-examples/grpcio-example.py @@ -10,12 +10,14 @@ pip install grpcio grpcio-tools Setup: - Before running, generate the gRPC Python code from the proto file: - python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. python-examples/grpc_example.proto + Navigate to the python-examples directory and run setup: + cd python-examples + python grpcio-example.py setup To run: - 1. Start the server: python grpcio-example.py server - 2. In another terminal, run the client: python grpcio-example.py client + cd python-examples + 1. Terminal 1: python grpcio-example.py server + 2. Terminal 2: python grpcio-example.py client Note: This example uses insecure channels for simplicity. In production, use secure channels with TLS/SSL certificates. @@ -28,15 +30,26 @@ import time import subprocess -# Check if proto files are generated, if not, generate them -if not os.path.exists('python-examples/grpc_example_pb2.py'): - print("Generating gRPC Python code from proto file...") +# Check if proto files are generated in the same directory +script_dir = os.path.dirname(os.path.abspath(__file__)) +proto_file = os.path.join(script_dir, 'grpc_example_pb2.py') + +if not os.path.exists(proto_file): + print("gRPC proto files not found. Attempting to generate...") + proto_source = os.path.join(script_dir, 'grpc_example.proto') + + if not os.path.exists(proto_source): + print(f"Error: Proto file not found at {proto_source}") + sys.exit(1) + try: subprocess.run([ sys.executable, '-m', 'grpc_tools.protoc', - '-I.', '--python_out=.', '--grpc_python_out=.', - 'python-examples/grpc_example.proto' - ], check=True) + f'-I{script_dir}', + f'--python_out={script_dir}', + f'--grpc_python_out={script_dir}', + proto_source + ], check=True, cwd=script_dir) print("Generated successfully!\n") except subprocess.CalledProcessError as e: print(f"Error generating proto files: {e}") @@ -44,19 +57,16 @@ sys.exit(1) # Import generated proto classes +# Note: proto files should be generated in the same directory as this script try: - from python-examples import grpc_example_pb2 - from python-examples import grpc_example_pb2_grpc + import grpc_example_pb2 + import grpc_example_pb2_grpc except ImportError: - # Try alternative import path - try: - sys.path.insert(0, 'python-examples') - import grpc_example_pb2 - import grpc_example_pb2_grpc - except ImportError: - print("Error: Failed to import generated gRPC modules.") - print("Please ensure the proto files are generated correctly.") - sys.exit(1) + print("Error: gRPC proto files not found.") + print("Please run: python grpcio-example.py setup") + print("Or manually generate with:") + print(" python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. python-examples/grpc_example.proto") + sys.exit(1) # Server implementation @@ -171,13 +181,23 @@ def print_usage(): if command == 'setup': print("Generating gRPC Python code from proto file...") + script_dir = os.path.dirname(os.path.abspath(__file__)) + proto_source = os.path.join(script_dir, 'grpc_example.proto') + + if not os.path.exists(proto_source): + print(f"✗ Error: Proto file not found at {proto_source}") + sys.exit(1) + try: subprocess.run([ sys.executable, '-m', 'grpc_tools.protoc', - '-I.', '--python_out=.', '--grpc_python_out=.', - 'python-examples/grpc_example.proto' - ], check=True) + f'-I{script_dir}', + f'--python_out={script_dir}', + f'--grpc_python_out={script_dir}', + proto_source + ], check=True, cwd=script_dir) print("✓ Generated successfully!") + print(f"✓ Files created in: {script_dir}") print("\nYou can now run:") print(" python grpcio-example.py server") print(" python grpcio-example.py client")