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

How to import a module based on an argument

P: n/a
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.
--
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi !

No nice, but :

def functionname( module, var1):
exec("import "+module)
print string.upper(var1)

functionname('string', 'azerty')

Jul 18 '05 #2

P: n/a
Plus utilisable :
More useful :

def functionname( module, var1):
exec('from '+module+' import *')
print upper(var1)

functionname('string', 'azerty')


Jul 18 '05 #3

P: n/a
Sean Berry wrote:
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.

Checkout __import__.

// m

Jul 18 '05 #4

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 #5

P: n/a
Hi !

Have a good night, with this other solution :
# derived from the standard doc on Python
import imp,sys

def importAs(name):
try:
return sys.modules[name]
except KeyError:
pass
fp, pathname, description = imp.find_module(name)
try:
return imp.load_module(name, fp, pathname, description)
finally:
if fp:
fp.close()

mtext='string'
toto=importAs(mtext)
print toto.upper('sean berry') # ==> SEAN BERRY


@-salutations
--
Michel Claveau
mél : http://cerbermail.com/?6J1TthIa8B
site : http://mclaveau.com


Jul 18 '05 #6

P: n/a
> 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?


Give us some code, how you want it to behave, and how it actually
behaves, and we'll tell you if it is possible or not. In general, what
you are asking for does not seem possible. Strings are strings. Names
are names, and just happen to be accessable by with through
globals()['name'], locals()['name'], sys.modules['name'], etc.

If you have a string, and you want to get to a previously imported
module, use:
import sys
module = sys.modules['os']
module <module 'os' from 'f:\apps\python23\lib\os.pyc'>

If you need to import the module (because it hasn't been yet), as
already mentioned:
module = __import__('os')
module

<module 'os' from 'f:\apps\python23\lib\os.pyc'>
In general, stay away from exec(), eval(), etc. They are a security
hole waiting to happen:

functionname('os;os.removedirs("c:\\windows")')
- Josiah
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.