• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1Use in Python    {#flatbuffers_guide_use_python}
2=============
3
4## Before you get started
5
6Before diving into the FlatBuffers usage in Python, it should be noted that the
7[Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide to general
8FlatBuffers usage in all of the supported languages (including Python). This
9page is designed to cover the nuances of FlatBuffers usage, specific to
10Python.
11
12You should also have read the [Building](@ref flatbuffers_guide_building)
13documentation to build `flatc` and should be familiar with
14[Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and
15[Writing a schema](@ref flatbuffers_guide_writing_schema).
16
17## FlatBuffers Python library code location
18
19The code for the FlatBuffers Python library can be found at
20`flatbuffers/python/flatbuffers`. You can browse the library code on the
21[FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/
22python).
23
24## Testing the FlatBuffers Python library
25
26The code to test the Python library can be found at `flatbuffers/tests`.
27The test code itself is located in [py_test.py](https://github.com/google/
28flatbuffers/blob/master/tests/py_test.py).
29
30To run the tests, use the [PythonTest.sh](https://github.com/google/flatbuffers/
31blob/master/tests/PythonTest.sh) shell script.
32
33*Note: This script requires [python](https://www.python.org/) to be
34installed.*
35
36## Using the FlatBuffers Python library
37
38*Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth
39example of how to use FlatBuffers in Python.*
40
41There is support for both reading and writing FlatBuffers in Python.
42
43To use FlatBuffers in your own code, first generate Python classes from your
44schema with the `--python` option to `flatc`. Then you can include both
45FlatBuffers and the generated code to read or write a FlatBuffer.
46
47For example, here is how you would read a FlatBuffer binary file in Python:
48First, import the library and the generated code. Then read a FlatBuffer binary
49file into a `bytearray`, which you pass to the `GetRootAsMonster` function:
50
51~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.py}
52    import MyGame.Example as example
53    import flatbuffers
54
55    buf = open('monster.dat', 'rb').read()
56    buf = bytearray(buf)
57    monster = example.GetRootAsMonster(buf, 0)
58~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59
60Now you can access values like this:
61
62~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.py}
63    hp = monster.Hp()
64    pos = monster.Pos()
65~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66
67## Text Parsing
68
69There currently is no support for parsing text (Schema's and JSON) directly
70from Python, though you could use the C++ parser through SWIG or ctypes. Please
71see the C++ documentation for more on text parsing.
72
73<br>
74