pythonargv

Pass parameters to main(argv) from within another python script


I have a python script that passes arguments "-i on" to argv in main define this is part of the code;

def main(argv):

   status = "none"

   try:
      opts, args = getopt.getopt(argv,"hi:")
   except getopt.GetoptError:
      print sys.argv[0], ' -i on|off'
      sys.exit(2)
   for opt, arg in opts:
      if opt == '-h':
         print sys.argv[0]," -i on|off"
         print "Switches invertor on and off"
         sys.exit()
      elif opt in ("-i"):
         status = arg
   
   up = UPower()
   if (up.connect() < 0):
    print "Could not connect to the device"
    exit -2
 
   
   newstatR = 0
   if (status == "on"): newstatR = 1

I have another script that i would like to call this script like this;

import sys
from flask import Flask, request
sys.path.insert(0, '/home/domoticame/epever-inverter-read')
import ivctl
...
ivctl.main('-i on')

This however doesnt reliably work, the execution is different than on command line It seems to execute the off command in stead of on command

This however doesnt reliably work, the execution is different than on command line It seems to execute the off command in stead of on command


Solution

  • Have you tried to pass them as a list?

    import sys
    from flask import Flask, request
    sys.path.insert(0, '/home/domoticame/epever-inverter-read')
    import ivctl
    ...
    ivctl.main('-i on'.split())