PytestWarning: Module already imported so cannot be rewritten: pytest_remotedata












3















I created some unit tests and run them from the same file. For tests in the same file:



if __name__ == "__main__":
import pytest
pytest.main(['--tb=short', __file__])


For tests in another file:



if __name__ == '__main__':
import pytest
pytest.main(['./test_stuff.py', "--capture=sys"])


In either case, when I execute the file the first time, it works fine, but the second and subsequent times it gives a bunch of warnings:



============================== warnings summary ===============================
C:Anaconda3libsite-packages_pytestconfig__init__.py:754
C:Anaconda3libsite-packages_pytestconfig__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_remotedata
self._mark_plugins_for_rewrite(hook)
C:Anaconda3libsite-packages_pytestconfig__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_openfiles
self._mark_plugins_for_rewrite(hook)
C:Anaconda3libsite-packages_pytestconfig__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_doctestplus
self._mark_plugins_for_rewrite(hook)
C:Anaconda3libsite-packages_pytestconfig__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_arraydiff
self._mark_plugins_for_rewrite(hook)

-- Docs: https://docs.pytest.org/en/latest/warnings.html
==================== 1 passed, 4 warnings in 0.06 seconds


Is there any way to make these warnings go away?



Restarting the kernel works, but IPython's %reset and %clear aren't enough to fix it, either.










