0

Here is my code file with name is 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")

When I run this file then it show error :

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

Where is mistake in code? i could not understand?Can you some one help me out?

Jivan
  • 21,522
  • 15
  • 80
  • 131
csr
  • 69
  • 1
  • 7

2 Answers2

3

In py2neo v3, the create method (and all similar methods) take only a single argument, which can be any graphy object (see the manual page on types). You can therefore create multiple nodes by unioning them into a subgraph to pass as the argument. In your case, you'll want something like:

graph.create(node1 | node2 | node3)
Nigel Small
  • 4,475
  • 1
  • 17
  • 15
  • Thanks, can you suggest any source where i can learn py2neo version 3, in document there is no specific details to use code. I am reading a book that have only details of v2. – csr Jun 09 '16 at 04:30
  • I was unaware of any books that contained details of either version! The documentation I wrote for the project is here -> http://py2neo.org/v3/ – Nigel Small Jun 09 '16 at 12:41
1

This message:

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

refers to this call:

graph.create(node1, node2, node3)

The call is passing four arguments (counting the invocant graph as well as node1, node2, and node3) to the create method, but that method only expects two (including the invocant).

Upon reviewing the API documentation, I believe that you are using version 3 of py2neo (documented here), in which Graph.create takes only a single non-invocant parameter, but trying to call it as if it were version 2 (documented here), where it can take more than one.

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • Thanks @mark but No, its not accepting list arguments, create is method of Graph class and can accept more then one arguments. – csr Jun 07 '16 at 10:49
  • In py2neo v2, yes. You appear to be using py2neo v3. See edit. – Mark Reed Jun 07 '16 at 13:52