0

Is there a python command that will essentially perform the following?

for i in range(0, length):
    IndShort[i].test_function()

Trying to open a number of .py files, or rather, run test_function from a number of .py files (all existing, named IndShort1, IndShort2 ...).

I don't know that there is any further information that I might provide

for i in range(0, length):
    IndShort[i].test_function()

Perhaps too general of a question to expect any specific results?

  • I think that your direct question is a request for dynamic `import` use. However, if your purpose is to run a test quite, then you should look into existing test support packages. Start with PyTest and UnitTest. – Prune Sep 09 '19 at 15:19
  • Possible duplicate of [Import arbitrary python source file. (Python 3.3+)](https://stackoverflow.com/questions/19009932/import-arbitrary-python-source-file-python-3-3) – Maurice Meyer Sep 09 '19 at 15:20

1 Answers1

0

You can use importlib to import modules dynamically.

for i in range(0, length):
    mod = importlib.import_module("IndShort{}".format(i))
    mod.test_function()
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895