Import module does not work through terminal, while it works through IDE
<p>I have a projects consisting of two packages like this:</p>

<pre><code>MyProjectDir
-Package1
--__init__.py
--file1_1.py
--file1_2.py
--file1_3.py
-Package2
--__init__.py
--file2_1.py
--file2_2.py
--file2_3.py
</code></pre>

<p>Now, in the packages the files have some imports between files:</p>

<p><strong>file2_3.py:</strong></p>

<pre><code>from Package2.file2_1 import *
run_some_code()
</code></pre>

<p>When I run file2_3.py directly from PyCharm, everything runs ok. But when I try to run the script in the terminal (I'm working on Windows 7):</p>

<pre><code>D:\SVN Repo\MyProjectDir\Package2&gt; python file2_3.py
</code></pre>

<p>or alternatively</p>

<pre><code>D:\SVN Repo\MyProjectDir&gt; python ./Package2/file2_3.py
</code></pre>

<p>It seems python can't see my packages and I get an error:</p>

<pre><code>Traceback (most recent call last):
  File "./Package2/file2_3.py", line 1, in &lt;module&gt;
    from Package2.file2_1 import *
ImportError: No module named 'Package2'
</code></pre>

<p>What is the reason?</p>

<p><strong>EDIT: If in the import line I use <code>from file2_1.py import *</code> without the package name, the IDE underlines the import as "Unresolved Reference Package2" (though it can run), and the terminal works...</strong></p>

<p>The issue is that the way in which you are running the program <em>is wrong</em>, PyCharm is aware on how to handle python submodules and thus executes the file correctly.</p>

<p>If you have a package <code>package1</code> with a module <code>package1.my_module</code> you should run this using the <code>-m</code> switch:</p>

<pre><code>python -m package1.my_module
</code></pre>

<p>Do <strong>not</strong> run it directly:</p>

<pre><code>python package1/my_module.py   &lt;-- NO! Incorrect
</code></pre>

<p>Also: you should run the file from <em>outside</em> the package.  So if your project is:</p>

<pre><code>MyProject
  |
  +- package1
  |   |
  |   +- file1.py
  |
  +- package2
      |
      +- file2.py
</code></pre>

<p>Your working directory should be <code>MyProject</code>.</p>

<p>My personal advice: <strong>never</strong> run submodules directly. Instead put the code in a <em>separate</em> script that is outside the package. So I'd have:</p>

<pre><code>MyProject
  |
  +- package1
  |   |
  |   +- file1.py
  |
  +- package2
  |   |
  |   +- file2.py
  |
  +- scripts
      |
      +- script1.py
</code></pre>

<p>Where <code>script1.py</code> imports the modules it needs:</p>

<pre><code>from package1 import file1
from package2 import file2

# code
</code></pre>

<p>Then you can run that script from your <code>MyProject</code> directory:</p>

<pre><code>python scripts/script1.py
</code></pre>

<p>When you want to deploy your code you'll write a <code>setup.py</code> scripts that adds <code>package1</code> and <code>package2</code> as packages while <code>script1.py</code> as a script and they will be installed in the correct directories so that you'll be able to import <code>package1</code> and <code>package2</code> from everywhere and run <code>script1.py</code> from everywhere.</p>

python windows import