0

I am trying to parse arguments of database connection using argparse module in python from jupyter notebook.

Looked into this link : Argparse in Jupyter Notebook throws a TypeError

Please let me know if its duplicate question as I couldn't find the solution.

Here is the code:

parser = argparse.ArgumentParser(description='This script is used to create schemas in database with default privileges')
parser.add_argument("--dbname", "-d", default='abcd',  help="Database Name")
parser.add_argument("--hostname", "-H",    help="database Hostname")
parser.add_argument("--port", "-P",help="database Port")
parser.add_argument("--username", "-u",    help="database Username")
parser.add_argument("--password", "-p",    help="database Password")

args = parser.parse_args()

Error:

usage: ipykernel_launcher.py [-h] [--dbname DBNAME] [--hostname HOSTNAME]
                         [--port PORT] [--username USERNAME]
                         [--password PASSWORD]
ipykernel_launcher.py: error: unrecognized arguments: -f 
C:\*\*\AppData\Roaming\jupyter\runtime\kernel-def059d8-35af-4659-a9b2-a295f094ec66.json
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

Once this is done then I will pass this in this code:

    conn = psycopg2.connect(database=dbname,
                        host=hostname,
                        port=port,
                        user=username,
                        password=password,
                        sslmode='require')

Please let me know how to resolve this.

MAC
  • 1,345
  • 2
  • 30
  • 60
  • What don't you understand about my previous answer? You can't use `argparse` in a Notebook. The `sys.argv` that you see comes from the `ipython` launcher, and is not applicable to your particular notebook. You have to find some other way of specifying the parameters in the notebook. Notebooks are interactive! – hpaulj May 01 '20 at 21:23
  • ok. If it was a python `.py` file then how could I have followed the same steps. Can you help? – MAC May 02 '20 at 05:48

0 Answers0