GRPC的protobuf代码生成器
grpcio-tools的Python项目详细描述
grpc python工具包。
支持的Python版本
python=3.5
不推荐的python版本
python==2.7。Python2.7支持将于2020年1月1日取消。
安装
grpc python工具包可用于linux、mac os x和windows 运行Python2.7。
从pypi安装
如果在本地安装…
$ pip install grpcio-tools
其他系统范围(在ubuntu上)…..
$ sudo pip install grpcio-tools
如果您在Windows上,请确保安装了pip.exe
组件
安装python时(如果不回去安装!)然后调用:
$ pip.exe install grpcio-tools
Windows用户可能需要从运行为的命令行调用pip.exe
管理员。
N.B.在Windows和Mac OS X上,必须有一个最新版本的pip
从pypi中检索正确的轮子。一定要升级到最新版本
版本!
您可能还需要安装cython来通过源代码处理安装 如果grpc python的系统覆盖率没有发生 包括你的系统。
从源安装
从源代码构建需要有python头(通常是
已安装名为python-dev
)和cython的包。它还需要
像gcc一样的编译器可以顺利地运行;您可能不需要
海合会喜欢的东西,但你可能会有一个糟糕的时间。
$ export REPO_ROOT=grpc # REPO_ROOT can be any directory of your choice $ git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc $REPO_ROOT $ cd $REPO_ROOT $ git submodule update --init $ cd tools/distrib/python/grpcio_tools $ python ../make_grpcio_tools.py # For the next command do `sudo pip install` if you get permission-denied errors $ GRPC_PYTHON_BUILD_WITH_CYTHON=1 pip install .
当前无法在Windows上从源代码安装Python。事情可能会成功 在msys2中为您提供(遵循linux的说明),但它并不是正式的 目前支持。
故障排除
帮助,我…
…尝试安装时请参见a
pkg_resources.VersionConflict
。 GRPC这可能是因为
pip
不拥有有问题的依赖项, 这可能是因为您的操作系统的包管理器拥有 它。您需要强制安装依赖项:pip install --ignore-installed $OFFENDING_DEPENDENCY
例如,如果出现如下错误:
Traceback (most recent call last): File "<string>", line 17, in <module> ... File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 509, in find raise VersionConflict(dist, req) pkg_resources.VersionConflict: (six 1.8.0 (/usr/lib/python2.7/dist-packages), Requirement.parse('six>=1.10'))
您可以通过执行以下操作来修复它:
sudo pip install --ignore-installed six
…从源安装或从源发行版安装时,请参阅某些平台上的编译器错误
如果您看到
/tmp/pip-build-U8pSsr/cython/Cython/Plex/Scanners.c:4:20: fatal error: Python.h: No such file or directory #include "Python.h" ^ compilation terminated.
您可以通过安装python dev包来修复它。即
sudo apt-get install python-dev
如果您看到类似的内容:
third_party/protobuf/src/google/protobuf/stubs/mathlimits.h:173:31: note: in expansion of macro 'SIGNED_INT_MAX' static const Type kPosMax = SIGNED_INT_MAX(Type); \\ ^
您的工具链是gcc(在编写本文时,至少在 gcc 6.0),这可能是gcc阻塞常量表达式的错误。 当指定
-fwrapv
标志时。你应该考虑设置 使用CFLAGS=-fno-wrapv
或使用clang(CC=clang
)的环境。
用法
给定protobuf包含目录$INCLUDE
,输出目录
$OUTPUT
,和proto文件$PROTO_FILES
,调用为:
$ python -m grpc.tools.protoc -I$INCLUDE --python_out=$OUTPUT --grpc_python_out=$OUTPUT $PROTO_FILES
要在基于distutils的项目中用作构建步骤,可以使用
命令类在您的setup.py
:
setuptools.setup( # ... cmdclass={ 'build_proto_modules': grpc.tools.command.BuildPackageProtos, } # ... )
调用该命令将遍历项目树并将
.proto
文件放入同一目录中的_pb2.py
文件中。
注意,这种特殊的方法要求grpcio-tools
在调用安装脚本之前安装在计算机上(即没有
组合setup_requires
或install_requires
将提供
如果还没有访问grpc.tools.command.BuildPackageProtos
已安装)。解决这个问题的一种方法是
grpcio-health-checking
package:
class BuildPackageProtos(setuptools.Command): """Command to generate project *_pb2.py modules from proto files.""" # ... def run(self): from grpc.tools import command command.build_package_protos(self.distribution.package_dir[''])
现在在setup_requires
中包含grpcio-tools
将提供
根据需要设置命令。
有关命令类的更多信息,请参阅distutils
和
setuptools
文档。