how to import a module dynamically

R

Randall Smith

How do I import a module when given the module name as input?

Between __import__, imp, and rexec, I'm very lost. What I want to do
seems so simple, but I don't know the best way to do it. How would I
import a module into the current namespace using the equivalent of these
2 methods:

import modname

and

from modname import *

where modname is validated input

Randall
 
I

Irmen de Jong

Randall said:
How do I import a module when given the module name as input?

import modname

exec "import "+your_module_name

from modname import *

exec "from %s import %s" % (your_module_name, your_imported_symbol)

HTH,
Irmen.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,485
Latest member
Andrewayne909

Latest Threads

Top