README.rst 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. gRPC Python Tools
  2. =================
  3. Package for gRPC Python tools.
  4. Supported Python Versions
  5. -------------------------
  6. Python >= 3.6
  7. Installation
  8. ------------
  9. The gRPC Python tools package is available for Linux, Mac OS X, and Windows.
  10. Installing From PyPI
  11. ~~~~~~~~~~~~~~~~~~~~
  12. If you are installing locally...
  13. ::
  14. $ pip install grpcio-tools
  15. Else system wide (on Ubuntu)...
  16. ::
  17. $ sudo pip install grpcio-tools
  18. If you're on Windows make sure that you installed the :code:`pip.exe` component
  19. when you installed Python (if not go back and install it!) then invoke:
  20. ::
  21. $ pip.exe install grpcio-tools
  22. Windows users may need to invoke :code:`pip.exe` from a command line ran as
  23. administrator.
  24. n.b. On Windows and on Mac OS X one *must* have a recent release of :code:`pip`
  25. to retrieve the proper wheel from PyPI. Be sure to upgrade to the latest
  26. version!
  27. You might also need to install Cython to handle installation via the source
  28. distribution if gRPC Python's system coverage with wheels does not happen to
  29. include your system.
  30. Installing From Source
  31. ~~~~~~~~~~~~~~~~~~~~~~
  32. Building from source requires that you have the Python headers (usually a
  33. package named :code:`python-dev`) and Cython installed. It further requires a
  34. GCC-like compiler to go smoothly; you can probably get it to work without
  35. GCC-like stuff, but you may end up having a bad time.
  36. ::
  37. $ export REPO_ROOT=grpc # REPO_ROOT can be any directory of your choice
  38. $ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc $REPO_ROOT
  39. $ cd $REPO_ROOT
  40. $ git submodule update --init
  41. $ cd tools/distrib/python/grpcio_tools
  42. $ python ../make_grpcio_tools.py
  43. # For the next command do `sudo pip install` if you get permission-denied errors
  44. $ GRPC_PYTHON_BUILD_WITH_CYTHON=1 pip install .
  45. You cannot currently install Python from source on Windows. Things might work
  46. out for you in MSYS2 (follow the Linux instructions), but it isn't officially
  47. supported at the moment.
  48. Troubleshooting
  49. ~~~~~~~~~~~~~~~
  50. Help, I ...
  51. * **... see a** :code:`pkg_resources.VersionConflict` **when I try to install
  52. grpc**
  53. This is likely because :code:`pip` doesn't own the offending dependency,
  54. which in turn is likely because your operating system's package manager owns
  55. it. You'll need to force the installation of the dependency:
  56. :code:`pip install --ignore-installed $OFFENDING_DEPENDENCY`
  57. For example, if you get an error like the following:
  58. ::
  59. Traceback (most recent call last):
  60. File "<string>", line 17, in <module>
  61. ...
  62. File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 509, in find
  63. raise VersionConflict(dist, req)
  64. pkg_resources.VersionConflict: (six 1.8.0 (/usr/lib/python2.7/dist-packages), Requirement.parse('six>=1.10'))
  65. You can fix it by doing:
  66. ::
  67. sudo pip install --ignore-installed six
  68. * **... see compiler errors on some platforms when either installing from source or from the source distribution**
  69. If you see
  70. ::
  71. /tmp/pip-build-U8pSsr/cython/Cython/Plex/Scanners.c:4:20: fatal error: Python.h: No such file or directory
  72. #include "Python.h"
  73. ^
  74. compilation terminated.
  75. You can fix it by installing `python-dev` package. i.e
  76. ::
  77. sudo apt-get install python-dev
  78. If you see something similar to:
  79. ::
  80. third_party/protobuf/src/google/protobuf/stubs/mathlimits.h:173:31: note: in expansion of macro 'SIGNED_INT_MAX'
  81. static const Type kPosMax = SIGNED_INT_MAX(Type); \\
  82. ^
  83. And your toolchain is GCC (at the time of this writing, up through at least
  84. GCC 6.0), this is probably a bug where GCC chokes on constant expressions
  85. when the :code:`-fwrapv` flag is specified. You should consider setting your
  86. environment with :code:`CFLAGS=-fno-wrapv` or using clang (:code:`CC=clang`).
  87. Usage
  88. -----
  89. Given protobuf include directories :code:`$INCLUDE`, an output directory
  90. :code:`$OUTPUT`, and proto files :code:`$PROTO_FILES`, invoke as:
  91. ::
  92. $ python -m grpc_tools.protoc -I$INCLUDE --python_out=$OUTPUT --grpc_python_out=$OUTPUT $PROTO_FILES
  93. To use as a build step in distutils-based projects, you may use the provided
  94. command class in your :code:`setup.py`:
  95. ::
  96. setuptools.setup(
  97. # ...
  98. cmdclass={
  99. 'build_proto_modules': grpc_tools.command.BuildPackageProtos,
  100. }
  101. # ...
  102. )
  103. Invocation of the command will walk the project tree and transpile every
  104. :code:`.proto` file into a :code:`_pb2.py` file in the same directory.
  105. Note that this particular approach requires :code:`grpcio-tools` to be
  106. installed on the machine before the setup script is invoked (i.e. no
  107. combination of :code:`setup_requires` or :code:`install_requires` will provide
  108. access to :code:`grpc_tools.command.BuildPackageProtos` if it isn't already
  109. installed). One way to work around this can be found in our
  110. :code:`grpcio-health-checking`
  111. `package <https://pypi.python.org/pypi/grpcio-health-checking>`_:
  112. ::
  113. class BuildPackageProtos(setuptools.Command):
  114. """Command to generate project *_pb2.py modules from proto files."""
  115. # ...
  116. def run(self):
  117. from grpc_tools import command
  118. command.build_package_protos(self.distribution.package_dir[''])
  119. Now including :code:`grpcio-tools` in :code:`setup_requires` will provide the
  120. command on-setup as desired.
  121. For more information on command classes, consult :code:`distutils` and
  122. :code:`setuptools` documentation.