pythonseleniumcommand-line-argumentsargparseinfluxdb-python

Python ArgumentParser to Influxdb-Client


I have a test script which sends test data to an Influxdb instance. To do so, I am utilizing InfluxDBClient to facilitate this. To make this script usable on my local machine as well as on my VM, I have setup Arguments.

The argument script looks like the below:

import argparse

parser = argparse.ArgumentParser(description='Setup for Automated Test')
parser.add_argument("--grafana_ip", dest="grafana_ip", action="store", default="localhost", help="The IP of the Grafana instance")
parser.add_argument("--grafana_port", dest="grafana_port", action="store", default="8086", help="The PORT of the Grafana instance")

args, unknown = parser.parse_known_args()

The test script is then setup as follows:

from argument_onstream import args

client = InfluxDBClient(host=args.grafana_ip, port=args.grafana_port, database='TEST')

*do test*

The issue I am getting is that when I run the test script from the command line as follows:

python test.py --grafana_ip="10.10.10.10" --grafana_port="8086"

I get the following error:

E           requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8086): Max retries exceeded with url: /write?db=TEST (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fd2011b42b0>: Failed to establish a new connection: [Errno 111] Connection refused'))

However, when I run a simple print within the test script I get the correct outputs:

10.10.10.10 8086

Does anyone have an idea of what could be going wrong?


Solution

  • To fix the issue I created a conftest.py file instead of using argparse.

    The issue was that because I was running a startup script (main.py), which called test.py, and within test.py I had imported variables from main.py. The argparse variables being set in the CL, were being reverted back to default when the test.py imported the main.py.

    Example below argparse.py

    import argparse
    
    parser = argparse.ArgumentParser(description='Setup for Automated Test')
    parser.add_argument("--grafana_ip", dest="grafana_ip", action="store", default="localhost", help="The IP of the Grafana instance")
    parser.add_argument("--grafana_port", dest="grafana_port", action="store", default="8086", help="The PORT of the Grafana instance")
    
    args, unknown = parser.parse_known_args()
    

    main.py

    from argparse import args
    
    *setup for test*
    

    test.py

    from main.py import stuff
    from argparse import args
    
    *do test*
    

    I believe this is a circular import issue, along with poorly written code.

    To correct the issue, I removed the main.py and argparse.py files. Added a conftest.py file, and allowed the conftest.py to pass variables to test.py.