ImportError while running tests
Answered
I have a test.py file that works well from the command line, but fails from PyCharm.
From the command line it runs the test correctly. Here is a snapshot of the test execution from command line:

Running the same test from PyCharm fails with the message "ImportError while importing test module 'C:\workspace\Anvil\Crating\test.py'.
Hint: make sure your test modules/packages have valid Python names.". Here is a snapshot of the test execution from PyCharm:

Please sign in to leave a comment.
What your run configuration looks like?
If you copy the command that PyCharm runs and run it in the command line, what's the result?
Hi Sergey,
I was able to get the test to run in PyCharm by moving test.py to Tests\test.py.
The PyCharm command doesn't work from command line, it says "ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it"
As I said now I can run the test, so I'm OK. But it would be nice to understand why it didn't work at root level (PyCharm should provide better error messages), or, better, get it to work as it does from command line.
I was unable to attach text files, so here are the text files that I had prepared for you:
--------------------- command line - root level.txt ---------------------
(anvil) C:\workspace\Anvil\Crating>python -m unittest test.py
Connecting to wss://anvil.works/uplink
Anvil websocket open
Connected to "Development environments only" as SERVER
---------- upload_test_data ----------
---------- release_items_1 ----------
Anvil server output: get_releases("", "Show Uploaded", "Undefined")
Anvil server output:
---------- release_1 ----------
Anvil server output: get_row_only_last_version("1234-12-1234", "R001")
Anvil server output:
Anvil server output: save_crates("1234-12-1234", "R001", ...)
Anvil server output:
---------- release_2 ----------
Anvil server output: get_row_only_last_version("1234-12-1234", "R001")
Anvil server output:
Anvil server output: save_crates("1234-12-1234", "R001", ...)
Anvil server output:
.
----------------------------------------------------------------------
Ran 1 test in 2.911s
OK
--------------------- command line - subfolder.txt ---------------------
(anvil) C:\workspace\Anvil\Crating>python -m unittest Tests\test.py
Connecting to wss://anvil.works/uplink
Anvil websocket open
Connected to "Development environments only" as SERVER
---------- upload_test_data ----------
---------- release_items_1 ----------
Anvil server output: get_releases("", "Show Uploaded", "Undefined")
Anvil server output:
---------- release_1 ----------
Anvil server output: get_row_only_last_version("1234-12-1234", "R001")
Anvil server output:
Anvil server output: save_crates("1234-12-1234", "R001", ...)
Anvil server output:
---------- release_2 ----------
Anvil server output: get_row_only_last_version("1234-12-1234", "R001")
Anvil server output:
Anvil server output: save_crates("1234-12-1234", "R001", ...)
Anvil server output:
.
----------------------------------------------------------------------
Ran 1 test in 3.418s
OK
--------------------- PyCharm - root level.txt ---------------------
C:\Users\stefanomenci\AppData\Local\conda\conda\envs\anvil\python.exe "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 62848 --file "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pycharm\_jb_pytest_runner.py" --path C:/workspace/Anvil/Crating/test.py
Testing started at 9:19 ...
Connected to pydev debugger (build 212.5080.64)
Launching pytest with arguments C:/workspace/Anvil/Crating/test.py --no-header --no-summary -q in C:\workspace\Anvil\Crating
============================= test session starts =============================
collecting ...
test.py:None (test.py)
ImportError while importing test module 'C:\workspace\Anvil\Crating\test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
C:\Users\stefanomenci\AppData\Local\conda\conda\envs\anvil\lib\site-packages\_pytest\python.py:578: in _importtestmodule
mod = import_path(self.fspath, mode=importmode)
C:\Users\stefanomenci\AppData\Local\conda\conda\envs\anvil\lib\site-packages\_pytest\pathlib.py:524: in import_path
importlib.import_module(module_name)
C:\Users\stefanomenci\AppData\Local\conda\conda\envs\anvil\lib\importlib\__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:994: in _gcd_import
???
<frozen importlib._bootstrap>:971: in _find_and_load
???
<frozen importlib._bootstrap>:953: in _find_and_load_unlocked
???
E ModuleNotFoundError: No module named 'Crating.test'
collected 0 items / 1 error
!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
============================== 1 error in 0.45s ===============================
Process finished with exit code 2
--------------------- PyCharm - subfolder.txt ---------------------
C:\Users\stefanomenci\AppData\Local\conda\conda\envs\anvil\python.exe "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 59351 --file "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pycharm\_jb_pytest_runner.py" --path C:/workspace/Anvil/Crating/Tests/test.py
Testing started at 9:20 ...
Connected to pydev debugger (build 212.5080.64)
Launching pytest with arguments C:/workspace/Anvil/Crating/Tests/test.py --no-header --no-summary -q in C:\workspace\Anvil\Crating
============================= test session starts =============================
collecting ... collected 1 item
Tests/test.py::TestUpload::test_releases
============================== 1 passed in 4.19s ==============================
Process finished with exit code 0
PASSED [100%]
---------- upload_test_data ----------
---------- release_items_1 ----------
Anvil server output: get_releases("", "Show Uploaded", "Undefined")
Anvil server output:
---------- release_1 ----------
Anvil server output: get_row_only_last_version("1234-12-1234", "R001")
Anvil server output:
Anvil server output: save_crates("1234-12-1234", "R001", ...)
Anvil server output:
---------- release_2 ----------
Anvil server output: get_row_only_last_version("1234-12-1234", "R001")
Anvil server output:
Anvil server output: save_crates("1234-12-1234", "R001", ...)
Anvil server output:
--------------------- command line PyCharm command - root level.txt ---------------------
(anvil) C:\workspace\Anvil\Crating>C:\Users\stefanomenci\AppData\Local\conda\conda\envs\anvil\python.exe "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 62848 --file "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pycharm\_jb_pytest_runner.py" --path C:/workspace/Anvil/Crating/test.py
Could not connect to 127.0.0.1: 62848
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 458, in start_client
s.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pydev\pydevd.py", line 2173, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pydev\pydevd.py", line 2055, in main
dispatcher.connect(host, port)
File "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pydev\pydevd.py", line 1826, in connect
self.client = start_client(self.host, self.port)
File "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 458, in start_client
s.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
--------------------- command line PyCharm command - subfolder.txt ---------------------
(anvil) C:\workspace\Anvil\Crating>C:\Users\stefanomenci\AppData\Local\conda\conda\envs\anvil\python.exe "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 59351 --file "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pycharm\_jb_pytest_runner.py" --path C:/workspace/Anvil/Crating/Tests/test.py
Could not connect to 127.0.0.1: 59351
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 458, in start_client
s.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pydev\pydevd.py", line 2173, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pydev\pydevd.py", line 2055, in main
dispatcher.connect(host, port)
File "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pydev\pydevd.py", line 1826, in connect
self.client = start_client(self.host, self.port)
File "C:\Program Files\JetBrains\PyCharm 2018.3\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 458, in start_client
s.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Could you check if the issue reproduces in a brand new project with some simple test?