Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First off, you will find all the information you need in <a href="http://docs.python.org/tutorial/modules.html" rel="noreferrer">section 6 of The Python Tutorial</a>.</p> <hr> <blockquote> <p>(1) Does python deal differently with imports on packages &amp; modules that exist in the pythonpath than when you are trying to import from your current directory?</p> </blockquote> <p>No, it doesn't. Actually, Python always searches <code>sys.path</code> when importing modules. Modules in the current directory are only found since <code>sys.path</code> contains an entry with the empty string, meaning the current directory.</p> <hr> <blockquote> <p>(2) Why doesn't <code>import testpackage</code> give me access to <code>testpackage.testmod</code>? When I import <code>os</code>, I can then access <code>os.path</code> (etc).</p> </blockquote> <p>For efficiency, <code>import testpackage</code> only loads <code>testpackage/__init__.py</code>. If you need <code>testpackage.testmod</code>, you have to <em>explicitely</em> import it:</p> <pre><code>import testpackage # Just imports testpackage, not testpackage.testmod! import testpackage.testmod # Import *both* testpackage and testpackage.testmod! </code></pre> <p>If you always want to export <code>testmod</code>, import it within <code>__init__.py</code>, this is what <code>os</code> (<code>os/__init__.py</code>) does. This way, <code>testpackage.testmod</code> is always available implicitely if you import <code>testpackage</code>.</p> <p>Since Python is cross-platform, there is actually no way to consistently and automatically load modules in a directory, because some filesystems are case-insensitive (Windows!). Python would not know whether to load <code>os/path.py</code> as <code>os.path</code> or <code>os.Path</code>, etc.</p> <hr> <blockquote> <p>(3) With a package, should I stick to using a single <code>__init__.py</code> in the base directory, or should I be nesting them in subsequent directories?</p> </blockquote> <p>You always need an <code>__init__.py</code> for each subpackage. There were discussions on dropping this requirement, but it was decided to keep it as it is.</p> <hr> <blockquote> <p>(4) How can I import a module specifying the package name? I.E. from <code>testmod.py</code>, I would like to import <code>testpackage.testmod2</code> rather than just <code>testmod2</code>.</p> </blockquote> <p>This should work. Just ensure that you run the code from the top-level directory. If the current directory is <code>testpackage</code>, <code>testmod</code> does not know that it's in a package.</p> <p>The preferred way is to use relative intra-package imports, though:</p> <pre><code>from . import testmod2 </code></pre> <p>This prevents name clashes if there is a global module named <code>testmod2</code> and enables you to use the names of well-known modules within your package without problems.</p> <hr> <blockquote> <p>(5) What is the proper way to import submodules from the subsubs directory? The only solution I see is to append that directory to the pythonpath from <code>__init__.py</code>, but I don't know if that's the correct way.</p> </blockquote> <p>No, don't do that! Never, ever put a directory to <code>sys.path</code> when one of it's parent directories already is in <code>sys.path</code>! This could cause your modules to be loaded twice, which is a bad thing!</p> <p>Usually you should be able to load modules from subpackages using absolute or relative imports:</p> <pre><code>import testpackage.testsubs.testsubmod from testpackage.testsubs import testsubmod from .testsubs import testsubmod </code></pre> <p>Just make sure to create a <code>__init__.py</code> within <code>testsubs/</code>!</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