2

I'm trying to convert a script I had from bash to python. The way the program worked is that it had a Master script in the top folder and then in the subfolder Scripts a series of smaller, more specialised scripts. These smaller scripts might have names such as foo_bar.x, bar_foo.x, foo_baz.x, foo_qux.x, bar_qux.x, and so on. The Master script would collect the variables (as defined in files or from command line arguments) and execute the appropriate script with this sort of syntax:

VAR1=foo
VAR2=bar
./Scripts/${VAR1}_${VAR2}.x

This would execute Scripts/foo_bar.x

Now I'm trying to do the same but in python. The scripts are now modules in the folder Modules named foo_bar.py, foo_qux.py and so on. For now, the only thing they have is:

def test():
 print "This is foo_bar."

(or whichever).

If use these commands, this happens:

>>> import Modules.foo_bar
>>> Modules.foo_bar.test()
This is foo_bar.
>>> import Modules.foo_qux
>>> Modules.foo_qux.test()
This is foo_qux.
>>> a = Modules.foo_bar
>>> a.test()
This is foo_bar.

Which is fine. However, I can't get the syntax right if I want to use variables in the import statement. The first variable is defined as the string output of a command, and the second is a dictionary entry. Trying to use them in an import command gives an error.

>>> var1 = a_file.read_value()
>>> print var1
foo
>>> print dict['var2']
bar
>>> import Modules.var1_dict['var2']
 File "<stdin>", line 1
 import Modules.var1_dict['var2']
 ^
SyntaxError: invalid syntax

I assume it's a question of having the correct quotes or parentheses to properly invoke the variables. I've tried a bunch of combinations involving single quotes, curly brackets, square brackets, whatever, but no dice. What am I missing?

asked Oct 18, 2016 at 5:44
2

3 Answers 3

4

You can use the importlib library. And use like this:

...
import importlib
...
 function_call = 'Modules.' + var1 + dict['var2']
 function_call = importlib.import_module(function_call) 
 function_call.test()
answered Oct 18, 2016 at 6:08
Sign up to request clarification or add additional context in comments.

3 Comments

This is close to working, but the importlib command should be
@PietroAronica, Yeah the method is import_module, but you got the idea ))).
function_call = importlib.import_module(function_call)
1

If you want to import a module whose name is in a variable, you need to use __import__.

For example, let's create a dictionary whose values are the names of modules:

>>> d = { 1:'re', 2:'numpy' }

Now, let's import one of them:

>>> x = __import__(d[1])
>>> x
<module 're' from '/usr/lib/python2.7/re.pyc'>

We can use the module as long as we refer to it by the name we have given it:

>>> s='jerry'; x.sub('j', 'J', s)
'Jerry

And, to import the other:

>>> y = __import__(d[2])
>>> y
<module 'numpy' from '/home/jll/.local/lib/python2.7/site-packages/numpy/__init__.pyc'>
>>> y.array([1,3,2])
array([1, 3, 2])
answered Oct 18, 2016 at 6:04

Comments

0

How about executing your import statement dynamically?

Dummy example :

d = {"a" : "path"}
exec("import os.%s"%d["a"])
print os.path.abspath(__file__)
answered Oct 18, 2016 at 6:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.