Karthikeyan Singaravelan <tir.kar...@gmail.com> added the comment:

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.

----------
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue36561>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to