3

I'm totally new with Python,can anyone please let me know how I can do the following two imports in a python script followed by the other line WHILE i IS BEING CHANGED IN EACH LOOP? (The following three lines are in a "for" loop whose counter is "i")

import Test_include_i
from Test_include_i import*
model = Test_include_i.aDefinedFunction

Thank you very much :)

TerryA
  • 58,805
  • 11
  • 114
  • 143
  • 10
    This is.... Such a terrible idea. – Jakob Bowyer Jul 24 '13 at 11:41
  • 1
    why would you want that ? :O – rnbguy Jul 24 '13 at 11:42
  • Check answer of this question. [Dynamic module import in python](http://stackoverflow.com/questions/301134/dynamic-module-import-in-python) – tailor_raj Jul 24 '13 at 11:47
  • To defend the OP a bit, there are use-cases for this, which you can see by the naming of the variables. He/She might want to elaborate, but having a bunch of similar models implemented in one `.py` file each is not such a bad idea. – filmor Jul 24 '13 at 11:49
  • @rnbcoder I am actually reading some information from a file named Test_include,which is being changed in each loop.And I want each loop to import the latest Test_include,so I am renaming it each time.If it is possible to somehow REFRESH the "import Test_include" command,then I can delete "_i" at the end of its name! – user2614391 Jul 24 '13 at 12:17
  • @filmor Thank you,I am actually reading some parameters of the model of a tunnel from the Test_include file,which is being changed in each loop,and I need the loop to load the latest Test_include file actually! :) – user2614391 Jul 24 '13 at 12:20
  • Use `imp.reload()` to force a reload of a module. – Ludo Jul 24 '13 at 12:22
  • @user2614391 no i meant why `import Test_include_i` and then `from Test_include_i import*` ? – rnbguy Jul 24 '13 at 12:57
  • I have absolutely no logical answer for this,as long as this file is just a small part of a really big problem in which I can not go deeper for numerous dependencies! – user2614391 Jul 24 '13 at 13:07

3 Answers3

6

This is not a good idea, but this is the implementation of it:

from importlib import import_module # Awesome line! :)

for i in range(1000):
    test_include = import_module("Test_include_%s" % i)
    model = test_include.aDefinedFunction

Regarding the differences between the provided methods:

  • __import__ is the low-level interface that handles from bla import blubb and import bla statements. It's direct use is according to the docs discouraged nowadays.
  • importlib.import_module is a convenience wrapper to __import__ which is preferred. The imported module will be recorded in sys.modules and thus be cached. If you changed the code during the session and want to use the new version you have to reload it explicitly using imp.reload.
  • imp.load_module is even closer to the internals and will always load the newest version of the module for you, i.e. if it is already loaded load_module is equivalent to a imp.reload call on the module. However to use this function you have to provide all 4 arguments, which are basically what imp.find_module returns.
filmor
  • 30,840
  • 6
  • 50
  • 48
1

You need to use the __import__ function, and perhaps importlib, although you should consider if that's what you really want to do. Perhaps explain what you're trying to achieve, and there will probably be a better way.

RoadieRich
  • 6,330
  • 3
  • 35
  • 52
  • I think the importlib library should work,however,unfortunately the Python with which I am working is 2.6.6 and does not have this library! I am reading some information from Test_include file in each step which is being changed and renamed (by adding i at the end of its name),so in each step I have to import the latest edited file,not the previous one. I can also add that it is not important for me if the previous Test_include files are kept or not! :) – user2614391 Jul 24 '13 at 12:01
  • If you're using Python 2.6.6, then the `imp` module would be your best option (see my answer below). I would not use `__import__`, as this is a low-level implementation which is not intended to be used in "production" code. There is a reason for the `imp` module :) – Ludo Jul 24 '13 at 12:09
  • That's backwards -- `imp` is the low-level implementation interface that provide access to the internals, and usually requires multiple calls to do things right (find followed by load). Using the documented `__import__` API is perfectly fine. – Fredrik Jul 24 '13 at 12:42
  • @user2614391 if that's all you want, then take a look at [`reload`](http://docs.python.org/2.6/library/functions.html#reload) – RoadieRich Jul 24 '13 at 12:54
0

You can use imp.load_module(), which accepts a string as module name. See http://docs.python.org/2/library/imp.html#imp.load_module

Ludo
  • 813
  • 1
  • 9
  • 21
  • Thank you very much,it was a really great help! :) It worked with "imp.reload(Test_include)" and I do not have to change the name of the Test_include file in each loop any more! – user2614391 Jul 24 '13 at 12:51