dynamic import

B

bry

Hi,
I'm trying to do a dynamic import of a file that has no problems with it, that
is to say I can import it normally, my sys.path is set to the right folder etc.
but my dynamic import code is not working, this is the problem code:

try:
import f[0]
except:
print "not importable"

f[0] returns the name of the file I'm importing, so I suppose this is a typing
problem, should I change f[0] to be some other type?

I've also tried
t = "import " + f[0]
try:
eval(t)
except:
print "not importable"

but I always get "not importable"
 
N

Nick Smallbone

Hi,
I'm trying to do a dynamic import of a file that has no problems with it, that
is to say I can import it normally, my sys.path is set to the right folder etc.
but my dynamic import code is not working, this is the problem code:

try:
import f[0]
except:
print "not importable"

f[0] returns the name of the file I'm importing, so I suppose this is a typing
problem, should I change f[0] to be some other type?

I've also tried
t = "import " + f[0]
try:
eval(t)
except:
print "not importable"

but I always get "not importable"

That's because eval evaluates an expression and returns the result. To
execute a statement you need to use exec(t) instead.

Nick
 
D

Duncan Booth

(e-mail address removed) wrote in

Hi,
I'm trying to do a dynamic import of a file that has no problems with
it, that is to say I can import it normally, my sys.path is set to the
right folder etc. but my dynamic import code is not working, this is
the problem code:

try:
import f[0]
except:
print "not importable"

f[0] returns the name of the file I'm importing, so I suppose this is
a typing problem, should I change f[0] to be some other type?

No. The import statement takes the name of a module, it doesn't take a
string. For example, instead of:

import sys
print sys.version

you are trying to do the equivalent of:

import "sys"
print "sys".version

You wouldn't expect the second line to work, so why should you expect the
first line to work? If it did work, how would you expect to refer to the
module it imported?

In this case your solution is to use the __import__ builtin. My example
becomes:

module = __import__("sys")
print module.version
 
P

Peter Otten

I'm trying to do a dynamic import of a file that has no problems with it,
that is to say I can import it normally, my sys.path is set to the right
folder etc. but my dynamic import code is not working, this is the problem
code:

try:
import f[0]
except:
print "not importable"

f[0] returns the name of the file I'm importing, so I suppose this is a
typing problem, should I change f[0] to be some other type?

I've also tried
t = "import " + f[0]
try:
eval(t)
except:
print "not importable"

but I always get "not importable"

Let's see.
File "<stdin>", line 1
import f[0]
^
SyntaxError: invalid syntax

So python won't accept the [0] stuff here.
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named f

So python doesnt try to resolve f as a variable name but rather as a
literal.
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 1
import os
^
SyntaxError: invalid syntax
So the eval() function chokes if you feed it an import statement (actually
any statement).

Quoted from http://docs.python.org/lib/built-in-funcs.html#l2h-23

"""Hints: dynamic execution of statements is supported by the exec
statement."""

Putting it together:
f = ["os"]
exec "import " + f[0]
os
<module 'os' from '/usr/local/lib/python2.3/os.pyc'>

Heureka :)

Now, did you see how useful Python's tracebacks are and how much information
you lose by a bare try ... except?

try:
...
except ImportError:
...

would have been fine, by the way.

Peter
 
J

John Roth

Hi,
I'm trying to do a dynamic import of a file that has no problems with it, that
is to say I can import it normally, my sys.path is set to the right folder etc.
but my dynamic import code is not working, this is the problem code:

try:
import f[0]
except:
print "not importable"

the import statement unfortunately doesn't take a string at
run time. As Duncan says, you need to use the
__import__() builtin. Read up on it in the library
reference (it's the very first entry in the builtins section,)
because there are some considerations in using it that
aren't exactly obvious on first glance.

The other alternative is something like:

exec "import " + f[0]

This will work as well, and it might be a bit clearer, depending
on what you're trying to do.

John Roth
 
5

510046470588-0001

John Roth said:
the import statement unfortunately doesn't take a string at
run time. As Duncan says, you need to use the
__import__() builtin. Read up on it in the library
reference (it's the very first entry in the builtins section,)
because there are some considerations in using it that
aren't exactly obvious on first glance.

The other alternative is something like:

exec "import " + f[0]

This will work as well, and it might be a bit clearer, depending
on what you're trying to do.

if one calls the __import__ function with the same argument several times,
will the module be cached inbetween?

Klaus Schilling
 
J

John Roth

John Roth said:
the import statement unfortunately doesn't take a string at
run time. As Duncan says, you need to use the
__import__() builtin. Read up on it in the library
reference (it's the very first entry in the builtins section,)
because there are some considerations in using it that
aren't exactly obvious on first glance.

The other alternative is something like:

exec "import " + f[0]

This will work as well, and it might be a bit clearer, depending
on what you're trying to do.

if one calls the __import__ function with the same argument several times,
will the module be cached inbetween?

You should get the same copy of the module.

John Roth
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top