Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import file using string as name [duplicate]

Tags:

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

like image 714
XtrmJosh Avatar asked Dec 28 '12 14:12

XtrmJosh


People also ask

What is __ import __ in Python?

__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.

What is dynamic import python?

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.

How do js imports work?

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.


1 Answers

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() 
like image 198
lbonn Avatar answered Sep 28 '22 14:09

lbonn