What does 'TypeError: connect() takes from 0 to 1 positional arguments but 3 were given' with python-oracledb 1.0 mean?

1.3k Views Asked by At

The python-oracledb 1.0 code:

import oracledb
import os

un = os.environ.get("PYTHON_USERNAME")
pw = os.environ.get("PYTHON_PASSWORD")
cs = "localhost/orclpdb1"

c = oracledb.connect(un, pw, cs)

gives the error:

TypeError: connect() takes from 0 to 1 positional arguments but 3 were given

The same error occurs when creating a connection pool:

pool = oracledb.create_pool(un, pw, cs, min=4, max=4)

or

pool = oracledb.SessionPool(un, pw, cs, min=4, max=4)

How do I fix this?

[Update: in python-oracledb 1.0.0 the message started with 'TypeError: wrapped() takes ...'. In version 1.0.1 the 'wrapped' was changed to the name of the python-oracledb API that had the error.]

1

There are 1 best solutions below

0
On BEST ANSWER

The error will come with a line number that points you at the connection or pool creation call. The solution is to use keyword parameters like:

c = oracledb.connect(user=un, password=pw, dsn=cs)

or

pool = oracledb.create_pool(user=un, password=pw, dsn=cs, min=4, max=4)

The oracledb.connect() and oracledb.create_pool() (and deprecated oracledb.SessionPool()) parameters are keyword, not positional. This complies with the Python Database API spec PEP 249.

Although the solution is to use keyword parameters, the message mentions positional parameters which seems contradictory. This is because PEP 249 allows one positional parameter like c = oracledb.connect("un/pw@cs"). Your original call used positional parameters so the driver assumed that's the argument style you were trying to use - but only one positional argument is allowed so you got an error. I don't recommend using "un/pw@cs" because there can be parsing problems if your passwords contains "/" or "@".