Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ah! Tricky, but I got it!</p> <p>"foo" is not a simple "other package" - it is seem by Python as a sub-module of your "package" module.</p> <p>When you first run "package" - either importing it from an external script, or by running it with the <code>-m</code> command line switch (but not if you run <code>python package/__init__.py</code> directly from the command line), the "package" module is parsed, and added to the <code>sys.modules</code> dicticionary (on the <code>sys</code> module). </p> <p>When the sub-module <code>foo</code> is read, besides being placed directly under <code>sys.modules</code> under the key <code>["package.foo"]</code>, it is also set as an attribute to its parent module. Therefore it would be avaliable in your Python app as <code>package.foo</code>. What happens is that setting an attribute in <code>sys.modules["package"]</code>, has the same effect than setting a key in <code>package/__init__.py</code> globals in runtime. That is what is happening.</p> <p>I hope I could translate the process into words properly - if not, just ask again by commenting. </p> <p>-- Since this is probably happening in real code you have, and the equivalent of "do_import" is being called from code outside your package (and have the side effects of making your sub-modules appear on the package's global namespace), there is no easy work around on the way you are doing it. My suggestion is to just add an underscore (<code>_</code>) at the beggining of the sub-modules names if they are not intended to be called from general code from outside your package. (It also won't show up if someone does <code>from package import *</code> in this case)</p>
 

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