title: Python imports - ModuleNotFoundError: No module named X
body: <p>I have read probably all of the posts on here regarding imports and I still cannot figure out what is going on with the imports, I have spent hours trying to get a very simple example working and am literally pulling my hair out.</p>
  
  <p>I am using python 3.7 and pycharm but I am running my code from the commandline, for the unit tests I am using pytest.</p>
  
  <p>My project structure is:</p>
  
  <pre><code>my_message_validator/
      __init__.py
  
      module_1/
          __init.py__
          foo.py
  
      module_2/
          __init.py__
          bar.py
          baz.py
  
      module_3
          context.py
          test_all.py
  </code></pre>
  
  <p>module_1.<strong>init</strong>.py</p>
  
  <pre><code>from module_1 import foo
  </code></pre>
  
  <p>module_2.<strong>init</strong>.py</p>
  
  <pre><code># For some reason pycharm doesnt complain when I use '.' but if I use module_2 it does
  from . import bar, baz
  </code></pre>
  
  <p>If I try to run my code or my tests from the commandline no matter how I move things around I seem to get either <code>ModuleNotFoundError: No module named</code>, when I have managed to get the tests working I still cannot run my code on its own from the commandline.</p>
  
  <p>How can I import <code>module_1</code> into <code>module_2</code> and are these actually packages? I am coming from java and find the imports a lot easier to understand, I am finding the python importing very confusing...</p>
  
  <p>Also how can I can then import whatever I need into my test module\package\folders <code>context.py</code>?</p>
  
  <p>Currently the test context look like:</p>
  
  <pre><code>import os
  import sys
  
  # Is this needed as it doesnt seem to do anything?
  sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
  
  from module_1.foo import Foo
  
  from module_2 import bar, baz
  </code></pre>
  
  <p>In <code>test_all.py</code> I am trying to import from the context file like this:</p>
  
  <pre><code>from .context import bar,baz
  from .context import Foo
  
  # Calling in test like
  Foo.load_file(file)
  
  bar.method_one()
  baz.method_two()
  </code></pre>
  
  <p>Do I need all the <code>__init.py__</code> files and what should I be putting in them to make my methods and classes public\exposed? I would like this entire package to be reusable so want to be able to treat it like a jar file in java.</p>
  
  <p>Any help would be much appreciated as it seems everytime I change something I get an error in a different place, python seems so much more complicated than java right now.</p>