Message339654
test.py shown in msg339649 accepts only --data_type DATA_TYPE. Running test.py without import and the non-existent flag would show all the options present like the below I am using from the repo you have shared. You might
./python.exe ../backups/bpo36561.py --non-existent 100
usage: bpo36561.py [-h] [--mode MODE] [--data_type DATA_TYPE] [--model-path MODEL_PATH]
[--data-path DATA_PATH] [--data-shuffle DATA_SHUFFLE] [--batch-size BATCH_SIZE]
[--num_epochs NUM_EPOCHS] [--val-step VAL_STEP] [--test-epoch TEST_EPOCH]
[--start-epoch START_EPOCH] [--neg-cnt NEG_CNT] [--lr LR] [--beta1 BETA1]
[--beta2 BETA2] [--dropout DROPOUT] [--n_critic N_CRITIC] [--emb-dim EMB_DIM]
[--hidden HIDDEN] [--nb NB] [--train_path TRAIN_PATH] [--val_path VAL_PATH]
[--test_path TEST_PATH]
bpo36561.py: error: unrecognized arguments: --non-existent 100
Looking further the problem might be that importing data_loader imports preprocess module and it has the below code. So this argparse code is executed and not the one from tests.py. Python executes all the top level code while importing. Maybe you can have this inside if __name__ == "__main__" so that this is not used while importing,
parser = argparse.ArgumentParser()
# data
parser.add_argument('--data_type', type=str, default="ml_100k")
args = parser.parse_args()
download_dataset(args.data_type)
preprocess(args.data_type)
Can help so that this is not executed.
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# data
parser.add_argument('--data_type', type=str, default="ml_100k")
args = parser.parse_args()
download_dataset(args.data_type)
preprocess(args.data_type)
This is reproducible like this where you expect foo to be printed but due to import bar's argument parser is used :
➜ gc-mc-pytorch git:(master) ✗ cat /tmp/foo.py
import bar
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', required=True)
parser.parse_args()
➜ gc-mc-pytorch git:(master) ✗ cat /tmp/bar.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--bar', required=True)
parser.parse_args()
➜ gc-mc-pytorch git:(master) ✗ python3 /tmp/foo.py
usage: foo.py [-h] --bar BAR
foo.py: error: the following arguments are required: --bar
Either way it's not a bug in Python and it's a side effect of top level code being executed during importing the module. I am closing this as not a bug. |
|
Date |
User |
Action |
Args |
2019-04-08 15:39:26 | xtreak | set | recipients:
+ xtreak, JP Zhang |
2019-04-08 15:39:26 | xtreak | set | messageid: <1554737966.95.0.918332559427.issue36561@roundup.psfhosted.org> |
2019-04-08 15:39:26 | xtreak | link | issue36561 messages |
2019-04-08 15:39:26 | xtreak | create | |
|