472,131 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,131 software developers and data experts.

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])
m = imp.load_module(mname, fp, pathname, description)
if fp:
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

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,
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,

mediocre nebula.
Dec 11 '07 #1
0 2218

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 leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.