running a script from another script without knowing it's name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • noama
    New Member
    • Aug 2007
    • 9

    running a script from another script without knowing it's name

    Hi,
    I want to make a script that will call the run() func of some other scripts who's name the main script will read from a file.

    i.e, the main script will:
    1. read the file
    2. for each <name> in the file:
    3. try to call <name>.run()

    i.e, if i have a text file:

    one
    two

    the main script will read the text file and then try to call one.run() and two.run()

    I can't use the os module since i need the return value of that function.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by noama
    Hi,
    I want to make a script that will call the run() func of some other scripts who's name the main script will read from a file.

    i.e, the main script will:
    1. read the file
    2. for each <name> in the file:
    3. try to call <name>.run()

    i.e, if i have a text file:

    one
    two

    the main script will read the text file and then try to call one.run() and two.run()

    I can't use the os module since i need the return value of that function.
    Use the __import__ function:
    [code=python]
    >>> name = "one"
    >>> mod = __import__(name )
    <module 'one' from blah blah blah>
    >>> mod.run() # call function
    [/code]

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by noama
      Hi,
      I want to make a script that will call the run() func of some other scripts who's name the main script will read from a file.

      i.e, the main script will:
      1. read the file
      2. for each <name> in the file:
      3. try to call <name>.run()

      i.e, if i have a text file:

      one
      two

      the main script will read the text file and then try to call one.run() and two.run()

      I can't use the os module since i need the return value of that function.
      In the text files, remember to leave off the '.py' extension.
      For every .txt file in a given directory, use:[CODE=python]import os.path
      import sys
      import glob
      for fullPath in glob.glob(r"aPa th\*.txt"):
      thisDir, fName = os.path.split(f ullPath)
      if thisDir not in sys.path:
      sys.path.append (thisDir)
      f = open(fName)
      lineList = f.readlines()
      f.close()
      for line in lineList:
      try:
      mod = __import__(line .strip())
      mod.run()
      except ImportError, AttributeError:
      pass # either no module or no run() function[/CODE]
      If you are hard-coding the names, use:[CODE=python]fNames = ["one.txt", "two.txt"]
      for fName in fNames:
      # current working directory; leave out os and sys stuff
      # everything else is the same here[/CODE]

      Comment

      • noama
        New Member
        • Aug 2007
        • 9

        #4
        and what do i do if the module i want to import isn't in the same directory as the main script, but it's not in a package (the directory doesn't contain a __init__.py file) either?

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by noama
          and what do i do if the module i want to import isn't in the same directory as the main script, but it's not in a package (the directory doesn't contain a __init__.py file) either?
          OK. I think that I get the scenario:

          For a script that reads a text file from its own (current working) directory, the text file might have:
          Code:
          # module list
          C:\rest\of\fully\quallified\path\Module1.py
          C:\rest\of\fully\quallified\path\Module2.py
          and this would import the modules listed in it:
          [CODE=python]import os.path
          import sys

          modListFile = open("thelistfi le.txt")
          moduleList = modListFile.rea dlines()
          modListFile .close()

          for line in moduleList:
          fullPath = os.path.normpat h(line.strip())
          thisDir, fName = os.path.split(f ullPath)
          modName, ext = os.path.splitex t(fName) # this is new #
          if thisDir not in sys.path:
          sys.path.append (thisDir)
          try:
          mod = __import__(modN ame)
          mod.run()
          except ImportError, AttributeError:
          pass # either no module or no run() function[/CODE]
          Which you might have been able to figure out from the original (it's not very different).

          Comment

          Working...