Source: https://dev-docs.yueshu.com.cn/v5.3.2/clients/python-client/ Documentation version: v5.3.2 # Python SDK 指南 本文介绍如何使用 [Python SDK](https://github.com/vesoft-inc/nebula-python/tree/v5.3.0) 连接悦数图数据库开发版并执行简单查询。 需要从安装依赖到验证应用接口的完整流程,请参见[使用 Python SDK 构建查询服务](https://dev-docs.yueshu.com.cn/v5.3.2/app-development/python-sdk/)。 ## 前提条件 - 已安装 Python 3.10 或更高版本。 - 对于本地部署用户,悦数图数据库开发版服务组正在运行中。更多信息,请参见[快速入门](https://dev-docs.yueshu.com.cn/v5.3.2/get-started/deploy/)。 ## 操作步骤 1. 通过以下任一方式安装 SDK: - 从 [PyPI](https://pypi.org/project/nebula5-python/) 安装: ```` pip install nebula5_python==5.3.0 ```` - 从源码安装: ```` git clone -b dev https://github.com/vesoft-inc/nebula-python.git cd nebula-python pip install -e . ```` 2. 使用 SDK 连接悦数图数据库开发版并执行查询: ```` from nebulagraph_python.client import NebulaClient # Create client client = NebulaClient( addresses=":", user_name="", password="", ) query = """ RETURN 1 AS a, 2 AS b """ # Execute query try: result = client.execute(query) finally: client.close() ```` 参数`:`、``和``须替换为实际的连接信息。 3. 若要检查查询结果,可参考以下方法: - 以表格形式打印结果 ```` result.print() ```` Return: ```` ╔═══╤═══╗ ║ │ ║ ║ a │ b ║ ║ │ ║ ╟───┼───╢ ║ │ ║ ║ 1 │ 2 ║ ║ │ ║ ╚═══╧═══╝ Summary ├── Rows: 1 └── Latency: 1450μs ```` - 从查询集中检索特定值 ```` # Get one row row = result.one() # Get one value cell = row["a"].cast_primitive() # Print its value print(cell, type(cell)) ```` Return: ```` 1 ```` - 从结果集中检索原始值 ```` print(result.as_primitive_by_column()) print(list(result.as_primitive_by_row())) ```` Return: ```` {'a': [1], 'b': [2]} [{'a': 1, 'b': 2}] ```` - 从查询集中检索 Pandas DataFrame ```` result = client.execute(query) df = result.as_pandas_df() ```` Return: ```` a b 0 1 2 ```` ## 更多信息 - [Python SDK GitHub 仓库](https://github.com/vesoft-inc/nebula-python/tree/dev) - [Python SDK API 文档](https://vesoft-inc.github.io/nebula-python/5.3.0/annotated.html) - [多进程并发查询](https://github.com/vesoft-inc/nebula-python/blob/dev/example/MultiProcessAsyncClientExample.py)