ImportError with module in same directory
<p>I am trying to test the mechanism that one python program calling python functions defined in other files. For instance, the main program is <code>run.py</code>, </p>

<pre><code>import os
import shutil
import ae.autoencoder
if __name__ == '__main__':
     main()
</code></pre>

<p>which calls the <code>autoencoder.py</code> located under the subdirectory of <code>ae</code>. </p>

<p>autoencoer.py is </p>

<pre><code>import data.py
# import data
</code></pre>

<p>However, either <code>import data.py</code> or <code>import data</code> will always give the following error message</p>

<pre><code>python run.py
Traceback (most recent call last):
 File "run.py", line 3, in &lt;module&gt;
  import ae.autoencoder
 File "/home/autoencoder/ae/autoencoder.py", line 1, in &lt;module&gt;
  import data.py
 ImportError: No module named 'data'
</code></pre>

<p>The file structure is as follows:(/home/autoencoder is the working directory, where run.py locates)</p>

<p><a href="http://i.stack.imgur.com/XlBIw.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/XlBIw.jpg" alt="enter image description here"></a></p>

<p>First things first, no need to add the <code>.py</code> extension when importing :) That's implied. </p>

<p>The syntax for relative imports (importing modules in the same directory) got a little stricter between Python versions 2 and 3. The way you have it set up will work with Python 2.x, but I'm guessing you're running Python 3.x (you can check by running <code>python --version</code>). With Python 3, you need to explicitly use a dot to do relative imports now.</p>

<p>Python 2 relative import:</p>

<pre><code>import data
</code></pre>

<p>Better Python 2.5+ relative import:</p>

<pre><code>from . import data
</code></pre>

<p>You should use the second version in any code you write, unless you're specifically limited to using ancient python versions.</p>

python