Mihai Iacob wrote:
The input function takes a character string and attempts to interpret it as a Python _expression_. import is a statement, not an _expression_.Hello, I was wondering if there is a way to import modules using the input() command. If i try to do it directly it gives me an error:input()import time Yes:Traceback (most recent call last): File "<pyshell#168>", line 1, in <module> input() File "<string>", line 1 import time ^ SyntaxError: invalid syntaxIs there another way in which i can import modules with the input() command? modname = raw_input() exec "import " + modname That can be a security risk, in that a use could enter "time; import os; os.rmdir('some_valuable_directory')" You could prescan modname for semicolons to reduce the risk. Safer is: modname = raw_input() globals()[modname] = __import__(modname) |
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor