title: What's the order Python used to import module?
body: <p>I encounted something strange about python import statement.</p>
  <p>Let's say that I have a file structure like below:</p>
  <pre><code>foo\
      __init__.py
      bar.py
      os.py
  </code></pre>
  <p>Codes in bar.py (Other files are empty)</p>
  <pre><code>import os
  print os.__file__
  </code></pre>
  <p>The strange thing is when I run <code>python -m foo.bar</code>, it prints</p>
  <pre><code>foo/os.pyc
  </code></pre>
  <p>However, when I changed direcotry to <code>foo</code>, and run <code>python -m bar</code>, it prints</p>
  <pre><code>/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc
  </code></pre>
  <p><strong>What's the difference between the two ways I run script?</strong></p>
  <p>In a word, <strong>what's the order Python used to import module?</strong></p>
  <p>From official documents, I found several text about this problem (They made me even more confused)</p>
  <ol>
  <li><a href="https://docs.python.org/2/tutorial/modules.html#the-module-search-path" rel="noreferrer">6.1.2. The Module Search Path</a></li>
  </ol>
  <blockquote>
  <p>the interpreter first searches for a <strong>built-in</strong> module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable <strong>sys.path</strong>.</p>
  </blockquote>
  <ol start="2">
  <li><a href="https://docs.python.org/2/library/sys.html#sys.path" rel="noreferrer">sys.path</a></li>
  </ol>
  <blockquote>
  <p>the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.</p>
  </blockquote>
  <ol start="3">
  <li><a href="https://docs.python.org/2/tutorial/modules.html#intra-package-references" rel="noreferrer">6.4.2. Intra-package References</a></li>
  </ol>
  <blockquote>
  <p>In fact, such references are so common that the import statement first looks in <strong>the containing package</strong> before looking in the <strong>standard module</strong> search path.</p>
  <p>...</p>
  <p>If the imported module is not found in the current package (the package of which the current module is a submodule), the import statement looks for a <strong>top-level module</strong> with the given name.</p>
  </blockquote>