TypeError:create()需要2个位置参数,但是给出了4个

时间:2016-06-07 10:24:01

标签: python neo4j py2neo

这是我的代码文件,名称为CreateNode.py

#!/usr/bin/python
import py2neo

from py2neo import Graph, Node

def createNodeWithLabelProperties():
        print("Start Create label with prperties")
        py2neo.authenticate ("localhost:7474", "neo4j", "XXXXXXX")
        graph = Graph("http://localhost:7474/db/data")
        #Create Node with properties
        node1 = Node("LableFirst", name="Chuvindra Singh", age="27")
        #create Node with 2 label
        node2 = Node("Labelfirst", "LabelSecond",name="Koki Sing", age="27")
        node3 = Node("Labelk", "LabelB",name="Manzil", age="27")
        #now use Graph Object to create node
        resultNodes = graph.create(node1, node2, node3)
        for index in range(len(resultNodes)):
                print("Created Node - ", index, ", ", resultNodes[inedx])
                print("End Printing the node")

if __name__ =='__main__':
        print("start Creating nodes")
        createNodeWithLabelProperties()
        print("End Creating nodes")

当我运行此文件时,它会显示错误:

start Creating nodes
    Start Create label with prperties
    Traceback (most recent call last):
      File "CreateNode.py", line 23, in <module>
        createNodeWithLabelProperties()
      File "CreateNode.py", line 16, in createNodeWithLabelProperties
        resultNodes = graph.create(node1, node2, node3)
    TypeError: create() takes 2 positional arguments but 4 were given

代码中的错误在哪里?我无法理解?你能帮我一个人吗?

2 个答案:

答案 0 :(得分:3)

在py2neo v3中,create方法(以及所有类似的方法)只接受一个参数,可以是任何图形对象(参见manual page on types)。因此,您可以通过将它们合并到子图中来创建多个节点,以作为参数传递。在你的情况下,你会想要像:

graph.create(node1 | node2 | node3)

答案 1 :(得分:1)

此消息:

TypeError: create() takes 2 positional arguments but 4 were given

指的是这个电话:

graph.create(node1, node2, node3)

调用正在传递四个参数(将调用graph以及node1node2node3)计入create方法,但是该方法只需要两个(包括调用者)。

在查看API文档后,我相信您使用的是py2neo版本3(记录为here),其中Graph.create只接受一个非调用参数,但尝试将其称为如果它是版本2(记录为here),它可能需要不止一个。