<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Mihai Iacob wrote:
<blockquote cite="mid:356507.59532.qm@web32715.mail.mud.yahoo.com"
 type="cite">
  <pre wrap="">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:

  </pre>
  <blockquote type="cite">
    <blockquote type="cite">
      <blockquote type="cite">
        <pre wrap="">input()
        </pre>
      </blockquote>
    </blockquote>
  </blockquote>
  <pre wrap=""><!---->import time
  </pre>
</blockquote>
The input function takes a character string and attempts to interpret
it as a Python <i>expression</i>. import is a <i>statement</i>, not
an expression.<br>
<br>
<blockquote cite="mid:356507.59532.qm@web32715.mail.mud.yahoo.com"
 type="cite">
  <pre wrap="">Traceback (most recent call last):
  File "&lt;pyshell#168&gt;", line 1, in &lt;module&gt;
    input()
  File "&lt;string&gt;", line 1
    import time
         ^
SyntaxError: invalid syntax
  </pre>
  <pre wrap=""><!---->

Is there another way in which i can import modules
with the input() command?
  </pre>
</blockquote>
Yes:<br>
<br>
modname = raw_input()<br>
exec "import " + modname<br>
<br>
That can be a security risk, in that a use could enter "time; import
os; os.rmdir('some_valuable_directory')"<br>
You could prescan modname for semicolons to reduce the risk.<br>
<br>
Safer is:<br>
<br>
modname = raw_input() <br>
globals()[modname] = __import__(modname)<br>
<br>
</body>
</html>