title: Using pytest with a src layer
body: <p><a href="https://docs.pytest.org/en/3.7.2/goodpractices.html#tests-outside-application-code" rel="noreferrer">pytest recommends</a> including an additional directory to separate the source code within a project:</p>
  <pre><code>my_package
  ├── src  # &lt;-- no __init__.py on this layer
  │   └── my_package
  │       ├── __init__.py
  │       └── util_module
  │           ├── __init__.py
  │           └── utils.py
  └── tests
      ├── __init__.py
      └── test_util_module
          ├── __init__.py
          └── test_utils.py
  </code></pre>
  <p>Sadly, they say nothing<sup>[1]</sup> about how imports in the test code should work in such a case, which work for my IDE just fine in <a href="https://gitlab.com/arecknag/my_package" rel="noreferrer">this naive example</a><sup>[2]</sup>, but causes the following error with pytest:</p>
  <pre><code>my_package $ pytest
  
  ====================== test session starts ======================
  platform linux -- Python 3.6.4, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
  rootdir: /home/user/workspace/my_package, inifile:
  collected 0 items / 1 errors     
                                                                                                                                                                        
  ============================ ERRORS =============================
  ___ ERROR collecting tests/test_util_module/test_utils.py ___
  ImportError while importing test module '/home/user/workspace/my_package/tests/test_util_module/test_utils.py'.
  Hint: make sure your test modules/packages have valid Python names.
  Traceback:
  tests/test_util_module/test_utils.py:1: in &lt;module&gt;
      from test.test_module.some_file import starify
  E   ModuleNotFoundError: No module named 'my_package.util_module'
  !!!! Interrupted: 1 errors during collection !!!!!
  </code></pre>
  <p>I can fix the issue by changing the import of the test to <code>from src.my_package.util_module.utils import starify</code>, but then my IDE complaints about the <code>src</code> part being redundant, so I'd like to keep it out.</p>
  <hr />
  <p><sup>[1]<sub>: Not the case any more. As of version 3.7.3, pytest recommends the editable install also featured in @hoefling's answer at the top of its <a href="https://docs.pytest.org/en/stable/goodpractices.html?highlight=good%20practices" rel="noreferrer">good practices</a>.</sub></sup></p>
  <p><sup>[2]<sub>: Setup is <code>virtualenv env -p python3.6; source env/bin/activate; pip install pytest</code></sub></sup></p>