By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

How do i cast a string to be used as a module?

P: n/a
In article <cyH8c.3460$GH3.601@fed1read07>,
se**@sands.beach.net (Sean Berry) writes:
I don't know that this partilarily bad programming,
but I was interested in doing the following.

def functionname( module, var1, var2 ):
import module

I would like to be able to pass the module name
as an argument and have the module imported as a result.

For instance... functionname( os, "1", 234)

Thanks for any help.


Thanks for the help, and exec("import "+modname)
does indeed import the module.

But, what if I want to call a function from within this
module that takes as an argument the name of a module.

For example if I was making a module like the following

import inspect

def functionname( modname ):
exec("import "+modname)
s = inspect.getsource( modname )
return s

How do I cast modname as a module name and not as a string?

Thanks.
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"sean" <se********@cox.net> schrieb im Newsbeitrag
news:9kM8c.46388$Bg.39277@fed1read03...
In article <cyH8c.3460$GH3.601@fed1read07>,
se**@sands.beach.net (Sean Berry) writes:
I don't know that this partilarily bad programming,
but I was interested in doing the following.

<snipped for brevity>

Does this help?:

http://groups.google.com/groups?q=vi...home.nl&rnum=1

Regards

Vincent Wehren
Jul 18 '05 #2

P: n/a
sean a écrit :
In article <cyH8c.3460$GH3.601@fed1read07>,
se**@sands.beach.net (Sean Berry) writes:
I don't know that this partilarily bad programming,
but I was interested in doing the following.

def functionname( module, var1, var2 ):
import module

I would like to be able to pass the module name
as an argument and have the module imported as a result.

For instance... functionname( os, "1", 234)

Thanks for any help.

Thanks for the help, and exec("import "+modname)
does indeed import the module.

But, what if I want to call a function from within this
module that takes as an argument the name of a module.

For example if I was making a module like the following

import inspect

def functionname( modname ):
exec("import "+modname)
s = inspect.getsource( modname )
return s

How do I cast modname as a module name and not as a string?

Thanks.


exec("import chunk")
globals()["chunk"] <module 'chunk' from 'chunk.pyc'>

or even simplier :
__import__("chunk")

<module 'chunk' from 'chunk.pyc'>
Yermat

Jul 18 '05 #3

P: n/a
"sean" <se********@cox.net> wrote in message news:<9kM8c.46388$Bg.39277@fed1read03>...

But, what if I want to call a function from within this
module that takes as an argument the name of a module.

For example if I was making a module like the following

import inspect

def functionname( modname ):
exec("import "+modname)
s = inspect.getsource( modname )
return s


Use s = inspect.getsource(sys.modules[modname]), of course, import sys
first.

Another thing that people often ask is how to access module level
variables by name string. The device there is the globals()
dictionary. So, remember that there are a few interesting namespace
dictionaries that come in handy:

(a) __builtin__ (it's a module, but you can access its namespace via
__builtin__.__dict__)
(b) globals() and locals()
(c) sys.modules
(d) os.environ
etc.

One has to be careful with the locals() dictionary (use it only as
read-only.)

regards,

Hung Jung
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.