Possible Duplicate:
Dynamic module import in Python
I intend to make a suite of files at some point soon, and the best way to organize it is to have a list, that list will be at the very top of a file, and after it will come a ridiculous amount of code to handle what that list controls and how it operates. I'm looking to write said list only once, and said list is a list of folder and file names in this format:
[(folder/filename, bool, bool, int), (folder/filename, bool, bool, int)]
As you can see, folder/filename
are the same (sort of). File name is folder name with .py
on the end, but doing import XXX you don't need to do import XXX.py, so I don't see this causing an issue.
The problem I'm facing is importing using this method...
for (testName, auto, hardware, bit) in testList: print(testName) paths = "\\" + testName print paths addpath(paths) sys.modules[testName] = testName # One of a few options I've seen suggested on the net print("Path Added") test = testName + ".Helloworld()" eval(test)
So for each test I have, print the name, assemble a string which contains the path ("\\testName"
), for this example, print the test path, then add the path to the list (sys.path.append(path)
), then print to confirm it happened, then assemble a string which will be executed by eval
for the tests main module and eventually eval it.
As you can see, I'm currently having to have a list of imports at the top. I can't simply do import testName
(the contents of testName
are the name of the module I wish to import), as it will try to find a module called testName
, not a module called the contents of testName
.
I've seen a few examples of where this has been done, but can't find any which work in my circumstances. If someone could literally throw a chunk of code which does it that would be wonderful.
I'd also request that I'm not hung, drawn, nor quartered for use of eval, it is used in a very controlled environment (the list through which it cycles is within the .py file, so no "end user" should mess with it).
__import__() Parameters name - the name of the module you want to import. globals and locals - determines how to interpret name. fromlist - objects or submodules that should be imported by name. level - specifies whether to use absolute or relative imports.
Dynamically Loading Modules or Classes Using the imp module: Modules can be imported dynamically by the imp module in python. The example below is a demonstration on the using the imp module. It provides the find_module() method to find the module and the import_module() method to import it. Dynamic_import.py.
Javascript import statement is used to import bindings that are exported by another module. Using import, the code is easier to manage when it is small and bite-size chunks. This is the thinking behind keeping functions to only one task or having files contain only a few or one component at a time.
Not sure if I understood everything correctly, but you can import a module dynamically using __import__
:
mod = __import__(testName) mod.HelloWorld()
Edit: I wasn't aware that the use of __import__
was discouraged by the python docs for user code: __import__ documentation (as noted by Bakuriu)
This should also work and would be considered better style:
import importlib mod = importlib.import_module(testName) mod.HelloWorld()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With