share|improve this question





























    3















    I created some unit tests and run them from the same file. For tests in the same file:



    if __name__ == "__main__":
    import pytest
    pytest.main(['--tb=short', __file__])


    For tests in another file:



    if __name__ == '__main__':
    import pytest
    pytest.main(['./test_stuff.py', "--capture=sys"])


    In either case, when I execute the file the first time, it works fine, but the second and subsequent times it gives a bunch of warnings:



    ============================== warnings summary ===============================
    C:Anaconda3libsite-packages_pytestconfig__init__.py:754
    C:Anaconda3libsite-packages_pytestconfig__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_remotedata
    self._mark_plugins_for_rewrite(hook)
    C:Anaconda3libsite-packages_pytestconfig__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_openfiles
    self._mark_plugins_for_rewrite(hook)
    C:Anaconda3libsite-packages_pytestconfig__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_doctestplus
    self._mark_plugins_for_rewrite(hook)
    C:Anaconda3libsite-packages_pytestconfig__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_arraydiff
    self._mark_plugins_for_rewrite(hook)

    -- Docs: https://docs.pytest.org/en/latest/warnings.html
    ==================== 1 passed, 4 warnings in 0.06 seconds


    Is there any way to make these warnings go away?



    Restarting the kernel works, but IPython's %reset and %clear aren't enough to fix it, either.










    share|improve this question



























      3












      3








      3








      I created some unit tests and run them from the same file. For tests in the same file:



      if __name__ == "__main__":
      import pytest
      pytest.main(['--tb=short', __file__])


      For tests in another file:



      if __name__ == '__main__':
      import pytest
      pytest.main(['./test_stuff.py', "--capture=sys"])


      In either case, when I execute the file the first time, it works fine, but the second and subsequent times it gives a bunch of warnings:



      ============================== warnings summary ===============================
      C:Anaconda3libsite-packages_pytestconfig__init__.py:754
      C:Anaconda3libsite-packages_pytestconfig__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_remotedata
      self._mark_plugins_for_rewrite(hook)
      C:Anaconda3libsite-packages_pytestconfig__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_openfiles
      self._mark_plugins_for_rewrite(hook)
      C:Anaconda3libsite-packages_pytestconfig__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_doctestplus
      self._mark_plugins_for_rewrite(hook)
      C:Anaconda3libsite-packages_pytestconfig__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_arraydiff
      self._mark_plugins_for_rewrite(hook)

      -- Docs: https://docs.pytest.org/en/latest/warnings.html
      ==================== 1 passed, 4 warnings in 0.06 seconds


      Is there any way to make these warnings go away?



      Restarting the kernel works, but IPython's %reset and %clear aren't enough to fix it, either.










      share|improve this question
















      I created some unit tests and run them from the same file. For tests in the same file:



      if __name__ == "__main__":
      import pytest
      pytest.main(['--tb=short', __file__])


      For tests in another file:



      if __name__ == '__main__':
      import pytest
      pytest.main(['./test_stuff.py', "--capture=sys"])


      In either case, when I execute the file the first time, it works fine, but the second and subsequent times it gives a bunch of warnings:



      ============================== warnings summary ===============================
      C:Anaconda3libsite-packages_pytestconfig__init__.py:754
      C:Anaconda3libsite-packages_pytestconfig__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_remotedata
      self._mark_plugins_for_rewrite(hook)
      C:Anaconda3libsite-packages_pytestconfig__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_openfiles
      self._mark_plugins_for_rewrite(hook)
      C:Anaconda3libsite-packages_pytestconfig__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_doctestplus
      self._mark_plugins_for_rewrite(hook)
      C:Anaconda3libsite-packages_pytestconfig__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_arraydiff
      self._mark_plugins_for_rewrite(hook)

      -- Docs: https://docs.pytest.org/en/latest/warnings.html
      ==================== 1 passed, 4 warnings in 0.06 seconds


      Is there any way to make these warnings go away?



      Restarting the kernel works, but IPython's %reset and %clear aren't enough to fix it, either.







      python python-import pytest






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 16:04







      endolith

















      asked Jan 2 at 15:58









      endolithendolith

      11k1884157




      11k1884157
























          3 Answers
          3






          active

          oldest

          votes


















          1














          Use subprocess instead of pytest.main:



          if __name__ == "__main__":
          import subprocess
          subprocess.call(['pytest', '--tb=short', str(__file__)])


          If the above does not print anything, try the workaround (as suggested in comments):



          if __name__ == "__main__":
          from subprocess import Popen, PIPE
          with Popen(['pytest',
          '--tb=short', # shorter traceback format
          str(__file__)], stdout=PIPE, bufsize=1,
          universal_newlines=True) as p:
          for line in p.stdout:
          print(line, end='')





          share|improve this answer


























          • This doesn't print the output at all, though... :/

            – endolith
            Feb 19 at 3:09











          • It does. :) At least in Spyder IDE with ipython console (Python 3.7). Also direct call from command line and repeated calls from inside ipython do print anything you like... Maybe you catch somewhere else your sdtout and stderr?

            – mrkwjc
            Feb 19 at 8:08











          • No it doesn't print anything in Spyder. This works though: github.com/pytest-dev/pytest/issues/3143#issuecomment-464973797

            – endolith
            Feb 19 at 14:50






          • 1





            Well, my configuration differs only in pytest. I have version 4.2.0. Neverthless i edited the answer to cover the no printing case.

            – mrkwjc
            Feb 20 at 22:31






          • 1





            I just tested things on Windows and subprocessed pytest really does not print output. It seems this is Windows specific problem.

            – mrkwjc
            Feb 22 at 21:37



















          2














          It would appear that pytest just isn't really designed to perform pytest.main() calls repeatedly from within the same process. The pytest documentation mentions:




          Calling pytest.main() will result in importing your tests and any
          modules that they import. Due to the caching mechanism of python’s
          import system, making subsequent calls to pytest.main() from the same
          process will not reflect changes to those files between the calls. For
          this reason, making multiple calls to pytest.main() from the same
          process (in order to re-run tests, for example) is not recommended.




          So actually the real danger of performing multiple pytest.main() calls within the same process is that you could get false-positive or false-negative test results if you've edited your code files inbetween.



          The Spyder IDE has a nifty feature though that seems to negate this issue: the User Module Reloader (UMR). If enabled, then any changed user modules are automatically reloaded when a script file is (re-)run.



          Therefore I think that as long as you're working in Spyder (with the UMR feature enabled!), you can safely rerun pytest.main() without the need for a new console. The pytest module already-imported warnings you can then simply suppress, since those pytest modules will not have changed. This can be done with pytest's -W flag. Example:



          if __name__ == '__main__':
          import pytest
          pytest.main(['./test_stuff.py', "--capture=sys", "-W", "ignore:Module already imported:pytest.PytestWarning"])





          share|improve this answer





















          • 1





            It didn't used to have this problem. Is there a recommended way to run tests every time a file is edited and run?

            – endolith
            Jan 2 at 22:49






          • 1





            @endolith My own recommendation would be to use to an IDE that supports custom run configurations. Within the IDE set up a dedicated run configuration for your pytest tests (one that always launches a new process).

            – Xukrao
            Jan 3 at 15:33








          • 1





            @endolith "Execute in dedicated console" results in a new console (i.e. new process) being started upon first run of the code file, but not upon subsequent reruns of the same code file. "Remove all variables" results in only user-defined variables being removed. Try using the run configuration options as described in the edited answer instead.

            – Xukrao
            Jan 4 at 19:40






          • 1





            @endolith "evert time I edit it, I Run it and it says whether the tests still pass." This surprised me at first. I was expecting that subsequent test runs would be giving false-positive or false-negative results (due to edited code files not being reloaded). However after discovering and reading about Spyder's User Module Reloader (UMR) it's starting to make sense. This UMR ensures that previously imported user modules are automatically reloaded if they've been changed (quite a useful feature).

            – Xukrao
            Jan 4 at 23:23






          • 1





            @endolith I think this means that as long as you're working in Spyder, you can actually safely rerun pytest.main() without the need for a new console. The warnings that you were getting you can simply suppress (see edited answer).

            – Xukrao
            Jan 4 at 23:24



















          1














          If you are only concerned about warnings, you can use --disable-warnings argument. You can also filter out only the warnings you are getting now with --pythonwarnings=PYTHONWARNINGS argument in pytest.main . pytest --help has more info about these arguments.



          It seems that you are invoking pytest with ipython or jupyter. python.main imports pytest specific modules at the time of pytest initialization. When you run pytest.main again, it throws the warnings. There are two possible ways to reload pytest to avoid getting this initialization warnings.



          You can either use pytest.exit after pytest.main or reload pytest after pytest.main .



          Let us know if these approaches work.






          share|improve this answer
























          • Be careful. While these solutions will indeed get rid of the pytest warnings, one larger issue remains: the user's to-be-tested code files are not reloaded each time, so any changes made to the code files are ignored during subsequent pytest runs.

            – Xukrao
            Jan 3 at 16:04











          • exit and reload don't work :/

            – endolith
            Jan 4 at 4:54











          • --disable-warnings works, but --pythonwarnings=PYTHONWARNINGS says INTERNALERROR> File "C:Anaconda3libwarnings.py", line 236, in _getaction INTERNALERROR> raise _OptionError("invalid action: %r" % (action,)) INTERNALERROR> warnings._OptionError: invalid action: 'PYTHONWARNINGS'

            – endolith
            Jan 5 at 21:00











          • Correct syntax to hide particular warning in this case (taken from an answer here): pytest.main(['./test_stuff.py', "--capture=sys", "--pythonwarnings", "ignore:Module already imported:pytest.PytestWarning"]) You should look into reloading pytest module as that will be the cleanest solution in your case.

            – SilentGuy
            Jan 7 at 20:11













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009371%2fpytestwarning-module-already-imported-so-cannot-be-rewritten-pytest-remotedata%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Use subprocess instead of pytest.main:



          if __name__ == "__main__":
          import subprocess
          subprocess.call(['pytest', '--tb=short', str(__file__)])


          If the above does not print anything, try the workaround (as suggested in comments):



          if __name__ == "__main__":
          from subprocess import Popen, PIPE
          with Popen(['pytest',
          '--tb=short', # shorter traceback format
          str(__file__)], stdout=PIPE, bufsize=1,
          universal_newlines=True) as p:
          for line in p.stdout:
          print(line, end='')





          share|improve this answer


























          • This doesn't print the output at all, though... :/

            – endolith
            Feb 19 at 3:09











          • It does. :) At least in Spyder IDE with ipython console (Python 3.7). Also direct call from command line and repeated calls from inside ipython do print anything you like... Maybe you catch somewhere else your sdtout and stderr?

            – mrkwjc
            Feb 19 at 8:08











          • No it doesn't print anything in Spyder. This works though: github.com/pytest-dev/pytest/issues/3143#issuecomment-464973797

            – endolith
            Feb 19 at 14:50






          • 1





            Well, my configuration differs only in pytest. I have version 4.2.0. Neverthless i edited the answer to cover the no printing case.

            – mrkwjc
            Feb 20 at 22:31






          • 1





            I just tested things on Windows and subprocessed pytest really does not print output. It seems this is Windows specific problem.

            – mrkwjc
            Feb 22 at 21:37
















          1














          Use subprocess instead of pytest.main:



          if __name__ == "__main__":
          import subprocess
          subprocess.call(['pytest', '--tb=short', str(__file__)])


          If the above does not print anything, try the workaround (as suggested in comments):



          if __name__ == "__main__":
          from subprocess import Popen, PIPE
          with Popen(['pytest',
          '--tb=short', # shorter traceback format
          str(__file__)], stdout=PIPE, bufsize=1,
          universal_newlines=True) as p:
          for line in p.stdout:
          print(line, end='')





          share|improve this answer


























          • This doesn't print the output at all, though... :/

            – endolith
            Feb 19 at 3:09











          • It does. :) At least in Spyder IDE with ipython console (Python 3.7). Also direct call from command line and repeated calls from inside ipython do print anything you like... Maybe you catch somewhere else your sdtout and stderr?

            – mrkwjc
            Feb 19 at 8:08











          • No it doesn't print anything in Spyder. This works though: github.com/pytest-dev/pytest/issues/3143#issuecomment-464973797

            – endolith
            Feb 19 at 14:50






          • 1





            Well, my configuration differs only in pytest. I have version 4.2.0. Neverthless i edited the answer to cover the no printing case.

            – mrkwjc
            Feb 20 at 22:31






          • 1





            I just tested things on Windows and subprocessed pytest really does not print output. It seems this is Windows specific problem.

            – mrkwjc
            Feb 22 at 21:37














          1












          1








          1







          Use subprocess instead of pytest.main:



          if __name__ == "__main__":
          import subprocess
          subprocess.call(['pytest', '--tb=short', str(__file__)])


          If the above does not print anything, try the workaround (as suggested in comments):



          if __name__ == "__main__":
          from subprocess import Popen, PIPE
          with Popen(['pytest',
          '--tb=short', # shorter traceback format
          str(__file__)], stdout=PIPE, bufsize=1,
          universal_newlines=True) as p:
          for line in p.stdout:
          print(line, end='')





          share|improve this answer















          Use subprocess instead of pytest.main:



          if __name__ == "__main__":
          import subprocess
          subprocess.call(['pytest', '--tb=short', str(__file__)])


          If the above does not print anything, try the workaround (as suggested in comments):



          if __name__ == "__main__":
          from subprocess import Popen, PIPE
          with Popen(['pytest',
          '--tb=short', # shorter traceback format
          str(__file__)], stdout=PIPE, bufsize=1,
          universal_newlines=True) as p:
          for line in p.stdout:
          print(line, end='')






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 20 at 22:32

























          answered Feb 13 at 9:07









          mrkwjcmrkwjc

          330311




          330311













          • This doesn't print the output at all, though... :/

            – endolith
            Feb 19 at 3:09











          • It does. :) At least in Spyder IDE with ipython console (Python 3.7). Also direct call from command line and repeated calls from inside ipython do print anything you like... Maybe you catch somewhere else your sdtout and stderr?

            – mrkwjc
            Feb 19 at 8:08











          • No it doesn't print anything in Spyder. This works though: github.com/pytest-dev/pytest/issues/3143#issuecomment-464973797

            – endolith
            Feb 19 at 14:50






          • 1





            Well, my configuration differs only in pytest. I have version 4.2.0. Neverthless i edited the answer to cover the no printing case.

            – mrkwjc
            Feb 20 at 22:31






          • 1





            I just tested things on Windows and subprocessed pytest really does not print output. It seems this is Windows specific problem.

            – mrkwjc
            Feb 22 at 21:37



















          • This doesn't print the output at all, though... :/

            – endolith
            Feb 19 at 3:09











          • It does. :) At least in Spyder IDE with ipython console (Python 3.7). Also direct call from command line and repeated calls from inside ipython do print anything you like... Maybe you catch somewhere else your sdtout and stderr?

            – mrkwjc
            Feb 19 at 8:08











          • No it doesn't print anything in Spyder. This works though: github.com/pytest-dev/pytest/issues/3143#issuecomment-464973797

            – endolith
            Feb 19 at 14:50






          • 1





            Well, my configuration differs only in pytest. I have version 4.2.0. Neverthless i edited the answer to cover the no printing case.

            – mrkwjc
            Feb 20 at 22:31






          • 1





            I just tested things on Windows and subprocessed pytest really does not print output. It seems this is Windows specific problem.

            – mrkwjc
            Feb 22 at 21:37

















          This doesn't print the output at all, though... :/

          – endolith
          Feb 19 at 3:09





          This doesn't print the output at all, though... :/

          – endolith
          Feb 19 at 3:09













          It does. :) At least in Spyder IDE with ipython console (Python 3.7). Also direct call from command line and repeated calls from inside ipython do print anything you like... Maybe you catch somewhere else your sdtout and stderr?

          – mrkwjc
          Feb 19 at 8:08





          It does. :) At least in Spyder IDE with ipython console (Python 3.7). Also direct call from command line and repeated calls from inside ipython do print anything you like... Maybe you catch somewhere else your sdtout and stderr?

          – mrkwjc
          Feb 19 at 8:08













          No it doesn't print anything in Spyder. This works though: github.com/pytest-dev/pytest/issues/3143#issuecomment-464973797

          – endolith
          Feb 19 at 14:50





          No it doesn't print anything in Spyder. This works though: github.com/pytest-dev/pytest/issues/3143#issuecomment-464973797

          – endolith
          Feb 19 at 14:50




          1




          1





          Well, my configuration differs only in pytest. I have version 4.2.0. Neverthless i edited the answer to cover the no printing case.

          – mrkwjc
          Feb 20 at 22:31





          Well, my configuration differs only in pytest. I have version 4.2.0. Neverthless i edited the answer to cover the no printing case.

          – mrkwjc
          Feb 20 at 22:31




          1




          1





          I just tested things on Windows and subprocessed pytest really does not print output. It seems this is Windows specific problem.

          – mrkwjc
          Feb 22 at 21:37





          I just tested things on Windows and subprocessed pytest really does not print output. It seems this is Windows specific problem.

          – mrkwjc
          Feb 22 at 21:37













          2














          It would appear that pytest just isn't really designed to perform pytest.main() calls repeatedly from within the same process. The pytest documentation mentions:




          Calling pytest.main() will result in importing your tests and any
          modules that they import. Due to the caching mechanism of python’s
          import system, making subsequent calls to pytest.main() from the same
          process will not reflect changes to those files between the calls. For
          this reason, making multiple calls to pytest.main() from the same
          process (in order to re-run tests, for example) is not recommended.




          So actually the real danger of performing multiple pytest.main() calls within the same process is that you could get false-positive or false-negative test results if you've edited your code files inbetween.



          The Spyder IDE has a nifty feature though that seems to negate this issue: the User Module Reloader (UMR). If enabled, then any changed user modules are automatically reloaded when a script file is (re-)run.



          Therefore I think that as long as you're working in Spyder (with the UMR feature enabled!), you can safely rerun pytest.main() without the need for a new console. The pytest module already-imported warnings you can then simply suppress, since those pytest modules will not have changed. This can be done with pytest's -W flag. Example:



          if __name__ == '__main__':
          import pytest
          pytest.main(['./test_stuff.py', "--capture=sys", "-W", "ignore:Module already imported:pytest.PytestWarning"])





          share|improve this answer





















          • 1





            It didn't used to have this problem. Is there a recommended way to run tests every time a file is edited and run?

            – endolith
            Jan 2 at 22:49






          • 1





            @endolith My own recommendation would be to use to an IDE that supports custom run configurations. Within the IDE set up a dedicated run configuration for your pytest tests (one that always launches a new process).

            – Xukrao
            Jan 3 at 15:33








          • 1





            @endolith "Execute in dedicated console" results in a new console (i.e. new process) being started upon first run of the code file, but not upon subsequent reruns of the same code file. "Remove all variables" results in only user-defined variables being removed. Try using the run configuration options as described in the edited answer instead.

            – Xukrao
            Jan 4 at 19:40






          • 1





            @endolith "evert time I edit it, I Run it and it says whether the tests still pass." This surprised me at first. I was expecting that subsequent test runs would be giving false-positive or false-negative results (due to edited code files not being reloaded). However after discovering and reading about Spyder's User Module Reloader (UMR) it's starting to make sense. This UMR ensures that previously imported user modules are automatically reloaded if they've been changed (quite a useful feature).

            – Xukrao
            Jan 4 at 23:23






          • 1





            @endolith I think this means that as long as you're working in Spyder, you can actually safely rerun pytest.main() without the need for a new console. The warnings that you were getting you can simply suppress (see edited answer).

            – Xukrao
            Jan 4 at 23:24
















          2














          It would appear that pytest just isn't really designed to perform pytest.main() calls repeatedly from within the same process. The pytest documentation mentions:




          Calling pytest.main() will result in importing your tests and any
          modules that they import. Due to the caching mechanism of python’s
          import system, making subsequent calls to pytest.main() from the same
          process will not reflect changes to those files between the calls. For
          this reason, making multiple calls to pytest.main() from the same
          process (in order to re-run tests, for example) is not recommended.




          So actually the real danger of performing multiple pytest.main() calls within the same process is that you could get false-positive or false-negative test results if you've edited your code files inbetween.



          The Spyder IDE has a nifty feature though that seems to negate this issue: the User Module Reloader (UMR). If enabled, then any changed user modules are automatically reloaded when a script file is (re-)run.



          Therefore I think that as long as you're working in Spyder (with the UMR feature enabled!), you can safely rerun pytest.main() without the need for a new console. The pytest module already-imported warnings you can then simply suppress, since those pytest modules will not have changed. This can be done with pytest's -W flag. Example:



          if __name__ == '__main__':
          import pytest
          pytest.main(['./test_stuff.py', "--capture=sys", "-W", "ignore:Module already imported:pytest.PytestWarning"])





          share|improve this answer





















          • 1





            It didn't used to have this problem. Is there a recommended way to run tests every time a file is edited and run?

            – endolith
            Jan 2 at 22:49






          • 1





            @endolith My own recommendation would be to use to an IDE that supports custom run configurations. Within the IDE set up a dedicated run configuration for your pytest tests (one that always launches a new process).

            – Xukrao
            Jan 3 at 15:33








          • 1





            @endolith "Execute in dedicated console" results in a new console (i.e. new process) being started upon first run of the code file, but not upon subsequent reruns of the same code file. "Remove all variables" results in only user-defined variables being removed. Try using the run configuration options as described in the edited answer instead.

            – Xukrao
            Jan 4 at 19:40






          • 1





            @endolith "evert time I edit it, I Run it and it says whether the tests still pass." This surprised me at first. I was expecting that subsequent test runs would be giving false-positive or false-negative results (due to edited code files not being reloaded). However after discovering and reading about Spyder's User Module Reloader (UMR) it's starting to make sense. This UMR ensures that previously imported user modules are automatically reloaded if they've been changed (quite a useful feature).

            – Xukrao
            Jan 4 at 23:23






          • 1





            @endolith I think this means that as long as you're working in Spyder, you can actually safely rerun pytest.main() without the need for a new console. The warnings that you were getting you can simply suppress (see edited answer).

            – Xukrao
            Jan 4 at 23:24














          2












          2








          2







          It would appear that pytest just isn't really designed to perform pytest.main() calls repeatedly from within the same process. The pytest documentation mentions:




          Calling pytest.main() will result in importing your tests and any
          modules that they import. Due to the caching mechanism of python’s
          import system, making subsequent calls to pytest.main() from the same
          process will not reflect changes to those files between the calls. For
          this reason, making multiple calls to pytest.main() from the same
          process (in order to re-run tests, for example) is not recommended.




          So actually the real danger of performing multiple pytest.main() calls within the same process is that you could get false-positive or false-negative test results if you've edited your code files inbetween.



          The Spyder IDE has a nifty feature though that seems to negate this issue: the User Module Reloader (UMR). If enabled, then any changed user modules are automatically reloaded when a script file is (re-)run.



          Therefore I think that as long as you're working in Spyder (with the UMR feature enabled!), you can safely rerun pytest.main() without the need for a new console. The pytest module already-imported warnings you can then simply suppress, since those pytest modules will not have changed. This can be done with pytest's -W flag. Example:



          if __name__ == '__main__':
          import pytest
          pytest.main(['./test_stuff.py', "--capture=sys", "-W", "ignore:Module already imported:pytest.PytestWarning"])





          share|improve this answer















          It would appear that pytest just isn't really designed to perform pytest.main() calls repeatedly from within the same process. The pytest documentation mentions:




          Calling pytest.main() will result in importing your tests and any
          modules that they import. Due to the caching mechanism of python’s
          import system, making subsequent calls to pytest.main() from the same
          process will not reflect changes to those files between the calls. For
          this reason, making multiple calls to pytest.main() from the same
          process (in order to re-run tests, for example) is not recommended.




          So actually the real danger of performing multiple pytest.main() calls within the same process is that you could get false-positive or false-negative test results if you've edited your code files inbetween.



          The Spyder IDE has a nifty feature though that seems to negate this issue: the User Module Reloader (UMR). If enabled, then any changed user modules are automatically reloaded when a script file is (re-)run.



          Therefore I think that as long as you're working in Spyder (with the UMR feature enabled!), you can safely rerun pytest.main() without the need for a new console. The pytest module already-imported warnings you can then simply suppress, since those pytest modules will not have changed. This can be done with pytest's -W flag. Example:



          if __name__ == '__main__':
          import pytest
          pytest.main(['./test_stuff.py', "--capture=sys", "-W", "ignore:Module already imported:pytest.PytestWarning"])






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 5 at 0:05

























          answered Jan 2 at 19:14









          XukraoXukrao

          2,4632930




          2,4632930








          • 1





            It didn't used to have this problem. Is there a recommended way to run tests every time a file is edited and run?

            – endolith
            Jan 2 at 22:49






          • 1





            @endolith My own recommendation would be to use to an IDE that supports custom run configurations. Within the IDE set up a dedicated run configuration for your pytest tests (one that always launches a new process).

            – Xukrao
            Jan 3 at 15:33








          • 1





            @endolith "Execute in dedicated console" results in a new console (i.e. new process) being started upon first run of the code file, but not upon subsequent reruns of the same code file. "Remove all variables" results in only user-defined variables being removed. Try using the run configuration options as described in the edited answer instead.

            – Xukrao
            Jan 4 at 19:40






          • 1





            @endolith "evert time I edit it, I Run it and it says whether the tests still pass." This surprised me at first. I was expecting that subsequent test runs would be giving false-positive or false-negative results (due to edited code files not being reloaded). However after discovering and reading about Spyder's User Module Reloader (UMR) it's starting to make sense. This UMR ensures that previously imported user modules are automatically reloaded if they've been changed (quite a useful feature).

            – Xukrao
            Jan 4 at 23:23






          • 1





            @endolith I think this means that as long as you're working in Spyder, you can actually safely rerun pytest.main() without the need for a new console. The warnings that you were getting you can simply suppress (see edited answer).

            – Xukrao
            Jan 4 at 23:24














          • 1





            It didn't used to have this problem. Is there a recommended way to run tests every time a file is edited and run?

            – endolith
            Jan 2 at 22:49






          • 1





            @endolith My own recommendation would be to use to an IDE that supports custom run configurations. Within the IDE set up a dedicated run configuration for your pytest tests (one that always launches a new process).

            – Xukrao
            Jan 3 at 15:33








          • 1





            @endolith "Execute in dedicated console" results in a new console (i.e. new process) being started upon first run of the code file, but not upon subsequent reruns of the same code file. "Remove all variables" results in only user-defined variables being removed. Try using the run configuration options as described in the edited answer instead.

            – Xukrao
            Jan 4 at 19:40






          • 1





            @endolith "evert time I edit it, I Run it and it says whether the tests still pass." This surprised me at first. I was expecting that subsequent test runs would be giving false-positive or false-negative results (due to edited code files not being reloaded). However after discovering and reading about Spyder's User Module Reloader (UMR) it's starting to make sense. This UMR ensures that previously imported user modules are automatically reloaded if they've been changed (quite a useful feature).

            – Xukrao
            Jan 4 at 23:23






          • 1





            @endolith I think this means that as long as you're working in Spyder, you can actually safely rerun pytest.main() without the need for a new console. The warnings that you were getting you can simply suppress (see edited answer).

            – Xukrao
            Jan 4 at 23:24








          1




          1





          It didn't used to have this problem. Is there a recommended way to run tests every time a file is edited and run?

          – endolith
          Jan 2 at 22:49





          It didn't used to have this problem. Is there a recommended way to run tests every time a file is edited and run?

          – endolith
          Jan 2 at 22:49




          1




          1





          @endolith My own recommendation would be to use to an IDE that supports custom run configurations. Within the IDE set up a dedicated run configuration for your pytest tests (one that always launches a new process).

          – Xukrao
          Jan 3 at 15:33







          @endolith My own recommendation would be to use to an IDE that supports custom run configurations. Within the IDE set up a dedicated run configuration for your pytest tests (one that always launches a new process).

          – Xukrao
          Jan 3 at 15:33






          1




          1





          @endolith "Execute in dedicated console" results in a new console (i.e. new process) being started upon first run of the code file, but not upon subsequent reruns of the same code file. "Remove all variables" results in only user-defined variables being removed. Try using the run configuration options as described in the edited answer instead.

          – Xukrao
          Jan 4 at 19:40





          @endolith "Execute in dedicated console" results in a new console (i.e. new process) being started upon first run of the code file, but not upon subsequent reruns of the same code file. "Remove all variables" results in only user-defined variables being removed. Try using the run configuration options as described in the edited answer instead.

          – Xukrao
          Jan 4 at 19:40




          1




          1





          @endolith "evert time I edit it, I Run it and it says whether the tests still pass." This surprised me at first. I was expecting that subsequent test runs would be giving false-positive or false-negative results (due to edited code files not being reloaded). However after discovering and reading about Spyder's User Module Reloader (UMR) it's starting to make sense. This UMR ensures that previously imported user modules are automatically reloaded if they've been changed (quite a useful feature).

          – Xukrao
          Jan 4 at 23:23





          @endolith "evert time I edit it, I Run it and it says whether the tests still pass." This surprised me at first. I was expecting that subsequent test runs would be giving false-positive or false-negative results (due to edited code files not being reloaded). However after discovering and reading about Spyder's User Module Reloader (UMR) it's starting to make sense. This UMR ensures that previously imported user modules are automatically reloaded if they've been changed (quite a useful feature).

          – Xukrao
          Jan 4 at 23:23




          1




          1





          @endolith I think this means that as long as you're working in Spyder, you can actually safely rerun pytest.main() without the need for a new console. The warnings that you were getting you can simply suppress (see edited answer).

          – Xukrao
          Jan 4 at 23:24





          @endolith I think this means that as long as you're working in Spyder, you can actually safely rerun pytest.main() without the need for a new console. The warnings that you were getting you can simply suppress (see edited answer).

          – Xukrao
          Jan 4 at 23:24











          1














          If you are only concerned about warnings, you can use --disable-warnings argument. You can also filter out only the warnings you are getting now with --pythonwarnings=PYTHONWARNINGS argument in pytest.main . pytest --help has more info about these arguments.



          It seems that you are invoking pytest with ipython or jupyter. python.main imports pytest specific modules at the time of pytest initialization. When you run pytest.main again, it throws the warnings. There are two possible ways to reload pytest to avoid getting this initialization warnings.



          You can either use pytest.exit after pytest.main or reload pytest after pytest.main .



          Let us know if these approaches work.






          share|improve this answer
























          • Be careful. While these solutions will indeed get rid of the pytest warnings, one larger issue remains: the user's to-be-tested code files are not reloaded each time, so any changes made to the code files are ignored during subsequent pytest runs.

            – Xukrao
            Jan 3 at 16:04











          • exit and reload don't work :/

            – endolith
            Jan 4 at 4:54











          • --disable-warnings works, but --pythonwarnings=PYTHONWARNINGS says INTERNALERROR> File "C:Anaconda3libwarnings.py", line 236, in _getaction INTERNALERROR> raise _OptionError("invalid action: %r" % (action,)) INTERNALERROR> warnings._OptionError: invalid action: 'PYTHONWARNINGS'

            – endolith
            Jan 5 at 21:00











          • Correct syntax to hide particular warning in this case (taken from an answer here): pytest.main(['./test_stuff.py', "--capture=sys", "--pythonwarnings", "ignore:Module already imported:pytest.PytestWarning"]) You should look into reloading pytest module as that will be the cleanest solution in your case.

            – SilentGuy
            Jan 7 at 20:11


















          1














          If you are only concerned about warnings, you can use --disable-warnings argument. You can also filter out only the warnings you are getting now with --pythonwarnings=PYTHONWARNINGS argument in pytest.main . pytest --help has more info about these arguments.



          It seems that you are invoking pytest with ipython or jupyter. python.main imports pytest specific modules at the time of pytest initialization. When you run pytest.main again, it throws the warnings. There are two possible ways to reload pytest to avoid getting this initialization warnings.



          You can either use pytest.exit after pytest.main or reload pytest after pytest.main .



          Let us know if these approaches work.






          share|improve this answer
























          • Be careful. While these solutions will indeed get rid of the pytest warnings, one larger issue remains: the user's to-be-tested code files are not reloaded each time, so any changes made to the code files are ignored during subsequent pytest runs.

            – Xukrao
            Jan 3 at 16:04











          • exit and reload don't work :/

            – endolith
            Jan 4 at 4:54











          • --disable-warnings works, but --pythonwarnings=PYTHONWARNINGS says INTERNALERROR> File "C:Anaconda3libwarnings.py", line 236, in _getaction INTERNALERROR> raise _OptionError("invalid action: %r" % (action,)) INTERNALERROR> warnings._OptionError: invalid action: 'PYTHONWARNINGS'

            – endolith
            Jan 5 at 21:00











          • Correct syntax to hide particular warning in this case (taken from an answer here): pytest.main(['./test_stuff.py', "--capture=sys", "--pythonwarnings", "ignore:Module already imported:pytest.PytestWarning"]) You should look into reloading pytest module as that will be the cleanest solution in your case.

            – SilentGuy
            Jan 7 at 20:11
















          1












          1








          1







          If you are only concerned about warnings, you can use --disable-warnings argument. You can also filter out only the warnings you are getting now with --pythonwarnings=PYTHONWARNINGS argument in pytest.main . pytest --help has more info about these arguments.



          It seems that you are invoking pytest with ipython or jupyter. python.main imports pytest specific modules at the time of pytest initialization. When you run pytest.main again, it throws the warnings. There are two possible ways to reload pytest to avoid getting this initialization warnings.



          You can either use pytest.exit after pytest.main or reload pytest after pytest.main .



          Let us know if these approaches work.






          share|improve this answer













          If you are only concerned about warnings, you can use --disable-warnings argument. You can also filter out only the warnings you are getting now with --pythonwarnings=PYTHONWARNINGS argument in pytest.main . pytest --help has more info about these arguments.



          It seems that you are invoking pytest with ipython or jupyter. python.main imports pytest specific modules at the time of pytest initialization. When you run pytest.main again, it throws the warnings. There are two possible ways to reload pytest to avoid getting this initialization warnings.



          You can either use pytest.exit after pytest.main or reload pytest after pytest.main .



          Let us know if these approaches work.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 0:11









          SilentGuySilentGuy

          17016




          17016













          • Be careful. While these solutions will indeed get rid of the pytest warnings, one larger issue remains: the user's to-be-tested code files are not reloaded each time, so any changes made to the code files are ignored during subsequent pytest runs.

            – Xukrao
            Jan 3 at 16:04











          • exit and reload don't work :/

            – endolith
            Jan 4 at 4:54











          • --disable-warnings works, but --pythonwarnings=PYTHONWARNINGS says INTERNALERROR> File "C:Anaconda3libwarnings.py", line 236, in _getaction INTERNALERROR> raise _OptionError("invalid action: %r" % (action,)) INTERNALERROR> warnings._OptionError: invalid action: 'PYTHONWARNINGS'

            – endolith
            Jan 5 at 21:00











          • Correct syntax to hide particular warning in this case (taken from an answer here): pytest.main(['./test_stuff.py', "--capture=sys", "--pythonwarnings", "ignore:Module already imported:pytest.PytestWarning"]) You should look into reloading pytest module as that will be the cleanest solution in your case.

            – SilentGuy
            Jan 7 at 20:11





















          • Be careful. While these solutions will indeed get rid of the pytest warnings, one larger issue remains: the user's to-be-tested code files are not reloaded each time, so any changes made to the code files are ignored during subsequent pytest runs.

            – Xukrao
            Jan 3 at 16:04











          • exit and reload don't work :/

            – endolith
            Jan 4 at 4:54











          • --disable-warnings works, but --pythonwarnings=PYTHONWARNINGS says INTERNALERROR> File "C:Anaconda3libwarnings.py", line 236, in _getaction INTERNALERROR> raise _OptionError("invalid action: %r" % (action,)) INTERNALERROR> warnings._OptionError: invalid action: 'PYTHONWARNINGS'

            – endolith
            Jan 5 at 21:00











          • Correct syntax to hide particular warning in this case (taken from an answer here): pytest.main(['./test_stuff.py', "--capture=sys", "--pythonwarnings", "ignore:Module already imported:pytest.PytestWarning"]) You should look into reloading pytest module as that will be the cleanest solution in your case.

            – SilentGuy
            Jan 7 at 20:11



















          Be careful. While these solutions will indeed get rid of the pytest warnings, one larger issue remains: the user's to-be-tested code files are not reloaded each time, so any changes made to the code files are ignored during subsequent pytest runs.

          – Xukrao
          Jan 3 at 16:04





          Be careful. While these solutions will indeed get rid of the pytest warnings, one larger issue remains: the user's to-be-tested code files are not reloaded each time, so any changes made to the code files are ignored during subsequent pytest runs.

          – Xukrao
          Jan 3 at 16:04













          exit and reload don't work :/

          – endolith
          Jan 4 at 4:54





          exit and reload don't work :/

          – endolith
          Jan 4 at 4:54













          --disable-warnings works, but --pythonwarnings=PYTHONWARNINGS says INTERNALERROR> File "C:Anaconda3libwarnings.py", line 236, in _getaction INTERNALERROR> raise _OptionError("invalid action: %r" % (action,)) INTERNALERROR> warnings._OptionError: invalid action: 'PYTHONWARNINGS'

          – endolith
          Jan 5 at 21:00





          --disable-warnings works, but --pythonwarnings=PYTHONWARNINGS says INTERNALERROR> File "C:Anaconda3libwarnings.py", line 236, in _getaction INTERNALERROR> raise _OptionError("invalid action: %r" % (action,)) INTERNALERROR> warnings._OptionError: invalid action: 'PYTHONWARNINGS'

          – endolith
          Jan 5 at 21:00













          Correct syntax to hide particular warning in this case (taken from an answer here): pytest.main(['./test_stuff.py', "--capture=sys", "--pythonwarnings", "ignore:Module already imported:pytest.PytestWarning"]) You should look into reloading pytest module as that will be the cleanest solution in your case.

          – SilentGuy
          Jan 7 at 20:11







          Correct syntax to hide particular warning in this case (taken from an answer here): pytest.main(['./test_stuff.py', "--capture=sys", "--pythonwarnings", "ignore:Module already imported:pytest.PytestWarning"]) You should look into reloading pytest module as that will be the cleanest solution in your case.

          – SilentGuy
          Jan 7 at 20:11




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009371%2fpytestwarning-module-already-imported-so-cannot-be-rewritten-pytest-remotedata%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          Npm cannot find a required file even through it is in the searched directory