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>Probably even if you import the module <code>data</code> from the module <code>autoencoder</code> that is in the same folder, it doesn't work because the path where modules are searched is always the path containing the first file that gets executed, so the path of <code>run.py</code> in your case. Both <code>autoencoder</code> and <code>data</code> are in the <code>ae</code> subdirectory.</p>

<p>You imported <code>autoencoder</code> from <code>run.py</code> doing: <code>import ae.autoencoder</code></p>

<p>You can import <code>data</code> in the same way: import <code>ae.data</code> becase it still refers to the path of <code>run.py</code> so you have to enter the <code>ae</code> directory.</p>

<p>If you want to avoid mentioning the folder all the time you can add the <code>ae</code> directory to the system path.</p>

<p>Open <code>run.py</code>, then you need the module <code>os</code> that you already imported, also import <code>sys</code> module,</p>

<p>then:</p>

<pre><code>sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "./ae")
</code></pre>

<hr>

<p>So you <strong>run.py</strong> file should look like this:</p>

<pre><code>import os
import shutil
import sys
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "./ae")
import autoencoder
def main():
    pass

if __name__ == "__main__":
    main()
</code></pre>

<p>And your <strong>autoencoder.py</strong> should look like this:</p>

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

<p>If you use an IDE and you try the <code>sys.path.append</code> method it may show as the modules <code>autoencoder</code> and <code>data</code> aren't found by the IDE, but actually they are found at runtime after their path is included, so it should run.</p>

<hr>

<p>Another way of loading a module located in the same directory is:</p>

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

<hr>

<p>When you need to import many modules or import modules at runtime it is recommended to use the <strong>imp</strong> (now deprecated) or <strong>importlib</strong> module.</p>

<p>For Python 3.5+:</p>

<pre><code>import importlib.util
spec = importlib.util.spec_from_file_location("module_name", "/path/to/file")
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
foo.MyClass()
</code></pre>

<p>For Python 3.3 and 3.4:</p>

<pre><code>from importlib.machinery import SourceFileLoader

foo = SourceFileLoader("module_name", "/path/to/file").load_module()
foo.MyClass()
</code></pre>

<p>For Python 2:</p>

<pre><code>import imp

foo = imp.load_source('module_name', '/path/to/file')
foo.MyClass()
</code></pre>

python