Function that imports modules | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Function that imports modules

Hello, community! I wanted to write a program (Python) that includes a function with a module name as an argument. So if the user inputs a module name, the function would import it. I tried it, but the argument of that function is not assigned to the method name of the 'import <method name>' line. I hope it's kinda clear what I mean :) I wish you all a nice weekend!

30th Dec 2017, 1:01 AM
David Handl
David Handl - avatar
2 Answers
+ 4
i am not sure that is possible and why would you do that, function is not using for import modules
30th Dec 2017, 1:35 AM
Vukan
Vukan - avatar
+ 1
You can do it, but you need to use the __import__() function instead of use the import statement: http://python-reference.readthedocs.io/en/latest/docs/functions/__import__.html
26th Jan 2018, 11:02 PM
visph
visph - avatar