4

I'm really new to python. I'm using python<2.7. I have to import a file whose name I don't know at start. In fact I have to pass the name of file through command prompt, now I have read the name and stored in variable, but I don't know how to pass it to import statement. I'm trying the code

str = sys.argv[lastIndex]
from "%s" import *,str

but it's giving the error

File "IngestDataToMongo.py", line 86
from "%s" import *,str
        ^
SyntaxError: invalid syntax

So how to do it. Also is it possible with python <2.7, because for some reasons I can't change the version of Python or install anything where this code is running.

Shirish Herwade
  • 11,461
  • 20
  • 72
  • 111
  • 1
    you should refer to dynamic module imports http://stackoverflow.com/questions/301134/dynamic-module-import-in-python – avasal Nov 30 '12 at 09:16

3 Answers3

5

You can use __import__() function instead to import modules. It accepts variables as its arguments.

But first of all, make sure you really need that - 90% of the time people do not really need that function.

Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • I think it is more like 99%. – sberry Nov 30 '12 at 09:18
  • @sberry: Maybe, but the metaclasses are something that people do not need 99% of the time, and I think `__import__()` is really useful to a bigger number of people, so I picked 90% instead of 99% ;) Anyway, no real data behind it. – Tadeck Nov 30 '12 at 09:20
  • 83 percent of statistics are made up on the spot. – johnsyweb Nov 30 '12 at 09:22
  • 1
    @Johnsyweb: Yeah, we got it :) It is nice you introduced some real data to the discussion ;) – Tadeck Nov 30 '12 at 09:23
5

You should use importlib module

import importlib

mdl = 'urllib'
your_module = importlib.import_module(mdl)
your_module.quote
>>> <function urllib.quote>

EDIT

Thanks to Tadeck - This does work for python 2.7 and 3.1+

Community
  • 1
  • 1
alexvassel
  • 10,600
  • 2
  • 29
  • 31
  • 2
    One important note: `importlib` is not available in Python <2.7, nor 3.0, and even in 2.7 it is "_a minor subset of what is available in the more full-featured package of the same name from Python 3.1_" ([source](http://docs.python.org/2.7/library/importlib.html)). – Tadeck Nov 30 '12 at 09:22
  • @Tadeck seeing your comment I have to edit my question, because I have to use python 2.6 – Shirish Herwade Nov 30 '12 at 09:27
2

If you do not want to import the importlib module, this does it:

import sys
s = sys.argv[lastIndex]
def is_save(s):
    #test if s is a valid argument
    return True or False

if is_save(s):
    exec('from %s import *'%s)

Note: the is_save function is needed to avoid abuse of the script since the usage of exec (or eval) is potentially dangerous.

j-i-l
  • 10,281
  • 3
  • 53
  • 70
  • 1
    hey man, you are great, This worked perfectly, and with only 1 line of code. Thousand thanks to you. I'm your fan now. – Shirish Herwade Nov 30 '12 at 09:37
  • 4
    I can't recommend this version in cases where the imported argument might be unsafe (i.e. come from a user you may not trust). In general `exec` and `eval` should be avoided, since it's difficult to make sure that the string passed to these functions is sane. The use of `importlib` or `__import__` is definitely recommend. – David Zwicker Nov 30 '12 at 10:32
  • @DavidZwicker I agree with you and modified the answer to avoid the passage of potentially insane strings. – j-i-l Dec 01 '12 at 10:06