Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Imports, Paths, Directories & Modules
    primarykey
    data
    text
    <p>Let me start by saying I've done extensive research over the course of the past week and have not yet found actual answers to these questions - just some fuzzy answers that don't really explain what is going on. If that's just cause I missed what I was looking for, I'm sorry - please just point me in the correct direction.</p> <p>My directory structure is:</p> <pre><code>TestProject/ runtest* testpackage/ __init__.py testmod.py testmod2.py testsubs/ testsubmod.py </code></pre> <p>A couple notes:</p> <ul> <li>I'm using python2.7 on Ubuntu</li> <li>I'm testing with bpython</li> <li>I'm running bpython from specific directories to test the way import behaves</li> <li>I'm attempting to follow best practices.</li> <li>This package is not installed, it's in a random dev directory</li> <li>This directory is not in the pythonpath</li> <li>I have a single <strong>init</strong>.py in the package directory</li> <li>There are no <strong>init</strong>.py files in the nested directories</li> <li>The <strong>init</strong>.py file is empty</li> <li>testpackage/testmod.py contains TestModClass</li> <li>testpackage/testsubs/testsubmod.py contains TestSubModClass</li> </ul> <p>Things I've observed:</p> <ul> <li>When I run bpython from TestProject/ <strong>import testpackage</strong> works <ul> <li>This does not import testpackage.testmod</li> <li>I cannot access testpackage.testmod at all</li> </ul></li> <li>When I run bpython from TestProject/ <strong>import testpackage.testmod</strong> fails</li> <li>When I run bpython from TestProject/ <strong>from testpackage import testmod</strong> works</li> <li>I can add code to <strong>init</strong>.py to explicitly import testmod.py, but not testsubs/testmod.py <ul> <li>I don't think this is the correct way to do this, what if the user doesn't want to import that module?</li> </ul></li> <li>from testmod.py i can import testmod2, but not testpackage.testmod2 <ul> <li>This would be nice to do so that I can name my own modules with overlapping names with STL or twisted (such as testpackage.logging) without causing errors (it sucks to have to name my own modules crap like customerlogging, instead of just mypackage.logging)</li> </ul></li> </ul> <p>And the questions:</p> <ol> <li>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?</li> <li>Why doesn't import testpackage give me access to testpackage.testmod? When I import os, I can then access os.path (etc).</li> <li>With a package, should I stick to using a single <strong>init</strong>.py in the base directory, or should I be nesting them in subsequent directories?</li> <li>How can I import a module specifying the package name? I.E. from testmod.py, I would like to import testpackage.testmod2 rather than just testmod2.</li> <li>What is the proper way to import submodules from the subsubs directory? <ol> <li>The only solution I see is to append that directory to the pythonpath from <strong>init</strong>.py, but I don't know if that's the correct way.</li> </ol></li> </ol> <p>Thanks in advance.</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.
 

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