Failed to import test module when running test from editor


I can't seem to run individual tests. Before updating to 2017.1, if I was editing the source code for a test, I could press CTRL-SHIFT-F10 and run the test immediately. However, now if I do this, I get the following error:

Traceback (most recent call last):
File "$HOME/anaconda2/envs/my_env/lib/python3.5/unittest/", line 58, in testPartExecutor
File "$HOME/anaconda2/envs/my_env/lib/python3.5/unittest/", line 600, in run
File "$HOME/anaconda2/envs/my_env/lib/python3.5/unittest/", line 34, in testFailure
raise self._exception
ImportError: Failed to import test module: test_foo
Traceback (most recent call last):
File "$HOME/anaconda2/envs/my_env/lib/python3.5/unittest/", line 153, in loadTestsFromName
module = __import__(module_name)
ImportError: No module named 'tests.test_foo'

If I instead right click the test file from the Project View and click 'Run Unittests in' it works, but I liked being able to run an individual test. Any ideas?


If it help: I can't seem to change the default interpreter that's used when running via CTRL+SHIFT+F10 (right now it's using the my_env environment). I changed the interpreter in Settings > Build, Execution, Deployment > Console > Python Consolue. This correctly works when I right click the file name and run from there. However, a different interpreter is used when running the test from the editor with CTRL+SHIFT+F10. I can manually change it afterwards if I go to Run > Edit Configurations..., bit this is annoying to do for every test I want to run. Also, changing the interpreter doesn't fix this import problem. 



Thank you for the report.

Could you please contact and attach your project to let us reproduce the issue? If it's not possible, could you please attach a screenshot with automatically created Run configuration?



I have this problem with PyCharm too, after upgrading to 2017 version. I can't do individual tests, the context menu works intermittently, sometime it shows the option, sometimes it doesn't. 

Please advise the fix?



I had the same problem, introduced after upgrading to 2017.1, Running a single test yields "Exception: No module named 'Tests'". A fix that worked for me was making sure that I had an file in my test directory.


The exact same problem for me could not be resolved by adding because it had already been there. :)

Following discussion on this link I managed to resolve the issue by renaming the python file containing the tests. Obviously this file and root folder can't have same names in pycharm 2017.1.

*NOTE*: The problem did not exist in earlier (e.g. 2016.x) pycham versions!


In my case, the path for the test runner was changed.  This seems to be due to Run Configurations->Python Tests->Unittests losing the default config some how.  Any new tests that are attempted fail.  Once changing the default back, and clearing the "corrupt" run configs, it works again.


Hi Nick! I am sorry for the inconvenience. Losing default settings is a known bug, should be fixed in 2017.3.2.


Hi Pavel, that is good news!  Can you share the details, specifically how to reproduce this issue?  I was not able to identify what actions caused it.


Sure, the problem is covered by PY-25397 ticket, more specifically it's triggered when you have changed defaults for multiple run configurations (pytest and nose, nose and unittest and so on), here is my comment with detailed steps to reproduce: By the way 2017.3.2 is already live.


Please sign in to leave a comment.