此文首發於我的個人博客:Python-PyCharm 報錯解決:ImportError: cannot import name ‘InteractiveConsole’ from ‘code’ — zhang0peter的個人博客
早上在用PyCharm跑Python代碼時遇到報錯:
Traceback (most recent call last):
File "C:\Users\peter\AppData\Local\JetBrains\Toolbox\apps\PyCharm-P\ch-0\193.5662.61\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_console_integration.py", line 4, in <module>
from code import InteractiveConsole
ImportError: cannot import name 'InteractiveConsole' from 'code' (C:\Users\peter\Documents\GitHub\code.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\peter\AppData\Local\JetBrains\Toolbox\apps\PyCharm-P\ch-0\193.5662.61\plugins\python\helpers\pydev\pydevd.py", line 37, in <module>
from _pydevd_bundle.pydevd_comm import CMD_SET_BREAK, CMD_SET_NEXT_STATEMENT, CMD_STEP_INTO, CMD_STEP_OVER, \
File "C:\Users\peter\AppData\Local\JetBrains\Toolbox\apps\PyCharm-P\ch-0\193.5662.61\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 91, in <module>
from _pydevd_bundle import pydevd_console_integration
File "C:\Users\peter\AppData\Local\JetBrains\Toolbox\apps\PyCharm-P\ch-0\193.5662.61\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_console_integration.py", line 6, in <module>
from _pydevd_bundle.pydevconsole_code_for_ironpython import InteractiveConsole
File "C:\Users\peter\AppData\Local\JetBrains\Toolbox\apps\PyCharm-P\ch-0\193.5662.61\plugins\python\helpers\pydev\_pydevd_bundle\pydevconsole_code_for_ironpython.py", line 305
exec code in self.locals
^
SyntaxError: Missing parentheses in call to 'exec'
Process finished with exit code 1
看到報錯ImportError: cannot import name 'InteractiveConsole' from 'code'
我一下子就知道哪裏出了問題,這是因爲我的代碼文件名就叫code.py
與PyCharm中用來處理輸出的代碼衝突了。
解決方法是把文件重命名就可以了。