pytest "No module named" error
<p>I'm new to python and trying to write a test for my first app.
simple app structure:</p>

<pre><code>- app
      - tests
        __init__.py
        - test_main.py
      - __init__.py
      - main.py
</code></pre>

<p>The <code>main.py</code> contains  <code>main_func</code></p>

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

<pre><code>from main import main_func

def test_check():
    assert main_func() is True
</code></pre>

<p>If i run the test manually, by the command <code>pytest</code> while in <code>app</code> directory - got this <strong>error:</strong></p>

<pre><code>C:\Users\*****\PycharmProjects\checker3&gt;pytest
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-3.0.3, py-1.4.31, pluggy-0.4.0
rootdir: C:\Users\*****\PycharmProjects\app, inifile:
collected 0 items / 1 errors 

=================================== ERRORS ====================================
_____________________ ERROR collecting tests/test_main.py _____________________
ImportError while importing test module 'C:\Users\*****\PycharmProjects\app\tests\test_main.py'.
Original error message:
'No module named 'main''
Make sure your test modules/packages have valid Python names.
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.14 seconds ===========================
</code></pre>

<p>the packages names are right. Also, i use the <code>PyCharm</code> IDE and it doesn't track any import package error.</p>

<p>Moreover, when i execute the pytest test configuration through the IDE - test is working.</p>

<p>I think it runs in pycharm because of folder configuration mark in pycharm project. If you set the app folder as "Source" in settings, pycharm can include in any path. </p>

<p>For details;
<a href="https://www.jetbrains.com/help/pycharm/2016.1/configuring-folders-within-a-content-root.html" rel="nofollow">https://www.jetbrains.com/help/pycharm/2016.1/configuring-folders-within-a-content-root.html</a></p>

<p>It doesn't run in shell because it cannot include main. If you want to include and run, you should insert app path in sys.</p>

<pre><code>import sys
sys.path.insert(0, &lt;app folder path&gt;)
</code></pre>

python pycharm py.test