469,641 Members | 1,129 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

dynamically importing a module and function

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
Jun 27 '08 #1
4 1920
rk*****@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']
OT: Any good reason for using a dictionary instead of a class instance
(data.functiom, data.module, etc)?
>
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'])
This is possibly due to python using what was the current working
directory when it started up.

An alternative (untested):

saved = sys.path
sys.path = data['cwd']
module = __import__(data['module'])
sys.path = saved

Exception handling is left as an exercise for the reader.

HTH,
John
Jun 27 '08 #2
On Mon, Apr 21, 2008 at 3:39 PM, John Machin <sj******@lexicon.netwrote:
rk*****@gmail.com wrote:
data['module'], in the directory data['cwd']
OT: Any good reason for using a dictionary instead of a class instance
(data.functiom, data.module, etc)?
not really, i just wanted to stick to primitive python data types.

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'])
saved = sys.path
sys.path = data['cwd']
module = __import__(data['module'])
sys.path = saved
now the module gets loaded, but i am not able to get the function from
the module, though it works fine in the interactive-shell

Traceback (most recent call last):
File "/home/mark/work/common/funcq.py", line 62, in <module>
if __name__ == '__main__':do()
File "/home/mark/work/common/funcq.py", line 35, in do
function = getattr(module, data['function'])
AttributeError: 'module' object has no attribute 'new'
this works in shell though..
>>import os
os.chdir('/home/mark/work/proj1')
import sys
sys.path.append('/home/mark/work/proj1')
module = __import__('app')
function = getattr(module, 'new')
function(1)
1
Jun 27 '08 #3
rk*****@gmail.com wrote:
On Mon, Apr 21, 2008 at 3:39 PM, John Machin <sj******@lexicon.netwrote:
>rk*****@gmail.com wrote:
>>data['module'], in the directory data['cwd']
OT: Any good reason for using a dictionary instead of a class instance
(data.functiom, data.module, etc)?
not really, i just wanted to stick to primitive python data types.

>>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'])

> saved = sys.path
sys.path = data['cwd']
module = __import__(data['module'])
sys.path = saved

now the module gets loaded, but i am not able to get the function from
the module, though it works fine in the interactive-shell

Traceback (most recent call last):
File "/home/mark/work/common/funcq.py", line 62, in <module>
if __name__ == '__main__':do()
File "/home/mark/work/common/funcq.py", line 35, in do
function = getattr(module, data['function'])
AttributeError: 'module' object has no attribute 'new'
this works in shell though..
>>>import os
os.chdir('/home/mark/work/proj1')
import sys
sys.path.append('/home/mark/work/proj1')
module = __import__('app')
function = getattr(module, 'new')
function(1)
1
It's not at all obvious that the "works in shell" code is the same as
the code in your script.

Consider the possibility that as a result of frantic experimentation you
have multiple copies of app.* with varying contents lying around.

Try this in your script so that you can see exactly what it is doing,
instead of comparing it to a strawman:
print "attempting to import", whatever
module = __import__(whatever)
print "got", module.__name__, "from", os.path.abspath(module.__file__)
print "module contents":, dir(module)
Jun 27 '08 #4
On Mon, Apr 21, 2008 at 4:47 PM, John Machin <sj******@lexicon.netwrote:
saved = sys.path
sys.path = data['cwd']
module = __import__(data['module'])
sys.path = saved
>
import os
os.chdir('/home/mark/work/proj1')
import sys
sys.path.append('/home/mark/work/proj1')
module = __import__('app')
function = getattr(module, 'new')
function(1)
>

>
1

It's not at all obvious that the "works in shell" code is the same as the
code in your script.

Consider the possibility that as a result of frantic experimentation you
have multiple copies of app.* with varying contents lying around.
thanks a lot!
there was a file app.pyc lying around in the dir from which i was
running the script...
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by J. Kenney | last post: by
reply views Thread by Mark English | last post: by
12 posts views Thread by qwweeeit | last post: by
4 posts views Thread by jean-marc | last post: by
5 posts views Thread by Matt_D | last post: by
reply views Thread by rkmr.em | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.