dynamically importing a module and function

rkmr.em at gmail.com rkmr.em at gmail.com
Mon Apr 21 17:53:15 EDT 2008


there was a mistake in my prev mail.. it is not able to successfully
import the module.

abcde is in directory /home/mark/work/proj1, but it is looking for it
in /home/mark from where i am running the script.. though i changed
cwd using os.chdir() function



>   File "/home/mark/app.py", line 5, in <module>
>     import abcde
>  ImportError: No module named abcde


On Mon, Apr 21, 2008 at 2:46 PM, rkmr.em at gmail.com <rkmr.em at gmail.com> wrote:
> 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