importing module from variable

python import module from path
import module from string python
python modules
python import from variable
module = __import__(module_name)
python import module with period
python import package

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 "" and do something like:

import config_file1 as params

# and then I can access the different parameters as

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


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


*** ****
num_iterations = 100
initial_value1 = 10
initial_value2 = 20 

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

However another config file ( 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.

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


Import Python Module by String Name, Learn more about how Python searches for imports, how the `PYTHONPATH` environment variable is used, and how to use `sys.path` in my  By default, Import-Module imports all members that the module exports, but you can use the Alias, Function, Cmdlet, and Variable parameters to restrict which members are imported. The NoClobber parameter prevents Import-Module from importing members that have the same names as members in the current session.

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.

Some Notes on Importing Modules by Name, module = __import__("module"). Note that you have to assign the module object to a variable yourself. This allows you to rename the module on the way in: For a module to be imported, it is necessary for it to be present on the system or on a remote server. Starting from PowerShell 3.0, Modules are automatically imported the first time any cmdlet in the installed module is run. $PSModuleAutoloadingPreference preference variable is used to enable or disable the automatic importing of modules.

You can do like this:

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

6. Modules, Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have  When a module is imported by using the Import-Module cmdlet, by default, all exported module members are imported into the session, including any commands exported to the module by a nested module. By default, variables and aliases are not exported. To restrict the members that are exported, use a module manifest.

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")

Creating and Importing Modules in Python, py extension. The name of the file becomes the module name. Inside the file, we can have definitions and implementations of classes, variables, or functions. You can't import any of the effects except via the package if you did manage to get this work somehow (they'd except an effectList in the importing module). You have to manually do the append in every effect you write.

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

params = __import__('module_name')


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


ES modules: A cartoon deep-dive - Mozilla Hacks, Once you have the ability to export and import variables between modules, it makes it a lot easier to break up your code into small chunks that  import shared_stuff def f(): print shared_stuff.a Don't use a from import unless the variable is intended to be a constant. from shared_stuff import a would create a new a variable initialized to whatever shared_stuff.a referred to at the time of the import, and this new a variable would not be affected by assignments to shared_stuff.a.

import, The static import statement is used to import read only live bindings which are exported by another module. Imported modules are in strict mode  Import a single export from a module Given an object or value named myExport which has been exported from the module my-module either implicitly (because the entire module is exported) or explicitly (using the export statement), this inserts myExport into the current scope. Import multiple exports from module

Dynamic Import in Python 3, Do you want to know how you can import modules, and class in or class by passing a string, and assign the imported object to a variable. Importing Modules To make use of the functions in a module, you’ll need to import the module with an import statement. An import statement is made up of the import keyword along with the name of the module. In a Python file, this will be declared at the top of the code, under any shebang lines or general comments.

Python 3 - Modules, A module can define functions, classes and variables. #!/usr/bin/python3 # Import module support import support # Now you can call defined function that  Import: Named exports from module: import {x [as y], } from "module" Default export: import x from "module" import {default as x} from "module" Everything: import * as obj from "module" Import the module (its code runs), but do not assign it to a variable: import "module" We can put import/export statements at the top or at the bottom of a

  • This looks like an incredibly risky design, provided even if this was doable, what if you load up a config where params.var1 doesn't exist? Your entire script would fail.
  • I handle the cases where some variable might not exist. I just want to be able to define variables outside my program and to be able to select which set of variables I want to load.
  • Won't work for me. Maybe you could suggest a better approach. Basically I have some experiment configurations that I want to run. Each experiment must have some parameters set to certain values. So I define those variables in a file and import it. But I want to be able to select different configs
  • Thank you for the code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by describing why this is a good solution to the problem, and would make it more useful to future readers with other similar questions. Please edit your answer to add some explanation, including the assumptions you've made.