title: How to solve ImportError with pytest
body: <p><a href="https://stackoverflow.com/questions/41816973/modulenotfounderror-what-does-it-mean-main-is-not-a-package">There</a>  <a href="https://stackoverflow.com/questions/20985157/py-test-no-module-named">were</a> <a href="https://stackoverflow.com/questions/45446418/modulenotfounderror-no-module-named-main-xxxx-main-is-not-a-packag/52756937">already</a> questions regarding this topic. Sometimes programmers put some <code>__init__.py</code> at some places, often it is said one should use absolute paths. However, I don't get it to work here:</p>
  <p>How do I import a class from a package so that tests in pytest run and the code can be used?</p>
  <p>At the moment I get pytest or the code passing respective running.</p>
  <p>My example project structure is</p>
  <pre><code>.
  ├── testingonly
  │   ├── cli.py
  │   ├── __init__.py
  │   └── testingonly.py
  └── tests
      ├── __init__.py
      └── test_testingonly.py
  </code></pre>
  <p><code>__init__.py</code> is in both cases an empty file.</p>
  <pre><code>$ cat testingonly/cli.py
  &quot;&quot;&quot;Console script for testingonly.&quot;&quot;&quot;
  from testingonly import Tester
  
  def main(args=None):
      &quot;&quot;&quot;Console script for testingonly.&quot;&quot;&quot;
      te = Tester()
      return 0
  
  main()
  </code></pre>
  <pre><code>$ cat testingonly/testingonly.py
  &quot;&quot;&quot;Main module.&quot;&quot;&quot;
  class Tester():
      def __init__(self):
          print(&quot;Hello&quot;)
  </code></pre>
  <p>This gives - as expected:</p>
  <pre><code>$ python3 testingonly/cli.py
  Hello
  </code></pre>
  <p>Trying to test this, however, fails:</p>
  <pre><code>$ pytest
  ========================================================= test session starts =========================================================
  platform linux -- Python 3.7.3, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
  rootdir: /home/stefan/Development/testingonly
  collected 0 items / 1 error                                                                                                           
  
  =============================================================== ERRORS ================================================================
  _____________________________________________ ERROR collecting tests/test_testingonly.py ______________________________________________
  ImportError while importing test module '/home/stefan/Development/testingonly/tests/test_testingonly.py'.
  Hint: make sure your test modules/packages have valid Python names.
  Traceback:
  /usr/lib/python3.7/importlib/__init__.py:127: in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
  tests/test_testingonly.py:10: in &lt;module&gt;
      from testingonly import cli
  testingonly/cli.py:2: in &lt;module&gt;
      from testingonly import Tester
  E   ImportError: cannot import name 'Tester' from 'testingonly' (/home/stefan/Development/testingonly/testingonly/__init__.py)
  </code></pre>
  <hr />
  <p>Renaming <code>testingonly/testingonly.py</code> to <code>testingonly/mytest.py</code> and changing the imports in test_testingonly.py (from testingonly import mytest) and cli.py (from mytest import Tester) gives</p>
  <pre><code>$ pytest
  ========================================================= test session starts =========================================================
  platform linux -- Python 3.7.3, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
  rootdir: /home/stefan/Development/testingonly
  collected 0 items / 1 error                                                                                                           
  
  =============================================================== ERRORS ================================================================
  _____________________________________________ ERROR collecting tests/test_testingonly.py ______________________________________________
  ImportError while importing test module '/home/stefan/Development/testingonly/tests/test_testingonly.py'.
  Hint: make sure your test modules/packages have valid Python names.
  Traceback:
  /usr/lib/python3.7/importlib/__init__.py:127: in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
  tests/test_testingonly.py:10: in &lt;module&gt;
      from testingonly import cli
  testingonly/cli.py:2: in &lt;module&gt;
      from mytest import Tester
  E   ModuleNotFoundError: No module named 'mytest'
  ======================================================= short test summary info =======================================================
  ERROR tests/test_testingonly.py
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  ========================================================== 1 error in 0.37s ===========================================================
  $ python3 testingonly/cli.py
  Hello
  </code></pre>
  <hr />
  <p>The other proposed solution with renaming to mytest.py lets the tests pass, but in cli.py using <code>from testingonly.mytest import Tester</code> gives a <em>NameNotFound</em> error.</p>
  <pre><code>$ python3 testingonly/cli.py 
  Traceback (most recent call last):
    File &quot;testingonly/cli.py&quot;, line 2, in &lt;module&gt;
      from testingonly.mytest import Tester
  ModuleNotFoundError: No module named 'testingonly'
  </code></pre>
  <pre><code>$ pytest
  ========================================================= test session starts =========================================================
  platform linux -- Python 3.7.3, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
  rootdir: /home/stefan/Development/testingonly
  collected 1 item                                                                                                                      
  
  tests/test_testingonly.py .                                                                                                     [100%]
  
  ========================================================== 1 passed in 0.12s ==========================================================
  </code></pre>