0

下面的 main.py 在 PyCharm 中运行良好,但如果我尝试通过终端在 virtualenv 中运行 main.py,我会收到以下错误:

主要.py:

from plotservice import plot_point
from dialog import dialogue

def main():
    """ Main entry point of the app """
    print("hello world")
    d = dialogue()
    d.run_dialog()
    x, y, z = d.get_data()
    plot_point(-x, y, z)


if __name__ == "__main__":
    """ This is executed when run from the command line """
    main()

错误:

➜ python3 main.py
hello world
2019-08-30 21:40:02.712 Python[6862:195147] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7fba7d6d8110
2019-08-30 21:40:02.714 Python[6862:195147] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7fba7d6d8110'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff35f332fd __exceptionPreprocess + 256
    1   libobjc.A.dylib                     0x00007fff60604a17 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff35fad106 -[NSObject(NSObject) __retain_OA] + 0
    3   CoreFoundation                      0x00007fff35ed518f ___forwarding___ + 1485
    4   CoreFoundation                      0x00007fff35ed4b38 _CF_forwarding_prep_0 + 120
    5   libtk8.6.dylib                      0x000000011a3241f2 TkpInit + 408
    6   libtk8.6.dylib                      0x000000011a293aac Initialize + 2454
    7   _tkinter.cpython-37m-darwin.so      0x00000001190ecdc4 Tcl_AppInit + 84
    8   _tkinter.cpython-37m-darwin.so      0x00000001190eca98 _tkinter_create + 1144
    9   Python                              0x000000010c12a4ee _PyMethodDef_RawFastCallKeywords + 430
    10  Python                              0x000000010c129a5a _PyCFunction_FastCallKeywords + 42
    11  Python                              0x000000010c1e85a4 call_function + 724
    12  Python                              0x000000010c1e5576 _PyEval_EvalFrameDefault + 25190
    13  Python                              0x000000010c1e90d6 _PyEval_EvalCodeWithName + 2422
    14  Python                              0x000000010c1295fb _PyFunction_FastCallDict + 523
    15  Python                              0x000000010c12a8cf _PyObject_Call_Prepend + 143
    16  Python                              0x000000010c178d51 slot_tp_init + 145
    17  Python                              0x000000010c1746a9 type_call + 297
    18  Python                              0x000000010c129871 _PyObject_FastCallKeywords + 433
    19  Python                              0x000000010c1e8474 call_function + 420
    20  Python                              0x000000010c1e5576 _PyEval_EvalFrameDefault + 25190
    21  Python                              0x000000010c129e90 function_code_fastcall + 128
    22  Python                              0x000000010c1e85b2 call_function + 738
    23  Python                              0x000000010c1e5617 _PyEval_EvalFrameDefault + 25351
    24  Python                              0x000000010c1e90d6 _PyEval_EvalCodeWithName + 2422
    25  Python                              0x000000010c129a21 _PyFunction_FastCallKeywords + 257
    26  Python                              0x000000010c1e85b2 call_function + 738
    27  Python                              0x000000010c1e555c _PyEval_EvalFrameDefault + 25164
    28  Python                              0x000000010c1e90d6 _PyEval_EvalCodeWithName + 2422
    29  Python                              0x000000010c129a21 _PyFunction_FastCallKeywords + 257
    30  Python                              0x000000010c1e85b2 call_function + 738
    31  Python                              0x000000010c1e555c _PyEval_EvalFrameDefault + 25164
    32  Python                              0x000000010c129e90 function_code_fastcall + 128
    33  Python                              0x000000010c1e85b2 call_function + 738
    34  Python                              0x000000010c1e555c _PyEval_EvalFrameDefault + 25164
    35  Python                              0x000000010c129e90 function_code_fastcall + 128
    36  Python                              0x000000010c1e85b2 call_function + 738
    37  Python                              0x000000010c1e5617 _PyEval_EvalFrameDefault + 25351
    38  Python                              0x000000010c1e90d6 _PyEval_EvalCodeWithName + 2422
    39  Python                              0x000000010c1df234 PyEval_EvalCode + 100
    40  Python                              0x000000010c21c8f1 PyRun_FileExFlags + 209
    41  Python                              0x000000010c21c16a PyRun_SimpleFileExFlags + 890
    42  Python                              0x000000010c23b9db pymain_main + 6875
    43  Python                              0x000000010c23bf2a _Py_UnixMain + 58
    44  libdyld.dylib                       0x00007fff61dd23d5 start + 1
    45  ???                                 0x0000000000000002 0x0 + 2
)
libc++abi.dylib: terminating with uncaught exception of type NSException
[1]    6862 abort      python3 main.py

收到上面的错误码后,python本身就崩溃了……

4

1 回答 1

0

问题可能是您尚未安装要导入的软件包,因为“Hello world”确实会打印出来,并且程序只会在之后崩溃。

PyCharm 在其自己的 Python 安装上运行,因此在 PyCharm 中安装包不会使它们可用于从控制台执行时使用的“真实”Python。

pip install plotservice
pip install dialog

应该解决问题。

于 2019-09-02T06:24:59.103 回答