How can I use import if I want to import a module as a string?


shadow

I have a list of module names in an array. I want to import all these modules from an already imported class. It seems that I cannot use from TestModule __import__(name)

How can I do this? Here is what I have:

import MainModule
arr = \
    ['Module1', 'Module2', 'Module3', 'Module4', 'Module5']
for string in arr:
     # Use FROM  to import the sub classes somehow
     from MainModule __import__(string)

Of course, Python doesn't allow me to do this.

g
import MainModule
arr = \
    ['Module1', 'Module2', 'Module3', 'Module4', 'Module5']
for string in arr:
    globals()[string] = getattr(MainModule, string)

Although the modification globals()is not very elegant , it might help you .

better

modules = {string: getattr(MainModule, string) for string in arr}

Then do whatever you moduleswant , e.g.

class MyModulesHolder(dict):
    pass

modules = MyModulesHolder()
modules.__dict__ = modules
modules.update({string: getattr(MainModule, string) for string in arr})

So you can do it modules.Module2.whatever().

Related


How can I use import if I want to import a module as a string?

shadow I have a list of module names in an array. I want to import all these modules from an already imported class. It seems that I cannot use from TestModule __import__(name) How can I do this? Here is what I have: import MainModule arr = \ ['Module1', '

How can I use import if I want to import a module as a string?

shadow I have a list of module names in an array. I want to import all these modules from an already imported class. It seems that I cannot use from TestModule __import__(name) How can I do this? Here is what I have: import MainModule arr = \ ['Module1', '

How can I import a module given the full path?

derfred: How can I load a Python module given the full path? Note that this file can be anywhere in the filesystem as it is a configuration option. Sebastian Rittau: For Python 3.5+ use: import importlib.util spec = importlib.util.spec_from_file_location("modu

How can I import my App module into the MyAppUITests file?

巴特米米 (BartłomiejSemańczyk) Here is my simple test case: import XCTest @testable import MyApp //it doesn't work because of this: class TabBarControllerTests: XCTestCase { override func setUp() { super.setUp() let defaults = NSUserDefaults

How can i import a class from another module maven

you did it I can't import classes from ejb module to web module. ejb class is in package:jsf in module ejb and I want to import it in web module package:jsf import jsf.DaneOsobowe; - Dose work What's wrong with me pom master: <?xml version="1.0" encoding="

How can I use import if I want to import a module as a string?

shadow I have a list of module names in an array. I want to import all these modules from an already imported class. It seems that I cannot use from TestModule __import__(name) How can I do this? Here is what I have: import MainModule arr = \ ['Module1', '

How can i import a class from another module maven

you did it I can't import classes from ejb module to web module. ejb class is in package:jsf in module ejb and I want to import it in web module package:jsf import jsf.DaneOsobowe; - Dose work What's wrong with me pom master: <?xml version="1.0" encoding="

How can I import and create my class from my module?

mark I am creating a module which has the following classes: class LogLevel(): Info = 1 Warn = 2 Error = 3 class FalconPeer(): def __init__(self, port=37896, log_level=LogLevel.Info): self._port = port self._log_leve

How can I use import if I want to import a module as a string?

shadow I have a list of module names in an array. I want to import all these modules from an already imported class. It seems that I cannot use from TestModule __import__(name) How can I do this? Here is what I have: import MainModule arr = \ ['Module1', '

How can i import lib from node module in laravel

Axel Amgar hello i want to load axios in script tag using laravel something like <script src="{{url('node_modules/axios/dist/axios.js')}}"></script> but it doesn't work, I get this error /node_modules/axios/dist/axios.js net::ERR_ABORTED 404 Do you have a so

How can I import my App module into the MyAppUITests file?

巴特米米 (BartłomiejSemańczyk) Here is my simple test case: import XCTest @testable import MyApp //it doesn't work Because of this: class TabBarControllerTests: XCTestCase { override func setUp() { super.setUp() let defaults = NSUserDefaults