[Tutor] How to import modules using the input() command

Kent Johnson kent37 at tds.net
Thu Nov 15 16:53:19 CET 2007


Mihai Iacob wrote:
> 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
> 
> Traceback (most recent call last):
>   File "<pyshell#168>", line 1, in <module>
>     input()
>   File "<string>", line 1
>     import time
>          ^
> SyntaxError: invalid syntax

import is a statement, not an expression, that is why it fails with 
input(). You could input the name of the module and then import it 
yourself with __import__(), or you could type '__import__("time")' to 
input() though that won't bind the module to a name.

Why do you want to do this? It is an unusual request.

Kent


More information about the Tutor mailing list