Unittests failing to import the class under Windows

Hello,

When trying to run a unittest for my Python class, PyCharm under Windows is failing to import the actual class into the unittest.

My unittest is placed inside a directory called `test`. The same configuration under Linux works fine. Below are the messages PyCharm reports:


C:\Python27\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pycharm\utrunner.py" C:/westsoft/vbet5/swarm/trunk/test/ true
Testing started at 3:57 PM ...
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pycharm\utrunner.py", line 107, in <module>
   modules = loadModulesFromFolderRec(a[0])
File "C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pycharm\utrunner.py", line 57, in loadModulesFromFolderRec
   os.path.walk(folder, walkModules, (modules, pattern))
File "C:\Python27\lib\ntpath.py", line 259, in walk
   func(arg, top, names)
File "C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pycharm\utrunner.py", line 45, in walkModules
   modules.append(loadSource(os.path.join(dirname, name)))
File "C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pycharm\utrunner.py", line 34, in loadSource
   module = imp.load_source(moduleName, fileName)
File "C:/westsoft/vbet5/swarm/trunk/test/test_parser.py", line 4, in <module>
   from parser import Parser
ImportError: cannot import name Parser

Process finished with exit code 1


My project settings are attached.

IMG_29082013_115801.png
5 comments
Hi Silver,

is it your own parser package with Parser class which is not imported? If so I think problem relates to sys.path (because Python itself has builtin parser module)
Please, try to print sys.path to console and attach output.
Add this lines to C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pycharm\utrunner.py before line 34
import sys
from pprint import pprint
pprint(sys.path) 
prettyPrint();
0
It is my own parser module with Parser class. This is my directory structure:


./test:
drwxrwxr-x 3 silver silver  4096 Aug 28 17:50 .
drwxrwxr-x 6 silver silver  4096 Aug 29 19:13 ..
  • rw-rw-r– 1 silver silver  1788 Jul 11 21:26 test_differ.py
  • rw-rw-r– 1 silver silver  1640 Aug 28 17:50 test_level.py
  • rw-rw-r– 1 silver silver 12134 Aug 27 16:16 test_parser.py  # <–
  • rw-rw-r– 1 silver silver   941 Jul 18 17:00 test_request.py
  • rw-rw-r– 1 silver silver 11314 Aug 27 18:21 test_swarmdata.py
  • rw-rw-r– 1 silver silver   624 Jul 24 16:21 test_swarm.py

.:
drwxrwxr-x  6 silver silver  4096 Aug 29 19:13 .
drwxrwxr-x 16 silver silver  4096 Jul 30 18:40 ..
  • rw-rw-r–  1 silver silver 14072 Aug 29 19:13 data.py
  • rw-rw-r–  1 silver silver   232 Jul 10 17:45 game.py
drwxrwxr-x  5 silver silver  4096 Aug 30 15:32 .idea
  • rw-rw-r–  1 silver silver  5080 Aug 28 18:08 level.py
  • rw-rw-r–  1 silver silver    22 Jul 23 15:18 overlord.py
  • rw-rw-r–  1 silver silver  5053 Aug 27 16:16 parser.py    # <–
  • rw-rw-r–  1 silver silver  1454 Aug  9 15:20 service.py
  • rw-rw-r–  1 silver silver  1537 Jul 18 19:31 session.py
drwxrwxr-x  4 silver silver  4096 Aug 29 19:21 .svn
  • rw-rw-r–  1 silver silver    48 Jul 23 17:03 swarm.cfg
  • rw-rw-r–  1 silver silver  2457 Jul 30 16:32 swarm.py
drwxrwxr-x  3 silver silver  4096 Aug 28 17:50 test
  • rw-rw-r–  1 silver silver   499 Jul 11 21:18 util.py


Output for:
import sys
from pprint import pprint
pprint(sys.path) 
prettyPrint();


>>> sys.path
['C:\\Program Files (x86)\\JetBrains\\PyCharm 2.7.3\\helpers\\pydev', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', 'C:\\westsoft\\vbet5\\swarm\\trunk']
0
I think I found an issue:


C:\Python27\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pycharm\utrunner.py" C:/westsoft/vbet5/swarm/trunk/test/ true
Testing started at 3:40 PM ...
['C:/westsoft/vbet5/swarm/trunk/test',    # <– forward slash??
'C:\\westsoft\\vbet5\\swarm\\trunk',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages']
['C:/westsoft/vbet5/swarm/trunk/test',    # <– forward slash??
'C:\\westsoft\\vbet5\\swarm\\trunk',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages']
Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pycharm\utrunner.py", line 110, in <module>
    modules = loadModulesFromFolderRec(a[0])
  File "C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pycharm\utrunner.py", line 60, in loadModulesFromFolderRec
    os.path.walk(folder, walkModules, (modules, pattern))
  File "C:\Python27\lib\ntpath.py", line 259, in walk
    func(arg, top, names)
  File "C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pycharm\utrunner.py", line 48, in walkModules
    modules.append(loadSource(os.path.join(dirname, name)))
  File "C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pycharm\utrunner.py", line 37, in loadSource


See those forward slashes? Are those legit Windows paths?
0
Hi Silver,

My tests (running in Windows) have the same output (with forward slashes) and run well. But when trying to import anything from the file parser.py I have the same traceback output. Please try to rename your parser.py file and tell if it helps.
0
Hi Silver,

thanks for pointing this issue out! Fix'll be available in next PyCharm EAP.
0

Please sign in to leave a comment.