2

Say I have some python modules:

mymodule1.py
mymodule2.py
mymodule3.py
mymodule4.py
...

Can I iteratively import them?

This attempt doesn't work.

for x in range(1,5):
    mymod = 'mymodule' + repr(x)
    import mymod

Also, is there any reason why doing this is ill-advised?

Lee
  • 29,398
  • 28
  • 117
  • 170

1 Answers1

2

You can use importlib

A simple usage can be:

import importlib
mymodule = importlib.import_module("path/to/my/module.py")

Then you just have to adapt this code to import what you want "iteratively" (which I don't exactly know what it means).

bgusach
  • 14,527
  • 14
  • 51
  • 68