报错信息
在 Debug Python 程序的时候(PyCharm),直接就报了以下错误:
ImportError: cannot import name 'InteractiveConsole' from 'code' (/home/xovee/Desktop/contrastive_cascade/code/__init__.py)
...
SyntaxError: Missing parentheses in call to 'exec'
完整报错代码
/home/xovee/anaconda3/envs/tf22cuda101/bin/python /snap/pycharm-community/202/plugins/python-ce/helpers/pydev/pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 46645 --file /home/xovee/Desktop/contrastive_cascade/code/cgc_distillation.py
Traceback (most recent call last):
File "/snap/pycharm-community/202/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_console_integration.py", line 4, in <module>
from code import InteractiveConsole
ImportError: cannot import name 'InteractiveConsole' from 'code' (/home/xovee/Desktop/contrastive_cascade/code/__init__.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/snap/pycharm-community/202/plugins/python-ce/helpers/pydev/pydevd.py", line 39, in <module>
from _pydevd_bundle.pydevd_comm import CMD_SET_BREAK, CMD_SET_NEXT_STATEMENT, CMD_STEP_INTO, CMD_STEP_OVER, \
File "/snap/pycharm-community/202/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_comm.py", line 91, in <module>
from _pydevd_bundle import pydevd_console_integration
File "/snap/pycharm-community/202/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_console_integration.py", line 6, in <module>
from _pydevd_bundle.pydevconsole_code_for_ironpython import InteractiveConsole
File "/snap/pycharm-community/202/plugins/python-ce/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
环境
- Python 3.7
- PyCharm 2020.1.3
解决方案
原因是代码所在文件夹被命名为 code,所以。。。把文件夹的名字改成其他的就可以了,例如 src。
Reference
- Gulzar. (Jul 27, 2020). pytest exec code in self.locals SyntaxError: Missing parentheses in call to ‘exec’. Retrieved from https://stackoverflow.com/questions/61696180/pytest-exec-code-in-self-locals-syntaxerror-missing-parentheses-in-call-to-exe