dynamic module import?

L

Lawson Hanson

Is it possible to import a module of Python code
where I do not know the name of the module
until run-time?

The Python statement:

from someModule import *

requires that "someModule" be the name of the module,
but what I would like is to be able to define a value
for "someModule" ... and provided that such a module
exists (in an extended "sys.path" directory), then
import from the specified module at run-time

If I have a module called "dummy.py" in my own "myModules"
directory (just below my HOME directory in Linux)

If I do this:

import os, sys

myModDir = os.environ["HOME"] + "/myModules"

sys.path.append(myModDir)

modName = "%s/%s" % (myModDir, "dummy")

from modName import *

I get the following error:

ImportError: No module named modName

So is there any way to get Python to import the named module
without just doing "from dummy import *", because I will
not know that the user wants to use the "dummy" module
until run-time ... I'm trying to import control data
for different run scenarios which will be defined in
differently named Python modules which the user will
specify at run-time with a command-line option

And help with this would be most appreciated

Best regards,

Lawson Hanson
 
S

Steven D'Aprano

modu = "os"
exec("from " + modu + " import *")

"from module import *" is generally frowned upon, although it does
occasionally have its uses.

By the way, I think your computer's clock is set wrong. My newsclient is
reporting that you sent your post at 4:30pm today -- and I'm writing this
at 11am. Perhaps your timezone is set wrong?

Regards,
 
A

alex23

modu = "os"
exec("from " + modu + " import *")

And of course, there's the usual disclaimer that this should be only
used in circumstances where you can guarantee the contents of 'modu'
aren't ever going to be anything like:

modu = "os import system; system('rm *'); from os "

Another reason to prefer __import__ over exec is speed:

$ /usr/lib/python2.5/timeit.py -n 100000 'from os import system'
100000 loops, best of 3: 2.12 usec per loop

$ /usr/lib/python2.5/timeit.py -n 100000 '__import__("os", fromlist=
["system"])'
100000 loops, best of 3: 2.72 usec per loop

$ /usr/lib/python2.5/timeit.py -n 100000 'm = "os"; exec("from " + m +
" import system")'
100000 loops, best of 3: 25.1 usec per loop
 
E

Eduardo Lenz

Is it possible to import a module of Python code
where I do not know the name of the module
until run-time?

The Python statement:

from someModule import *

requires that "someModule" be the name of the module,
but what I would like is to be able to define a value
for "someModule" ... and provided that such a module
exists (in an extended "sys.path" directory), then
import from the specified module at run-time

If I have a module called "dummy.py" in my own "myModules"
directory (just below my HOME directory in Linux)

If I do this:

import os, sys

myModDir = os.environ["HOME"] + "/myModules"

sys.path.append(myModDir)

modName = "%s/%s" % (myModDir, "dummy")

from modName import *

I get the following error:

ImportError: No module named modName

So is there any way to get Python to import the named module
without just doing "from dummy import *", because I will
not know that the user wants to use the "dummy" module
until run-time ... I'm trying to import control data
for different run scenarios which will be defined in
differently named Python modules which the user will
specify at run-time with a command-line option

And help with this would be most appreciated

Best regards,

Lawson Hanson

modu = "os"
exec("from " + modu + " import *")

[]'s
Lenz.

--

Eduardo Lenz Cardoso
Dr. Eng.
Associate Professor

State University of Santa Catarina
Department of Mechanical Engineering
89223-100 - Joinville-SC - Brasil

Tel: +55 47 4009-7971 - Fax: +55 47 4009-7940
E-mail: (e-mail address removed)
---------------------------------------------
 
S

Steven D'Aprano

And of course, there's the usual disclaimer that this should be only
used in circumstances where you can guarantee the contents of 'modu'
aren't ever going to be anything like:

modu = "os import system; system('rm *'); from os "

Another reason to prefer __import__ over exec is speed:

Both very good points, but consider that you're not comparing apples with
apples.
from os import system
system
del system

exec "from %s import system" % "os"
system
del system

__import__("os", fromlist=["system"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'system' is not defined

I mention this only to be pedantic, because I agree with your point that
exec can introduce security issues, and be significantly slower.
 
E

Eduardo Lenz

"from module import *" is generally frowned upon, although it does
occasionally have its uses.

By the way, I think your computer's clock is set wrong. My newsclient is
reporting that you sent your post at 4:30pm today -- and I'm writing this
at 11am. Perhaps your timezone is set wrong?

Regards,


OK,

my point was about the exec statement, not the import * (it was just an
example). Well, my clock is OK here..maybe the time zone is wrong. I will
check this out. Thanks for the advice.

[]'s
Lenz.

--

Eduardo Lenz Cardoso
Dr. Eng.
Associate Professor

State University of Santa Catarina
Department of Mechanical Engineering
89223-100 - Joinville-SC - Brasil

Tel: +55 47 4009-7971 - Fax: +55 47 4009-7940
E-mail: (e-mail address removed)
---------------------------------------------
 
A

alex23

Both very good points, but consider that you're not comparing apples with
apples.
__import__("os", fromlist=["system"]) <module 'os' from '/usr/lib/python2.5/os.pyc'
system
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'system' is not defined

I must confess I've rarely had a need to use __import__ and don't
think I've ever used the fromlist arg. I'm confused, though, because
the docstring states:

The fromlist should be a list of names to emulate ``from name
import ...''

But it also states that __import__ always returns a module, so I'm
utterly confused as to the purpose of fromlist, or how to inject the
specified entries into the calling namespace. If anyone could explain
this for me, I'd really appreciate it.
I mention this only to be pedantic, because I agree with your point that
exec can introduce security issues, and be significantly slower.

Correcting misinformation isn't pedantry, especially when I've learned
something :)
 
A

alex23

[a nice concise explanation on __import__ & fromlist]

Cheers, Duncan, that explained it perfectly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top