Unittests failing to import the class under Windows Follow
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.
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.
Please sign in to leave a comment.
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
./test:
drwxrwxr-x 3 silver silver 4096 Aug 28 17:50 .
drwxrwxr-x 6 silver silver 4096 Aug 29 19:13 ..
.:
drwxrwxr-x 6 silver silver 4096 Aug 29 19:13 .
drwxrwxr-x 16 silver silver 4096 Jul 30 18:40 ..
Output for:
>>> 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']
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?
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.
thanks for pointing this issue out! Fix'll be available in next PyCharm EAP.