Gremlin-Python
Gremlin-Python is the Python Language Variant (GLV) for Apache TinkerPop, a graph computing framework. It enables users to express complex graph traversals using Python syntax, connecting to any TinkerPop-enabled graph system (like Gremlin Server or Amazon Neptune). The library is actively maintained and releases are typically aligned with major Apache TinkerPop versions, with the current version being 3.8.0.
Warnings
- gotcha Always close the `DriverRemoteConnection`. Failing to do so can lead to 'Unclosed Client Session' errors and resource leaks, especially in long-running applications or when repeatedly creating connections. Use a `try...finally` block or a context manager if available (though a direct `close()` call is most common for `DriverRemoteConnection`).
- gotcha Some Gremlin step names are Python reserved keywords (e.g., `in`, `and`, `as`, `from`, `is`, `not`, `or`, `set`). These steps must be suffixed with an underscore (`_`) in Gremlin-Python (e.g., `g.V().in_()`, `__.not_()`).
- gotcha Gremlin-Python traversals are lazy; they do not execute until a terminal step is called. Common terminal steps include `.next()`, `.toList()`, `.toSet()`, or `.iterate()`. Forgetting a terminal step means your traversal will not be sent to the server.
- breaking The `has(key, traversal)` step was removed due to its confusing behavior where it checked if the traversal yielded *any* results, not if the property's value matched the traversal's condition. This is a breaking change.
- breaking The semantics of the `repeat()` step changed to consistently treat its child traversal with global semantics, affecting how traversers are handled across loop iterations.
- breaking The `limit(local)`, `range(local)`, and `tail(local)` steps now consistently return a collection (e.g., a list) when operating on an iterable, even if the result size is one. Previously, a single result would be 'unboxed' and returned directly.
- breaking Apache TinkerPop 3.8.0 (and thus Gremlin-Python 3.8.0) requires Python 3.10 or newer. Support for earlier Python versions has been dropped. Additionally, Jython support was entirely removed, meaning native Python lambdas no longer execute in Gremlin Server; use Gremlin-Groovy for server-side lambdas.
Install
-
pip install gremlinpython
Imports
- DriverRemoteConnection
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
- traversal
from gremlin_python.process.anonymous_traversal import traversal
- __
from gremlin_python.process.graph_traversal import __
- statics.load_statics(globals())
from gremlin_python import statics statics.load_statics(globals())
Quickstart
import os
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
from gremlin_python.process.anonymous_traversal import traversal
GREMLIN_SERVER_URL = os.environ.get('GREMLIN_SERVER_URL', 'ws://localhost:8182/gremlin')
# Establish a remote connection to the Gremlin Server
connection = None
try:
connection = DriverRemoteConnection(GREMLIN_SERVER_URL, 'g')
g = traversal().withRemote(connection)
# Example Traversal: Get the count of all vertices
vertex_count = g.V().count().next()
print(f"Number of vertices: {vertex_count}")
# Add a vertex and then query it
new_vertex = g.addV('person').property('name', 'Alice').next()
print(f"Added vertex: {new_vertex.id} - {new_vertex.label}")
alice_name = g.V(new_vertex.id).values('name').next()
print(f"Name of added vertex: {alice_name}")
finally:
# Ensure the connection is closed to release resources
if connection:
connection.close()