title: Hiding implementation files in a package
body: <p>I have a module called <code>spellnum</code>. It can be used as a command-line utility (it has the <code>if __name__ == '__main__':</code> block) or it can be imported like a standard Python module.</p>
  
  <p>The module defines a class named <code>Speller</code> which looks like this:</p>
  
  <pre><code>class Speller(object):
      def __init__(self, lang="en"):
          module = __import__("spelling_" + lang)
          # use module's contents...
  </code></pre>
  
  <p>As you can see, the class constructor loads other modules at runtime. Those modules (<code>spelling_en.py</code>, <code>spelling_es.py</code>, etc.) are located in the same directory as the <code>spellnum.py</code> itself.</p>
  
  <p>Besides <code>spellnum.py</code>, there are other files with utility functions and classes. I'd like to hide those files since I don't want to expose them to the user and since it's a bad idea to pollute the Python's lib directory with random files. The only way to achieve this that I know of is to create a package.</p>
  
  <hr>
  
  <p>I've come up with this layout for the project (inspired by this great <a href="http://infinitemonkeycorps.net/docs/pph/" rel="nofollow">tutorial</a>):</p>
  
  <pre><code>spellnum/                # project root
      spellnum/            # package root
          __init__.py
          spellnum.py
          spelling_en.py
          spelling_es.py
          squash.py
          # ... some other private files
      test/
          test_spellnum.py
      example.py
  </code></pre>
  
  <p>The file <code>__init__.py</code> contains a single line:</p>
  
  <pre><code>from spellnum import Speller
  </code></pre>
  
  <p>Given this new layout, the code for dynamic module loading had to be changed:</p>
  
  <pre><code>class Speller(object):
      def __init__(self, lang="en"):
          spelling_mod = "spelling_" + lang
          package = __import__("spellnum", fromlist=[spelling_mod])
          module = getattr(package, spelling_mod)
          # use module as usual
  </code></pre>
  
  <p>So, with this project layout a can do the following:</p>
  
  <ol>
  <li><p>Successfully <code>import spellnum</code> inside <code>example.py</code> and use it like a simple module:</p>
  
  <pre><code># an excerpt from the example.py file
  import spellnum
  
  speller = spellnum.Speller(es)
  # ...
  </code></pre></li>
  <li><p><code>import spellnum</code> in the tests and run those tests from the project root like this:</p>
  
  <pre><code>$ PYTHONPATH="`pwd`:$PYTHONPATH" python test/test_spellnum.py
  </code></pre></li>
  </ol>
  
  <h3>The problem</h3>
  
  <p>I cannot execute <code>spellnum.py</code> directly with the new layout. When I try to, it shows the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    ...
    File "spellnum/spellnum.py", line 23, in __init__
      module = getattr(package, spelling_mod)
  AttributeError: 'module' object has no attribute 'spelling_en'
  </code></pre>
  
  <h3>The question</h3>
  
  <p>What's the best way to organize all of the files required by my module to work so that users are able to use the module both from command line and from their Python code?</p>
  
  <p>Thanks!</p>