Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As <a href="http://effbot.org/zone/import-confusion.htm" rel="noreferrer">Fredrik Lundh states:</a></p> <blockquote> <p>Anyway, here’s how these statements and functions work:</p> <p><code>import X</code> imports the module X, and creates a reference to that module in the current namespace. Or in other words, after you’ve run this statement, you can use <code>X.name</code> to refer to things defined in module X.</p> <p><code>from X import *</code> imports the module X, and creates references in the current namespace to all public objects defined by that module (that is, everything that doesn’t have a name starting with “_”). Or in other words, after you’ve run this statement, you can simply use a plain name to refer to things defined in module X. But X itself is not defined, so <code>X.name</code> doesn’t work. And if name was already defined, it is replaced by the new version. And if name in X is changed to point to some other object, your module won’t notice.</p> <p><code>from X import a, b, c</code> imports the module X, and creates references in the current namespace to the given objects. Or in other words, you can now use <code>a</code> and <code>b</code> and <code>c</code> in your program.</p> <p><strong>Finally, <code>X = __import__(‘X’)</code> works like <code>import X</code>, with the difference that you 1) pass the module name as a string, and 2) explicitly assign it to a variable in your current namespace.</strong></p> </blockquote> <p>And by the way that's the last one method that you're intrested in.</p> <p>Simply write (for example):</p> <pre><code>var = "datetime" module = __import__(var) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload