user2261062
user2261062

Reputation:

importing module from variable

I have different config files which are basically python files that define variables and I want to import them in my main program.

Usually I will have a "config_file1.py" and do something like:

import config_file1 as params

# and then I can access the different parameters as
params.var1
params.var2

Now I want to be able to select which config_file I want to use, so I want to pass a parameter when calling my program that tells which config file to use. Something like:

config_filename = sys.argv[1]
import config_filename as params

However this looks for a file named "config_filename".

I want instead to import the file referenced by the value of config_filename

EDIT:

Basically my program will run a set of experiments, and those experiments need a set of parameters to run.

Eg:

*** config1.py ****
num_iterations = 100
initial_value1 = 10
initial_value2 = 20 

So I can run my program loading those variables into memory.

However another config file (config2.py) might have another set of parameters, so I want to be able to select which experiment I want to run by loading the desired config file.

Upvotes: 0

Views: 77

Answers (5)

grapes
grapes

Reputation: 8636

Instead of using import statement, you can use __import__ function, like this:

params = __import__('module_name')

Definition:

importlib.__import__(name, globals=None, locals=None, fromlist=(), level=0)

Reference: https://docs.python.org/3/library/importlib.html#importlib.import

Upvotes: 0

Peter
Peter

Reputation: 3495

This is in response to your details and not the question.

If you want to load variables such as num_iterations = 100, initial_value1 = 10, initial_value2 = 20 from a file, then I'd really recommend some sort of config instead of abusing imports for global variables.

Json would be the easiest way, where you'd load the file and you'd straight up get a dict:

>>> import json
>>> params = json.loads(config_file1)
{'num_iterations': 100, 'initial_value1': 10, 'initial_value2': 20}

Alternatively you could use ConfigParser, which looks nicer, but I've found it to be quite prone to breaking.

Upvotes: 1

Akarius
Akarius

Reputation: 398

If you really want to do this, you can use the importlib module:

import importlib
params = importlib.import_module(sys.argv[1])

Then you can use the var like this

params.var1

Upvotes: 2

r.ook
r.ook

Reputation: 13868

I wouldn't recommend such a risky approach. You relinquish all controls over at the point of the sys.argv and your script can fail if any one of the named attribute doesn't exist within your module.

Instead I would suggest explicitly controlling what are the supported modules being passed in:

config_filename = sys.argv[1].lower()

if config_filename == 'os':
    import os as params
elif config_filename == 'shutil':
    import shutil as params
else:   # unhandled modules
    raise ImportError("Unknown module")

Upvotes: 0

Pedro Borges
Pedro Borges

Reputation: 1270

You can do like this:

config_filename = sys.argv[1]
params = __import__(config_filename)

Upvotes: 0

Related Questions