Skip to content

Commit fec3ed7

Browse files
authored
Merge pull request #33 from james-see/fix/grpcio-syntax-error
Fix syntax error in gRPC example
2 parents 69fb4b1 + eb45f68 commit fec3ed7

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed

python-examples/GRPC_README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,22 @@ pip install grpcio grpcio-tools
2121

2222
## Setup
2323

24-
First, generate the gRPC Python code from the proto file:
24+
First, navigate to the python-examples directory and generate the gRPC Python code:
2525

2626
```bash
27-
python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. python-examples/grpc_example.proto
27+
cd python-examples
28+
python grpcio-example.py setup
2829
```
2930

30-
Or use the built-in setup command:
31+
This generates:
32+
- `grpc_example_pb2.py` - Protocol buffer message classes
33+
- `grpc_example_pb2_grpc.py` - gRPC service classes
34+
35+
Alternatively, you can manually generate with:
3136

3237
```bash
3338
cd python-examples
34-
python grpcio-example.py setup
39+
python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. grpc_example.proto
3540
```
3641

3742
This generates:

python-examples/grpcio-example.py

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
pip install grpcio grpcio-tools
1111
1212
Setup:
13-
Before running, generate the gRPC Python code from the proto file:
14-
python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. python-examples/grpc_example.proto
13+
Navigate to the python-examples directory and run setup:
14+
cd python-examples
15+
python grpcio-example.py setup
1516
1617
To run:
17-
1. Start the server: python grpcio-example.py server
18-
2. In another terminal, run the client: python grpcio-example.py client
18+
cd python-examples
19+
1. Terminal 1: python grpcio-example.py server
20+
2. Terminal 2: python grpcio-example.py client
1921
2022
Note: This example uses insecure channels for simplicity. In production,
2123
use secure channels with TLS/SSL certificates.
@@ -28,35 +30,43 @@
2830
import time
2931
import subprocess
3032

31-
# Check if proto files are generated, if not, generate them
32-
if not os.path.exists('python-examples/grpc_example_pb2.py'):
33-
print("Generating gRPC Python code from proto file...")
33+
# Check if proto files are generated in the same directory
34+
script_dir = os.path.dirname(os.path.abspath(__file__))
35+
proto_file = os.path.join(script_dir, 'grpc_example_pb2.py')
36+
37+
if not os.path.exists(proto_file):
38+
print("gRPC proto files not found. Attempting to generate...")
39+
proto_source = os.path.join(script_dir, 'grpc_example.proto')
40+
41+
if not os.path.exists(proto_source):
42+
print(f"Error: Proto file not found at {proto_source}")
43+
sys.exit(1)
44+
3445
try:
3546
subprocess.run([
3647
sys.executable, '-m', 'grpc_tools.protoc',
37-
'-I.', '--python_out=.', '--grpc_python_out=.',
38-
'python-examples/grpc_example.proto'
39-
], check=True)
48+
f'-I{script_dir}',
49+
f'--python_out={script_dir}',
50+
f'--grpc_python_out={script_dir}',
51+
proto_source
52+
], check=True, cwd=script_dir)
4053
print("Generated successfully!\n")
4154
except subprocess.CalledProcessError as e:
4255
print(f"Error generating proto files: {e}")
4356
print("Make sure grpcio-tools is installed: pip install grpcio-tools")
4457
sys.exit(1)
4558

4659
# Import generated proto classes
60+
# Note: proto files should be generated in the same directory as this script
4761
try:
48-
from python-examples import grpc_example_pb2
49-
from python-examples import grpc_example_pb2_grpc
62+
import grpc_example_pb2
63+
import grpc_example_pb2_grpc
5064
except ImportError:
51-
# Try alternative import path
52-
try:
53-
sys.path.insert(0, 'python-examples')
54-
import grpc_example_pb2
55-
import grpc_example_pb2_grpc
56-
except ImportError:
57-
print("Error: Failed to import generated gRPC modules.")
58-
print("Please ensure the proto files are generated correctly.")
59-
sys.exit(1)
65+
print("Error: gRPC proto files not found.")
66+
print("Please run: python grpcio-example.py setup")
67+
print("Or manually generate with:")
68+
print(" python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. python-examples/grpc_example.proto")
69+
sys.exit(1)
6070

6171

6272
# Server implementation
@@ -171,13 +181,23 @@ def print_usage():
171181

172182
if command == 'setup':
173183
print("Generating gRPC Python code from proto file...")
184+
script_dir = os.path.dirname(os.path.abspath(__file__))
185+
proto_source = os.path.join(script_dir, 'grpc_example.proto')
186+
187+
if not os.path.exists(proto_source):
188+
print(f"✗ Error: Proto file not found at {proto_source}")
189+
sys.exit(1)
190+
174191
try:
175192
subprocess.run([
176193
sys.executable, '-m', 'grpc_tools.protoc',
177-
'-I.', '--python_out=.', '--grpc_python_out=.',
178-
'python-examples/grpc_example.proto'
179-
], check=True)
194+
f'-I{script_dir}',
195+
f'--python_out={script_dir}',
196+
f'--grpc_python_out={script_dir}',
197+
proto_source
198+
], check=True, cwd=script_dir)
180199
print("✓ Generated successfully!")
200+
print(f"✓ Files created in: {script_dir}")
181201
print("\nYou can now run:")
182202
print(" python grpcio-example.py server")
183203
print(" python grpcio-example.py client")

0 commit comments

Comments
 (0)