Pycharm -- RuntimeWarning: Parent module '...' not found while handling absolute import

在Ubuntu 中使用Pycharm 2016.2 出現該問題 :html

解決辦法是使用舊的utrunner.py替換新的utrunner.pypython

替換路徑 :django

**(pycharm path)/pycharm/helpers/pycharm/utrunner.pybash

下載utrunner.py或者新建.py文件 命名 爲utrunner.pyapp

1import sys
2import imp
3import os
4import fnmatch
5
6helpers_dir = os.getenv("PYCHARM_HELPERS_DIR", sys.path[0])
7if sys.path[0] != helpers_dir:
8  sys.path.insert(0, helpers_dir)
9
10from tcunittest import TeamcityTestRunner
11from nose_helper import TestLoader, ContextSuite
12from pycharm_run_utils import import_system_module
13from pycharm_run_utils import adjust_sys_path
14from pycharm_run_utils import debug, getModuleName, PYTHON_VERSION_MAJOR
15
16adjust_sys_path()
17
18os = import_system_module("os")
19re = import_system_module("re")
20
21modules = {}
22
23def loadSource(fileName):
24  baseName = os.path.basename(fileName)
25  moduleName = os.path.splitext(baseName)[0]
26
27  # for users wanted to run unittests under django
28  #because of django took advantage of module name
29  settings_file = os.getenv('DJANGO_SETTINGS_MODULE')
30  if settings_file and moduleName == "models":
31    baseName = os.path.realpath(fileName)
32    moduleName = ".".join((baseName.split(os.sep)[-2], "models"))
33
34  if moduleName in modules and len(sys.argv[1:-1]) == 1: # add unique number to prevent name collisions
35    cnt = 2
36    prefix = moduleName
37    while getModuleName(prefix, cnt) in modules:
38      cnt += 1
39    moduleName = getModuleName(prefix, cnt)
40  debug("/ Loading " + fileName + " as " + moduleName)
41  if os.path.isdir(fileName):
42    fileName = fileName + os.path.sep
43  module = imp.load_source(moduleName, fileName)
44  modules[moduleName] = module
45  return module
46
47def walkModules(modulesAndPattern, dirname, names):
48  modules = modulesAndPattern[0]
49  pattern = modulesAndPattern[1]
50  # fnmatch converts glob to regexp
51  prog_list = [re.compile(fnmatch.translate(pat.strip())) for pat in pattern.split(',')]
52  for name in names:
53    for prog in prog_list:
54      if name.endswith(".py") and prog.match(name):
55        modules.append(loadSource(os.path.join(dirname, name)))
56
57
58# For default pattern see https://docs.python.org/2/library/unittest.html#test-discovery
59def loadModulesFromFolderRec(folder, pattern="test*.py"):
60  modules = []
61  # fnmatch converts glob to regexp
62  prog_list = [re.compile(fnmatch.translate(pat.strip())) for pat in pattern.split(',')]
63  for root, dirs, files in os.walk(folder):
64    files = [f for f in files if not f[0] == '.']
65    dirs[:] = [d for d in dirs if not d[0] == '.']
66    for name in files:
67      for prog in prog_list:
68        if name.endswith(".py") and prog.match(name):
69          modules.append(loadSource(os.path.join(root, name)))
70  return modules
71
72testLoader = TestLoader()
73all = ContextSuite()
74pure_unittest = False
75
76def setLoader(module):
77  global testLoader, all
78  try:
79    module.__getattribute__('unittest2')
80    import unittest2
81
82    testLoader = unittest2.TestLoader()
83    all = unittest2.TestSuite()
84  except:
85    pass
86
87if __name__ == "__main__":
88  arg = sys.argv[-1]
89  if arg == "true":
90    import unittest
91
92    testLoader = unittest.TestLoader()
93    all = unittest.TestSuite()
94    pure_unittest = True
95
96  options = {}
97  for arg in sys.argv[1:-1]:
98    arg = arg.strip()
99    if len(arg) == 0:
100      continue
101
102    if arg.startswith("--"):
103      options[arg[2:]] = True
104      continue
105
106    a = arg.split("::")
107    if len(a) == 1:
108      # From module or folder
109      a_splitted = a[0].split("_args_separator_")  # ";" can't be used with bash, so we use "_args_separator_"
110      if len(a_splitted) != 1:
111        # means we have pattern to match against
112        if os.path.isdir(a_splitted[0]):
113          debug("/ from folder " + a_splitted[0] + ". Use pattern: " + a_splitted[1])
114          modules = loadModulesFromFolderRec(a_splitted[0], a_splitted[1])
115      else:
116        if  os.path.isdir(a[0]):
117          debug("/ from folder " + a[0])
118          modules = loadModulesFromFolderRec(a[0])
119        else:
120          debug("/ from module " + a[0])
121          modules = [loadSource(a[0])]
122
123      for module in modules:
124        all.addTests(testLoader.loadTestsFromModule(module))
125
126    elif len(a) == 2:
127      # From testcase
128      debug("/ from testcase " + a[1] + " in " + a[0])
129      module = loadSource(a[0])
130      setLoader(module)
131
132      if pure_unittest:
133        all.addTests(testLoader.loadTestsFromTestCase(getattr(module, a[1])))
134      else:
135        all.addTests(testLoader.loadTestsFromTestClass(getattr(module, a[1])),
136                     getattr(module, a[1]))
137    else:
138      # From method in class or from function
139      debug("/ from method " + a[2] + " in testcase " + a[1] + " in " + a[0])
140      module = loadSource(a[0])
141      setLoader(module)
142
143      if a[1] == "":
144        # test function, not method
145        all.addTest(testLoader.makeTest(getattr(module, a[2])))
146      else:
147        testCaseClass = getattr(module, a[1])
148        try:
149          all.addTest(testCaseClass(a[2]))
150        except:
151          # class is not a testcase inheritor
152          all.addTest(
153            testLoader.makeTest(getattr(testCaseClass, a[2]), testCaseClass))
154
155  debug("/ Loaded " + str(all.countTestCases()) + " tests")
156  TeamcityTestRunner().run(all, **options)
相關文章
相關標籤/搜索