Python Code Changes not Reflected on Script Run

Viewed 30
$ python --version
Python 3.6.8

I've written a script which has some command-line arguments. Initially, these worked without issue:

arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
    '-log',
    '--loglevel',
    default = 'info'
)
arg_parser.add_argument(
    '-lf',
    '--logfile',
    default = './logs/populate.log'
)
...
cl_options = arg_parser.parse_args()
...

I then changed the name of the "-log" short flag, and added another flag:

arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
    '-ll',
    '--loglevel',
    default = 'info'
)
arg_parser.add_argument(
    '-lf',
    '--logfile',
    default = './logs/populate.log'
)
arg_parser.add_argument(
    '-d',
    '--daemon',
    action = 'store_true'
)
...
cl_options = arg_parser.parse_args()
...

When running the script now, the initial set of arguments are still used - the name of the "-log" flag is the same and it is missing the "-d/--daemon" flag when run:

$ python3 populate.py --daemon
usage: populate.py [-h] [-log LOGLEVEL] [-lf LOGFILE]
populate.py: error: unrecognized arguments: --daemon

Things I have tried:

  • make sure I have checked out the proper git branch
  • delete the pycache folder
  • reboot the machine the script runs on
  • use the reload() option for argparse

If I look at the contents of the script I can see that the changes I've made are there, but they refuse to take effect.

I'm not a Python expert, I'm still learning, but I must be doing something wrong here. Can anyone point me in the right direction?

Thanks!

EDIT:

I have verified as well as I can that the script is using the most current files:

Remote System (where script is running):

$ pwd
/opt/ise-web-rpt

$ ls populate.py 
populate.py

$ git branch
* develop
  main

$ sha256sum populate.py 
2601cbb49f6956611e2ff50a1b1b90ba61c9c0686ed199831d671e682492be4b  populate.py

Local System (where development happens):

$ git branch
* develop
  main

$ sha256sum populate.py 
2601cbb49f6956611e2ff50a1b1b90ba61c9c0686ed199831d671e682492be4b  populate.py

As far as I can tell the script is the correct file and I'm on the correct branch in Git.

1 Answers

Stepping through this in pdb, it appears this was caused by importing another Python file in the populate.py script.

Both files had argparse configured the exact same way, so initially, there was no problem. When I added the new parameter to populate.py, the second file that was imported didn't have this parameter added, so it was "unrecognized" to the imported Python file. That's also why the flag names didn't appear to change - it was returning the names from the imported file, not the one I was trying to run. I added the new parameter to the args list in the second file and the script(s) were able to run.

I now need to figure out how hierarchy works for argparse, but that's a separate issue. Thanks everyone for the input.

Related