使用PyCharm导入模块时出现错误,使用Jupyter Notebook

时间:2017-10-10 07:07:44

标签: python pycharm jupyter-notebook python-import

对于项目,我需要从可变数据源导入DICOM文件。我正在研究PyCharm中的项目,因为我最终将迭代代码,最终产品将是一个程序。我得到的错误如下:

2017-10-10 08:58:16.307 Python [1010:26876] - [NSApplication _setup:]:无法识别的选择器发送到实例0x105154320

2017-10-10 08:58:16.311 Python [1010:26876] ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [NSApplication _setup:]:无法识别的选择器发送到实例0x105154320'< / p>

***首先抛出调用堆栈:

0   CoreFoundation                      0x00007fff9e92757b __exceptionPreprocess + 171
1   libobjc.A.dylib                     0x00007fffb3b801da objc_exception_throw + 48
2   CoreFoundation                      0x00007fff9e9a7f14 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3   CoreFoundation                      0x00007fff9e89ac93 ___forwarding___ + 1059
4   CoreFoundation                      0x00007fff9e89a7e8 _CF_forwarding_prep_0 + 120
5   Tk                                  0x000000010438cc02 TkpInit + 471
6   Tk                                  0x00000001043082a9 Tk_Init + 1794
7   _tkinter.cpython-36m-darwin.so      0x00000001041e0dfd Tcl_AppInit + 77
8   _tkinter.cpython-36m-darwin.so      0x00000001041de849 _tkinter_create + 889
9   Python                              0x000000010006a7b8 _PyCFunction_FastCallDict + 552
10  Python                              0x00000001000f4164 call_function + 612
11  Python                              0x00000001000f5b74 _PyEval_EvalFrameDefault + 5604
12  Python                              0x00000001000f3500 _PyEval_EvalCodeWithName + 2720
13  Python                              0x00000001000f43c7 _PyFunction_FastCallDict + 439
14  Python                              0x000000010000ea54 _PyObject_FastCallDict + 356
15  Python                              0x000000010000f120 _PyObject_Call_Prepend + 208
16  Python                              0x000000010000e443 PyObject_Call + 99
17  Python                              0x0000000100089af1 slot_tp_init + 81
18  Python                              0x00000001000802b4 type_call + 212
19  Python                              0x000000010000e9a4 _PyObject_FastCallDict + 180
20  Python                              0x00000001000f3fa5 call_function + 165
21  Python                              0x00000001000f5b74 _PyEval_EvalFrameDefault + 5604
22  Python                              0x00000001000f3eae fast_function + 606
23  Python                              0x00000001000f414b call_function + 587
24  Python                              0x00000001000f5b74 _PyEval_EvalFrameDefault + 5604
25  Python                              0x00000001000f3500 _PyEval_EvalCodeWithName + 2720
26  Python                              0x00000001000f3d2b fast_function + 219
27  Python                              0x00000001000f414b call_function + 587
28  Python                              0x00000001000f5b74 _PyEval_EvalFrameDefault + 5604
29  Python                              0x00000001000f3500 _PyEval_EvalCodeWithName + 2720
30  Python                              0x00000001000f43c7 _PyFunction_FastCallDict + 439
31  Python                              0x000000010000ea54 _PyObject_FastCallDict + 356
32  Python                              0x000000010000f120 _PyObject_Call_Prepend + 208
33  Python                              0x000000010000e443 PyObject_Call + 99
34  Python                              0x0000000100089af1 slot_tp_init + 81
35  Python                              0x00000001000802b4 type_call + 212
36  Python                              0x000000010000e9a4 _PyObject_FastCallDict + 180
37  Python                              0x00000001000f3fa5 call_function + 165
38  Python                              0x00000001000f5b74 _PyEval_EvalFrameDefault + 5604
39  Python                              0x00000001000f3500 _PyEval_EvalCodeWithName + 2720
40  Python                              0x00000001000f3d2b fast_function + 219
41  Python                              0x00000001000f414b call_function + 587
42  Python                              0x00000001000f5b74 _PyEval_EvalFrameDefault + 5604
43  Python                              0x00000001000f3500 _PyEval_EvalCodeWithName + 2720
44  Python                              0x00000001000f3d2b fast_function + 219
45  Python                              0x00000001000f414b call_function + 587
46  Python                              0x00000001000f5b74 _PyEval_EvalFrameDefault + 5604
47  Python                              0x00000001000f3500 _PyEval_EvalCodeWithName + 2720
48  Python                              0x00000001000f36a4 PyEval_EvalCode + 100
49  Python                              0x000000010013000e PyRun_FileExFlags + 206
50  Python                              0x00000001001302af PyRun_SimpleFileExFlags + 447
51  Python                              0x0000000100149b3a Py_Main + 3914
52  Python                              0x0000000100000dfe Python + 3582
53  Python                              0x0000000100000c34 Python + 3124
54  ???                                 0x0000000000000002 0x0 + 2

) libc ++ abi.dylib:以NSException类型的未捕获异常终止

使用退出代码134完成处理(由信号6中断:SIGABRT)

给我错误的代码如下:

import pydicom
from tkinter import filedialog
from matplotlib import pylab

"""Get fileName"""
#from tkinter import filedialog
filename = filedialog.askopenfilename()

"""Import DICOM File"""
#import pydicom
df = pydicom.read_file(filename)
dim = df.pixel_array

"""Show DICOM Image"""
#from matplotlib import pylab
pylab.imshow(dim, cmap="gray")
pylab.show()

当作为jupyter笔记本文件运行时,或者当导入位于注释行时,此精确代码不会产生相同的错误。

我不知道为什么会这样,我希望你知道我为什么会收到这个错误。我想将模块导入到脚本的顶部,因为,整洁和东西。

提前致谢!

1 个答案:

答案 0 :(得分:4)

我认为这是matplotlib TkAgg后端的一个问题。请尝试以下matplotlib库

import matplotlib
matplotlib.use("TkAgg")
from matplotlib import pyplot as plt