PageSourceSearch

https://memgraph.com/docs/_next/static/chunks/pages/custom-query-modules/python-5755f7e2cc26e938.js

js memgraph.com collected 2026-10-02 04:45:35 UTC 25,506 bytes, 1 lines download raw bytes

1(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[263],{19598:(e,i,s)=>{(window.__NEXT_P=window.__NEXT_P||[]).push(["/custom-query-modules/python",function(){return s(98185)}])},98185:(e,i,s)=>{"use strict";s.r(i),s.d(i,{default:()=>o,useTOC:()=>d});var n=s(23798),h=s(28293),r=s(67106),t=s(19935);let a={src:"/docs/_next/static/media/memgraph_lab_query_modules.e7174bf0.png",height:813,width:1425,blurDataURL:"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAFCAMAAABPT11nAAAAGFBMVEX4+Pf19fT9/f3v7+//9Ove0cbg4ODX1tZKi+YOAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAJklEQVQImR3GuQ0AMBDDMMnOs//GwaUhCKKsPcoENMkhkZ+2td4HBbcAUM1YiDIAAAAASUVORK5CYII=",blurWidth:8,blurHeight:5};var l=s(86665);function d(e){return[{value:"Python API",id:"python-api",depth:2},{value:"Mock Python API",id:"mock-python-api",depth:3},{value:"Install external Python libraries",id:"install-external-python-libraries",depth:2},{value:"Example",id:"example",depth:2},{value:"Prerequisites",id:"prerequisites",depth:2},{value:"Developing a module",id:"developing-a-module",depth:2},{value:"Terminate procedure execution",id:"terminate-procedure-execution",depth:3},{value:"Importing, querying and testing a module",id:"importing-querying-and-testing-a-module",depth:2},{value:"Working with the mock API",id:"working-with-the-mock-api",depth:2},{value:"Managing Memgraph’s Python environment",id:"managing-memgraphs-python-environment",depth:2}]}let o=(0,h.e)(function(e){let{toc:i=d(e)}=e,s={a:"a",code:"code",h1:"h1",h2:"h2",h3:"h3",img:"img",li:"li",p:"p",pre:"pre",span:"span",strong:"strong",ul:"ul",...(0,t.R)(),...e.components};return(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)(s.h1,{children:"How to create a query module in Python"}),"\n",(0,n.jsxs)(s.p,{children:["The ",(0,n.jsx)(s.a,{href:"/custom-query-modules/python/python-api",children:"Python API"}),"\nprovided by Memgraph lets you develop query modules. It is accompanied by the\n",(0,n.jsx)(s.a,{href:"/custom-query-modules/python/mock-python-api",children:"mock\nAPI"}),",\nwhich makes it possible to develop and test query modules for Memgraph without\nhaving to run a Memgraph instance."]}),"\n",(0,n.jsx)(s.h2,{id:i[0].id,children:i[0].value}),"\n",(0,n.jsxs)(s.p,{children:["Python API is defined in the ",(0,n.jsx)(s.code,{children:"mgp"})," module that can be found in the Memgraph\ninstallation directory ",(0,n.jsx)(s.code,{children:"/usr/lib/memgraph/python_support"}),". In essence, it is a\nwrapper around the C API. If you wish to write your own query modules using the\nPython API, you need to have Python version ",(0,n.jsx)(s.code,{children:"3.5.0"})," or above installed."]}),"\n",(0,n.jsxs)(s.p,{children:["For more information, check the ",(0,n.jsx)(s.a,{href:"/custom-query-modules/python/python-api",children:"Python API reference\nguide"}),".",(0,n.jsx)("br",{}),"\nWe also made ",(0,n.jsx)(s.a,{href:"/custom-query-modules/python/python-example",children:"an example module"})," to\nhelp you start developing your own modules."]}),"\n",(0,n.jsxs)(s.p,{children:["You can ",(0,n.jsx)(s.a,{href:"/custom-query-modules/python/implement-custom-query-module-in-python",children:"develop query modules in Python from Memgraph\nLab"})," (v2.0\nand newer). Just navigate to ",(0,n.jsx)(s.strong,{children:"Query Modules"})," and click on ",(0,n.jsx)(s.strong,{children:"New Module"})," to\nstart."]}),"\n",(0,n.jsx)(s.p,{children:(0,n.jsx)(s.img,{placeholder:"blur",src:a})}),"\n",(0,n.jsxs)(s.p,{children:["Custom modules developed via Memgraph Lab are located at\n",(0,n.jsx)(s.code,{children:"/var/lib/memgraph/internal_modules"}),"."]}),"\n",(0,n.jsx)(s.h3,{id:i[1].id,children:i[1].value}),"\n",(0,n.jsxs)(s.p,{children:["The ",(0,n.jsx)(s.a,{href:"/custom-query-modules/python/mock-python-api",children:"mock Python query module API"}),"\nenables you to develop and test query modules for Memgraph without having to run\na Memgraph instance by simulating its behavior. As the mock API is compatible\nwith the ",(0,n.jsx)(s.a,{href:"/custom-query-modules/python/python-api",children:"Python API"}),", you can add\nmodules developed with it to Memgraph as-is, without modifying the code."]}),"\n",(0,n.jsxs)(s.p,{children:["For more information and examples, check the\n",(0,n.jsx)(s.a,{href:"/custom-query-modules/python/mock-python-api",children:"mock Python API reference guide"}),"."]}),"\n",(0,n.jsx)(s.h2,{id:i[2].id,children:i[2].value}),"\n",(0,n.jsxs)(s.p,{children:["It is possible to install Python libraries that are not already included with\nMemgraph installation. For example, to install ",(0,n.jsx)(s.code,{children:"pandas"})," with Memgraph running\ninside a Docker container, run the following command in the terminal:"]}),"\n",(0,n.jsx)(s.pre,{tabIndex:"0","data-language":"plaintext","data-word-wrap":"","data-copy":"",children:(0,n.jsxs)(s.code,{children:[(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{children:'docker exec -i -u memgraph <container_id> bash -c "apt install -y python3-pip &&'})}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{children:'pip install pandas"'})})]})}),"\n",(0,n.jsxs)(s.p,{children:["Don’t forget to replace ",(0,n.jsx)(s.code,{children:"<container_id>"})," with the appropriate value, which you can\nfind by running ",(0,n.jsx)(s.code,{children:"docker ps"})," command in the terminal."]}),"\n",(0,n.jsxs)(s.p,{children:["If you are starting Memgraph with ",(0,n.jsx)(s.a,{href:"/getting-started/install-memgraph/docker-compose",children:"Docker\nCompose"}),", write the following\ncommands in the Dockerfile:"]}),"\n",(0,n.jsx)(s.pre,{tabIndex:"0","data-language":"plaintext","data-word-wrap":"","data-copy":"",children:(0,n.jsxs)(s.code,{children:[(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{children:"FROM memgraph/memgraph:latest"})}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{})}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{children:"USER memgraph"})}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{})}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{children:"RUN apt install -y python3-pip"})}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{children:"RUN pip install pandas"})}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{})})]})}),"\n",(0,n.jsxs)(s.p,{children:["Python libraries should now be installed as the ",(0,n.jsx)(s.code,{children:"memgraph"})," user, which is the\ndefault user inside the Memgraph container. You no longer need to switch to the\n",(0,n.jsx)(s.code,{children:"root"})," user to perform installations."]}),"\n",(0,n.jsx)(s.h2,{id:i[3].id,children:i[3].value}),"\n",(0,n.jsxs)(s.p,{children:["In this tutorial, we will learn how to develop a query module in\nPython on the example of the ",(0,n.jsx)(s.strong,{children:"random walk algorithm"}),"."]}),"\n",(0,n.jsx)(s.h2,{id:i[4].id,children:i[4].value}),"\n",(0,n.jsx)(s.p,{children:"There are three options for installing and working with Memgraph MAGE:"}),"\n",(0,n.jsxs)(s.ul,{children:["\n",(0,n.jsx)(s.li,{children:(0,n.jsx)(s.a,{href:"/custom-query-modules#install-mage-with-docker-hub",children:"install MAGE development environment from Docker Hub"})}),"\n",(0,n.jsx)(s.li,{children:(0,n.jsxs)(s.a,{href:"/custom-query-modules#install-mage-with-docker-build",children:["build MAGE using the ",(0,n.jsx)(s.code,{children:"docker build"})," command"]})}),"\n",(0,n.jsx)(s.li,{children:(0,n.jsx)(s.a,{href:"/custom-query-modules#install-mage-from-source",children:"build MAGE from source"})}),"\n"]}),"\n",(0,n.jsx)(s.h2,{id:i[5].id,children:i[5].value}),"\n",(0,n.jsxs)(s.p,{children:["Position yourself in the ",(0,n.jsx)(s.strong,{children:"MAGE repository"})," you cloned earlier. Specifically,\ngo in the ",(0,n.jsx)(s.code,{children:"python"})," subdirectory and create a new file named ",(0,n.jsx)(s.code,{children:"random_walk.py"}),"."]}),"\n",(0,n.jsx)(s.pre,{tabIndex:"0","data-language":"plaintext","data-word-wrap":"","data-copy":"",children:(0,n.jsxs)(s.code,{children:[(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{children:"mage"})}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{children:"└── python"})}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{children:"    └── random_walk.py"})}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{})})]})}),"\n",(0,n.jsxs)(s.p,{children:["For coding the query module, we’ll use the\n",(0,n.jsx)(s.a,{href:"https://github.com/memgraph/mgp",children:(0,n.jsx)(s.code,{children:"mgp"})})," package that has the Memgraph Python\nAPI including the key graph data structures:\n",(0,n.jsx)(s.a,{href:"https://github.com/memgraph/mgp/blob/main/mgp.py#L260",children:(0,n.jsx)(s.strong,{children:"Vertex"})})," and\n",(0,n.jsx)(s.a,{href:"https://github.com/memgraph/mgp/blob/main/mgp.py#L182",children:(0,n.jsx)(s.strong,{children:"Edge"})}),".\nTo install ",(0,n.jsx)(s.code,{children:"mgp"}),", run ",(0,n.jsx)(s.code,{children:"pip install mgp"}),"."]}),"\n",(0,n.jsx)(s.p,{children:"Here’s the code for the random walk algorithm:"}),"\n",(0,n.jsx)(s.pre,{icon:l.un,tabIndex:"0","data-language":"python","data-word-wrap":"","data-copy":"",children:(0,n.jsxs)(s.code,{children:[(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"import"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:" mgp"})]}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"import"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:" random"})]}),"\n",(0,n.jsx)(s.span,{children:" "}),"\n",(0,n.jsx)(s.span,{children:" "}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{style:{"--shiki-light":"#6F42C1","--shiki-dark":"#B392F0"},children:"@mgp.read_proc"})}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"def"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#6F42C1","--shiki-dark":"#B392F0"},children:" get_path"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"("})]}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"    start: mgp.Vertex,"})}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"    length: "}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#005CC5","--shiki-dark":"#79B8FF"},children:"int"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:" ="}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#005CC5","--shiki-dark":"#79B8FF"},children:" 10"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:","})]}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:") -> mgp.Record("}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#E36209","--shiki-dark":"#FFAB
170"},children:"path"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"="}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"mgp.Path):"})]}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{style:{"--shiki-light":"#032F62","--shiki-dark":"#9ECBFF"},children:'    """Generates a random path of length `length` or less starting'})}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{style:{"--shiki-light":"#032F62","--shiki-dark":"#9ECBFF"},children:"    from the `start` vertex."})}),"\n",(0,n.jsx)(s.span,{children:" "}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{style:{"--shiki-light":"#032F62","--shiki-dark":"#9ECBFF"},children:"    :param mgp.Vertex start: The starting node of the walk."})}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{style:{"--shiki-light":"#032F62","--shiki-dark":"#9ECBFF"},children:"    :param int length: The number of edges to traverse."})}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{style:{"--shiki-light":"#032F62","--shiki-dark":"#9ECBFF"},children:"    :return: Random path."})}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{style:{"--shiki-light":"#032F62","--shiki-dark":"#9ECBFF"},children:"    :rtype: mgp.Record(mgp.Path)"})}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{style:{"--shiki-light":"#032F62","--shiki-dark":"#9ECBFF"},children:'    """'})}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"    path "}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"="}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:" mgp.Path(start)"})]}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"    vertex "}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"="}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:" start"})]}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"    for"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:" _ "}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"in"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#005CC5","--shiki-dark":"#79B8FF"},children:" range"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"(length):"})]}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"        try"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:":"})]}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"            edge "}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"="}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:" random.choice("}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#005CC5","--shiki-dark":"#79B8FF"},children:"list"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"(vertex.out_edges))"})]}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"            path.expand(edge)"})}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"            vertex "}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"="}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:" edge.to_vertex"})]}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"        except"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#005CC5","--shiki-dark":"#79B8FF"},children:" IndexError"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:":"})]}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"            break"})}),"\n",(0,n.jsx)(s.span,{children:" "}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"    return"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:" mgp.Record("}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#E36209","--shiki-dark":"#FFAB
170"},children:"path"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"="}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"path)"})]})]})}),"\n",(0,n.jsxs)(s.p,{children:["The ",(0,n.jsx)(s.code,{children:"get_path"})," is decorated with the ",(0,n.jsx)(s.code,{children:"@mgp.read_proc"})," decorator, which tells\nMemgraph it’s a ",(0,n.jsx)(s.code,{children:"read"})," procedure, meaning it won’t make changes to the graph.\nThe path is created from the ",(0,n.jsx)(s.code,{children:"start"})," node, and edges are appended to it\niteratively."]}),"\n",(0,n.jsx)(s.h3,{id:i[6].id,children:i[6].value}),"\n",(0,n.jsxs)(s.p,{children:["Just as the execution of a Cypher query can be terminated with ",(0,n.jsx)(s.a,{href:"/fundamentals/transactions",children:(0,n.jsx)(s.code,{children:'TERMINATE TRANSACTIONS "id";'})})," query,\nthe execution of the procedure can as well, if it takes too long to yield a\nresponse or gets stuck in an infinite loop due to unpredicted input data."]}),"\n",(0,n.jsx)(s.p,{children:"Transaction ID is visible upon calling the SHOW TRANSACTIONS; query."}),"\n",(0,n.jsxs)(s.p,{children:["In order to be able to terminate the procedure, it has to contain function\n",(0,n.jsx)(s.code,{children:"ctx.check_must_abort()"})," which precedes crucial parts of the code, such as\n",(0,n.jsx)(s.code,{children:"while"})," and ",(0,n.jsx)(s.code,{children:"until"})," loops, or similar points where the procedure might become\ncostly."]}),"\n",(0,n.jsx)(s.p,{children:"Consider the following example:"}),"\n",(0,n.jsx)(s.pre,{icon:l.un,tabIndex:"0","data-language":"python","data-word-wrap":"","data-copy":"",children:(0,n.jsxs)(s.code,{children:[(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"import"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:" mgp"})]}),"\n",(0,n.jsx)(s.span,{children:" "}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{style:{"--shiki-light":"#6F42C1","--shiki-dark":"#B392F0"},children:"@mgp.read_proc"})}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"def"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#6F42C1","--shiki-dark":"#B392F0"},children:" long_query"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"(ctx: mgp.ProcCtx) -> mgp.Record("}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#E36209","--shiki-dark":"#FFAB70"},children:"my_id"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"="}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#005CC5","--shiki-dark":"#79B8FF"},children:"int"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"):"})]}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#005CC5","--shiki-dark":"#79B8FF"},children:"    id"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:" ="}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#005CC5","--shiki-dark":"#79B8FF"},children:" 1"})]}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"    try"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:":"})]}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"        while"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#005CC5","--shiki-dark":"#79B8FF"},children:" True"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:":"})]}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"            if"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:" ctx.check_must_abort():"})]}),"\n",(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"                break"})}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#005CC5","--shiki-dark":"#79B8FF"},children:"            id"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:" +="}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#005CC5","--shiki-dark":"#79B8FF"},children:" 1"})]}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"    except"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:" mgp.AbortError:"})]}),"\n",(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"        return"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:" mgp.Record("}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#E36209","--shiki-dark":"#FFAB
170"},children:"my_id"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"="}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#005CC5","--shiki-dark":"#79B8FF"},children:"id"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:")"})]})]})}),"\n",(0,n.jsxs)(s.p,{children:["The ",(0,n.jsx)(s.code,{children:"mgp.AbortError:"})," ensures that the correct message about termination is sent\nto the session where the procedure was originally run."]}),"\n",(0,n.jsx)(s.h2,{id:i[7].id,children:i[7].value}),"\n",(0,n.jsxs)(s.p,{children:["Now in order to import, query and test a module, check out the ",(0,n.jsx)(s.a,{href:"/custom-query-modules/manage-query-modules",children:"following\npage"}),"."]}),"\n",(0,n.jsxs)(s.p,{children:["Feel free to create an issue or open a pull request on our ",(0,n.jsx)(s.a,{href:"https://github.com/memgraph/memgraph",children:"GitHub\nrepo"})," to speed up the development.",(0,n.jsx)("br",{}),"\nAlso, don’t forget to throw us a star on GitHub. :star:"]}),"\n",(0,n.jsx)(s.h2,{id:i[8].id,children:i[8].value}),"\n",(0,n.jsxs)(s.p,{children:["The\n",(0,n.jsx)(s.a,{href:"/custom-query-modules/python/mock-python-api",children:"mock Python API"}),"\nlets you develop and test query modules for Memgraph without having to run a\nMemgraph instance. As it’s compatible with the Python API you can add modules\ndeveloped with it to Memgraph as-is, without having to refactor your code."]}),"\n",(0,n.jsxs)(s.p,{children:["The documentation on importing the mock API and running query modules with it\nis available\n",(0,n.jsx)(s.a,{href:"/custom-query-modules/python/mock-python-api#using-the-mock-api",children:"here"}),",\naccompanied by examples."]}),"\n",(0,n.jsx)(s.h2,{id:i[9].id,children:i[9].value}),"\n",(0,n.jsxs)(s.p,{children:["After some time, any production system requires updates for the packages it\nuses. For example, when developing a new query module that requires the latest\n",(0,n.jsx)(s.code,{children:"networkx"})," version."]}),"\n",(0,n.jsxs)(s.p,{children:["If Memgraph is already deployed somewhere with an installed ",(0,n.jsx)(s.code,{children:"networkx"})," package,\nyou would probably like to use some package manager, e.g. pip or conda, to\ndelete the old ",(0,n.jsx)(s.code,{children:"networkx"}),", and install a new ",(0,n.jsx)(s.code,{children:"networkx"})," package. You definitely\nwouldn’t want to redeploy the whole Memgraph just because of one Python package."]}),"\n",(0,n.jsxs)(s.p,{children:["However, Python caches all modules, packages and the compiled bytecode, so this\nprocedure cannot work out of the box. So after installing the new package, you\nneed to use the utility procedure ",(0,n.jsx)(s.code,{children:"mg.load_all()"}),"."]}),"\n",(0,n.jsx)(s.p,{children:"So the whole process looks like this:"}),"\n",(0,n.jsx)(s.p,{children:"Uninstall the old package:"}),"\n",(0,n.jsx)(s.pre,{icon:l.un,tabIndex:"0","data-language":"python","data-word-wrap":"","data-copy":"",children:(0,n.jsx)(s.code,{children:(0,n.jsx)(s.span,{children:(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"pip uninstall networkx"})})})}),"\n",(0,n.jsx)(s.p,{children:"Install a new package:"}),"\n",(0,n.jsx)(s.pre,{icon:l.un,tabIndex:"0","data-language":"python","data-word-wrap":"","data-copy":"",children:(0,n.jsx)(s.code,{children:(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"pip install networkx"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"=<"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:"new_version"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:">"})]})})}),"\n",(0,n.jsx)(s.p,{children:"Reload all query modules:"}),"\n",(0,n.jsx)(s.pre,{tabIndex:"0","data-language":"cypher","data-word-wrap":"","data-copy":"",children:(0,n.jsx)(s.code,{children:(0,n.jsxs)(s.span,{children:[(0,n.jsx)(s.span,{style:{"--shiki-light":"#D73A49","--shiki-dark":"#F97583"},children:"CALL"}),(0,n.jsx)(s.span,{style:{"--shiki-light":"#24292E","--shiki-dark":"#E1E4E8"},children:" mg.load_all();"})]})})})]})},"/custom-query-modules/python",{filePath:"pages/custom-query-modules/python.mdx",timestamp:1789565618e3,pageMap:r.O,frontMatter:{title:"How to create a query module in Python",description:"Seamlessly develop query modules with Memgraph's Python API. You don't have to run a Memgraph instance. Curious to learn more? Head over to our documentation."},title:"How to create a query module in Python"},"undefined"==typeof RemoteContent?d:RemoteContent.useTOC)}},e=>{e.O(0,[8293,7106,636,6593,8792],()=>e(e.s=19598)),_N_E=e.O()}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.