469,282 Members | 2,034 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Help: Trouble with imp.load_module

I'm not entirely sure what's going on here, but I suspect it's related
to my general lack of knowledge of the python import internals.

Here's the setup:

module: tester.py:
-----------------
import imp
def loader(mname, mpath):
fp, pathname, description = imp.find_module(mname,[mpath])
try:
m = imp.load_module(mname, fp, pathname, description)
finally:
if fp:
fp.close()
return m

module = loader("testA","/path/to/testA/)
print module.test_func("/path/to/something")

module = loader("test.B","/path/to/test.B/")
print module.test_func("/path/to/something")
------------------
module: testA.py:
---------------
def test_func(v):
import os
return os.path.exists(v)
---------------

module: test.B.py:
---------------
def test_func(v):
import os
return os.path.exists(v)
---------------
Okay, so modules "testA.py" and "test.B.py" are functionally identical,
except for the name of the module files themselves, and this is the
important part. The tester.py module is a really simple rig to run
"imp.load_module" on those two files.

You should get no problem running the first test of module "testA.py"
but you should get a traceback when attempting to run the second module
"test.B.py":

Traceback (most recent call last):
File "tester.py", line 15, in ?
print module.test_func("/path/to/something")
File "./test.B.py", line 2, in test_func
import os
File "/usr/lib/python2.4/os.py", line 131, in ?
from os.path import curdir, pardir, sep, pathsep, defpath, extsep,
altsep
ImportError: No module named path
So this must have something to do with the "." in the name of module
"test.B.py" but what is the problem, exactly? And how do I solve it? I
will sometimes need to run load_module on filenames which happen to have
"." in the name somewhere other than the ".py" extension. Is the
find_module somehow thinking this is a package?

Any help would be appreciated,
-Dave

--
Presenting:
mediocre nebula.
Dec 11 '07 #1
0 2157

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Jim | last post: by
4 posts views Thread by Josh | last post: by
6 posts views Thread by Don Sealer | last post: by
15 posts views Thread by carr4895 | last post: by
4 posts views Thread by Tim Chase | last post: by
1 post views Thread by Paul Sijben | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.