Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Python does not automatically recurse and import subpackages. When you say:</p> <pre><code>import packagename </code></pre> <p>That is all it imports. If you say:</p> <pre><code>import packagename.modulename </code></pre> <p>Then it first imports <code>packagename</code>, then imports <code>packagename.modulename</code> and <strong>assignes a reference to it</strong> as an attribute of <code>packagename</code>. Therefore, when you say in code:</p> <pre><code>packagename.modulename.Someclass() </code></pre> <p>Python is just using <strong>100% normal</strong> attribute lookups. First lookup the <code>packagename</code> variable in the current namespace. Then lookup the <code>modulename</code> attribute of the <code>packagename</code> object. Then lookup the <code>Someclass</code> attribute of the <code>modulename</code> object.</p> <p>If you neglected to import <code>packagename.modulename</code>, then there is plainly <strong>no attribute</strong> on <code>packagename</code> called <code>modulename</code>, and henceforth the <code>AttributeError</code>.</p> <p>I suggest you pop onto the command line and import something, then use <code>dir()</code> to examine it. Then import a subpackage, and use <code>dir()</code> again. You will quickly see the difference.</p> <hr> <p>Lastly, the syntax:</p> <pre><code>from packagename.modulename import SomePackage </code></pre> <p>Is essentially the same as this:</p> <pre><code>import packagename.modulename SomePackage = packagename.modulename.SomePackage </code></pre> <p>(of course, it's implemented differently, but more or less the same result).</p> <p>Does that help?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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