|
1 | 1 | Getting Started |
2 | 2 | =============== |
3 | 3 |
|
4 | | -## Installation |
| 4 | +## Compilation |
| 5 | + |
| 6 | + |
| 7 | +### Install protoc |
5 | 8 |
|
6 | | -The package `betterproto2` can be simply installed from PyPI using `pip`: |
| 9 | +The betterproto2 compiler is a plugin of `protoc`, you first need to [install](https://grpc.io/docs/protoc-installation/) it. |
| 10 | + |
| 11 | +You can also use it from `grpcio-tools`: |
7 | 12 |
|
8 | 13 | ```sh |
9 | | -pip install betterproto2[all] |
| 14 | +pip install grpcio-tools |
| 15 | +``` |
| 16 | + |
| 17 | +### Install `betterproto2_compiler` |
| 18 | + |
| 19 | +It is possible to install `betterproto2_compiler` using pip: |
| 20 | + |
| 21 | +``` |
| 22 | +pip install betterproto2_compiler |
| 23 | +``` |
| 24 | + |
| 25 | +### Compile a proto file |
| 26 | + |
| 27 | +Create the following `example.proto` file. |
| 28 | + |
| 29 | +```proto |
| 30 | +syntax = "proto3"; |
| 31 | +
|
| 32 | +package helloworld; |
| 33 | +
|
| 34 | +message HelloWorld { |
| 35 | + string message = 1; |
| 36 | +} |
| 37 | +
|
| 38 | +service HelloService { |
| 39 | + rpc SayHello (HelloWorld) returns (HelloWorld); |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +You should be able to compile it using: |
| 44 | + |
| 45 | +``` |
| 46 | +mkdir lib |
| 47 | +protoc -I . --python_betterproto2_out=lib example.proto |
| 48 | +``` |
| 49 | + |
| 50 | +If you installed `protoc` with `grpc-tools`, the command will be: |
| 51 | + |
10 | 52 | ``` |
| 53 | +mkdir lib |
| 54 | +python -m grpc.tools.protoc -I . --python_betterproto2_out=lib example.proto |
| 55 | +``` |
| 56 | + |
| 57 | +#### Service compilation |
| 58 | + |
| 59 | +##### Clients |
| 60 | + |
| 61 | +By default, for each service, betterproto will generate a synchronous client. Both synchronous and asynchronous clients |
| 62 | +are supported. |
| 63 | + |
| 64 | + - Synchronous clients rely on the `grpcio` package. Make sure to enable the `grpcio` extra package when installing |
| 65 | + betterproto2 to use them. |
| 66 | + - Asynchronous clients rely on the `grpclib` package. Make sure to enable the `grpclib` extra package when installing |
| 67 | + betterproto2 to use them. |
11 | 68 |
|
12 | | -!!! info |
13 | | - The package is compatible with all Python versions from 3.10 to 3.13. |
| 69 | +To choose which clients to generate, use the `client_generation` option of betterproto. It supports the following |
| 70 | +values: |
14 | 71 |
|
| 72 | + - `none`: Clients are not generated. |
| 73 | + - `sync`: Only synchronous clients are generated. |
| 74 | + - `async`: Only asynchronous clients are generated. |
| 75 | + - `sync_async`: Both synchronous and asynchronous clients are generated. |
| 76 | + Asynchronous clients are generated with the Async suffix. |
| 77 | + - `async_sync`: Both synchronous and asynchronous clients are generated. |
| 78 | + Synchronous clients are generated with the Sync suffix. |
| 79 | + - `sync_async_no_default`: Both synchronous and asynchronous clients are generated. |
| 80 | + Synchronous clients are generated with the Sync suffix, and asynchronous clients are generated with the Async |
| 81 | + suffix. |
15 | 82 |
|
16 | | -## Compiling proto files |
| 83 | +For example, `protoc -I . --python_betterproto2_out=lib example.proto --python_betterproto2_opt=client_generation=async` |
| 84 | +will only generate asynchronous clients. |
17 | 85 |
|
18 | | -Follow the documentation of [betterproto2 compiler](https://betterproto.github.io/python-betterproto2-compiler/getting-started/) to compile your proto files. |
| 86 | +##### Servers |
| 87 | + |
| 88 | +By default, betterproto will not generate server base classes. To enable them, set the `server_generation` option to |
| 89 | +`async` with `--python_betterproto2_opt=server_generation=async`. |
| 90 | + |
| 91 | +These base classes will be asynchronous and rely on `grpclib`. To use them, make sure to install `betterproto2` with the |
| 92 | +`grpclib` extra package. |
| 93 | + |
| 94 | + |
| 95 | +## Installation |
| 96 | + |
| 97 | +The package `betterproto2` can be installed from PyPI using `pip`: |
| 98 | + |
| 99 | +```sh |
| 100 | +pip install betterproto2[all] |
| 101 | +``` |
19 | 102 |
|
20 | 103 | !!! warning |
21 | 104 | Make sure that the proto files were generated with a version of `betterproto2_compiler` that is compatible with your |
22 | 105 | version of `betterproto2`. |
23 | 106 |
|
24 | 107 | The version `0.x.y` of `betterproto` is compatible with the version `0.a.b` of the compiler if and only if `a=b`. |
25 | 108 |
|
26 | | - |
27 | 109 | ## Basic usage |
28 | 110 |
|
29 | 111 | If you successfuly compiled the `example.proto` file from the compiler documentation, you should now be able to use it! |
|
0 commit comments