0

I have the following script where I utilize @click.option from the click library in Python. I want to know how to replace it with @click.argument without changing the output.

import sys
import click
import pandas as pd
from sklearn import metrics
@click.command()

@click.option('--filename')
# @click.argument('result.csv', type=click.Path(exists=True))

def main(filename):

    df = pd.read_csv(filename)
    y_test = df["actual"].values
    y_pred = df["predicted"].values

    print('R Squared: ' +  str(metrics.r2_score(y_test, y_pred)))
    
if __name__ == '__main__':
    sys.argv = ['',  '--filename', 'result.csv']
    main()
1
  • Your commented out was pretty near. See what I answered and give me feedback if it helped. Commented Jun 20, 2024 at 22:56

2 Answers 2

1
import sys
import click
import pandas as pd
from sklearn import metrics
@click.command()

# @click.option('--filename')
## @click.argument('result.csv', type=click.Path(exists=True))
@click.argument('filename', type=click.Path(exists=True))
def main(filename):

    df = pd.read_csv(filename)
    y_test = df["actual"].values
    y_pred = df["predicted"].values

    print('R Squared: ' +  str(metrics.r2_score(y_test, y_pred)))
    
if __name__ == '__main__':
    # sys.argv = ['',  '--filename', 'result.csv']
    sys.argv = ['',  'result.csv']
    main()
Sign up to request clarification or add additional context in comments.

3 Comments

I tried the code, it prints out the same output, but with the following error message afterwards : ERROR:root:Internal Python error in the inspect module. AttributeError: 'tuple' object has no attribute 'tb_frame' An exception has occurred, use %tb to see the full traceback. --------------------------------------------------------------------------- During handling of the above exception, another exception occurred: SystemExit: 0
Anyone has an answer?
@JoséRodrigues Have a look, also AttributeError answered.
0

Instead of a named option @click.option('--filename') you can use the argument decorator and specifying:

  1. the parameter-name of your function like here filename and
  2. the type of the parameter, e.g. a file-path that is validated for existence: click.Path(exists=True)

Click Argument for file-paths

You can define File Path Arguments as follows with click:

@click.command()
@click.argument('filename', type=click.Path(exists=True))
def main(filename):
    print(f"{filename=}")
    df = pd.read_csv(filename)

However this means changing your command-line, e.g. by removing the option '--filename' element from the array:

if __name__ == '__main__':
    sys.argv = ['',  'result.csv']
    main()

Attribute Error

The runtime error mentioned in a comment to guolei's answer:

AttributeError: 'tuple' object has no attribute 'tb_frame'

is most likely not related to your presented Python script (incl. click, pandas or scikit-learn). It rather comes from your runtime-environment like IPython or Jupyter Notebook. See AttributeError: 'TypeError' object has no attribute 'tb_frame' bug · Issue #12877 · ipython/ipython.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.