dynamically importing a module and function

rkmr.em at gmail.com rkmr.em at gmail.com
Mon Apr 21 17:46:20 EDT 2008


Hi
I have a function data['function'], that I need to import from a file
data['module'], in the directory data['cwd']

If I do this from python interactive shell (linux fedora core 8) from
dir /home/mark it works fine:

            cwd = data['cwd']
            os.chdir(cwd)
            print os.getcwd()
            module = __import__(data['module'])
            function = getattr(module, data['function'])

But if I put this in a file /home/mark/work/common/funcq.py and run it
from /home/mark,  it throws me error like this.. how to fix this? it
imports the module successfully, but it is not looking for subsequent
modules in the os.getcwd()..

/home/mark/work/proj1
Traceback (most recent call last):
  File "/home/mark/work/common/funcq.py", line 60, in <module>
    if __name__ == '__main__':do()
  File "/home/mark/work/common/funcq.py", line 33, in do
    module = __import__(data['module'])
  File "/home/mark/app.py", line 5, in <module>
    import abcde
ImportError: No module named abcde



More information about the Python-list mailing list