Home > Mobile >  TypeError session() - passing one argument but Python thinks I'm passing two
TypeError session() - passing one argument but Python thinks I'm passing two

Time:07-18

I'm getting this error in Python when trying to communicate to a Neo4j browser:

session = driver.session("WRITE")
TypeError: session() takes 1 positional argument but 2 were given

Does anyone know why it's showing an error for two arguments when there is only one?

CodePudding user response:

If you're trying to write using a session, I don't believe there is a need to add arguments. The extra argument provided is just self which is implied. Most of the docs reference using with to open and automatically close the session as needed, with the write_transaction() function to do any writing:

with driver.session() as session:
    session.write_transaction(add_friend, "Arthur", "Guinevere")
    session.write_transaction(add_friend, "Arthur", "Lancelot")
    session.write_transaction(add_friend, "Arthur", "Merlin")
    session.read_transaction(print_friends, "Arthur")

source: https://github.com/neo4j/neo4j-python-driver

CodePudding user response:

session() is a method in the Driver class. It is defined the source as:

    def session(self, **config):
        ...

The method takes one positional argument, self, and then only accepts keyword arguments, **config. Python will automatically pass self as the first positional argument. So when you call it with:

driver.session("WRITE")

You are calling session() with 2 positional arguments, self and "WRITE", which is why you are getting the error, TypeError: session() takes 1 positional argument but 2 were given.

Take a look at the Neo4j Python driver documentation for usage:

https://neo4j.com/docs/api/python-driver/current/api.html#session-construction

You can read more about self in the python docs:

https://docs.python.org/3/tutorial/classes.html?highlight=self#random-remarks

  • Related