2

I want to run a python script, say train_rnn_wdcnn_loads.py in jupyter notebook from Anaconda in Win 10. But I constantly get this error:

ipykernel_launcher.py: error: the following arguments are required: -data_path

(I'm new in Python and still figuring out how argparse works)

The code are:

import argparse
parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
parser.add_argument('-data_path', required=True)
args = parser.parse_args()

I have tried to make it works with:

parser.add_argument('-data_path', required=True, default="C:\\Users\\Firdaus\\Desktop\\data")

or from this answer:

args = parser.parse_args(data_path="C:\\Users\\Firdaus\\Desktop\\data")

and it still didn't work.

When I run the script via command line, python train_rnn_wdcnn_loads.py -data_path C:\\Users\\Firdaus\\Desktop\\data it works but still facing error in another python script.

Using TensorFlow backend.
1614364082.0161884
loading data ...
Traceback (most recent call last):
  File "train_rnn_wdcnn_loads.py", line 107, in <module>
    source_data = CWRUBearingData(data_path, experiment, source,
  File "C:\Users\Firdaus\AppData\Local\Programs\Python\Python38\lib\site-packages\utils\cwru_data_loader.py", line 114, in __init__
    for l in self.loads:
TypeError: 'NoneType' object is not iterable

How to run the script without error?

dzdws
  • 73
  • 2
  • 10
  • 1
    Are you sure that the path is correct? Because launching the script with ```python file.py -data_path /home/your/path``` seems correct. Try to print the ```args``` to check what it contains. – Elidor00 Feb 26 '21 at 19:23
  • yes the path is correct. what I want is launch the script within the jupyter notebook. when I launch via command line, it works. – dzdws Feb 27 '21 at 04:12
  • Try to see this https://discourse.jupyter.org/t/how-to-work-with-pure-python-file-py/4443 – Elidor00 Feb 27 '21 at 10:06
  • 1
    many thanks, I will try – dzdws Feb 27 '21 at 10:15

1 Answers1

0

If you put (required=True) in your code, it will ignore (default="C:\Users\Firdaus\Desktop\data")

This should work:

parser.add_argument('-data_path', required=False, default="C:\\Users\\Firdaus\\Desktop\\data")
CB Acnt
  • 36
  • 4