Python Forum

Full Version: a bunch of modules to import
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have a bunch of modules to import. their names are created by some code that uses a number in range(10,301) to make them. what is the most practical and pythonic way to do that? code might look like:
for number in range(10,301):
    x = number * number * number
    z = (x+9999) % 1000 + 137
    name = 'vmod' + str(z)[:3]
    from {name} import calc as {name}
    ...
    ...
...
trouble is, the above code does not work as hoped because variable name does not get substituted.

is there a way to do this on a host with no writable storage space?
Hsve you looked at the importlib module: https://docs.python.org/3/library/importlib.html?
Use
import importlib
module = importlib.import_module(name)