【发布时间】:2020-03-26 23:18:24
【问题描述】:
我创建了一个 virtualenv 使用:
python3 -m venv /path/to/new/virtual/environment
当我使用从那个virtualenv配置到解释器的Pycharm时,我无法在运行时添加变量,我无法向手表添加变量,我只能打印一些变量的值,也就是说,应用程序不起作用一点也不。这也发生在 Pycharm 2017、2018 和 2019 中。问题进一步出现在所有模块中,即使模块基本上是空白的。我得到的确切错误消息是当我尝试为cc 赋值时是:
cc=9
Traceback (most recent call last):
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3184, in run_ast_nodes
code = compiler(mod, cell_name, "exec")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/codeop.py", line 133, in __call__
codeob = compile(source, filename, symbol, self.flags, 1)
TypeError: required field "type_ignores" missing from Module
当我使用不是使用virtualenv 创建的解释器运行 Pycharm 时,我没有任何问题。现在让我证明我已正确完成所有 Pycharm 设置。当我打印 sys.version 时,我得到:
3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27)
Pycharm 总是打印执行时使用的解释器,即:
/Users/kylefoley/codes/venv/bin/python "/Applications/PyCharm CE 2.app/Contents/helpers/pydev/pydevd.py"
venv 是 virtualenv 创建的目录。
更新
我将venv 文件夹中的所有站点包放入系统解释器中,但我再次遇到了同样的错误。因此,抛出错误的模块一定有问题
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3184, in run_ast_nodes
我什至不知道为什么要使用IPython。这就是模块所说的错误发生的地方,这对我来说没有任何意义:
def run_ast_nodes(self, nodelist:ListType[AST], cell_name:str, interactivity='last_expr',
compiler=compile, result=None):
"""Run a sequence of AST nodes. The execution mode depends on the
interactivity parameter.
Parameters
----------
nodelist : list
A sequence of AST nodes to run.
cell_name : str
Will be passed to the compiler as the filename of the cell. Typically
the value returned by ip.compile.cache(cell).
interactivity : str
'all', 'last', 'last_expr' , 'last_expr_or_assign' or 'none',
specifying which nodes should be run interactively (displaying output
from expressions). 'last_expr' will run the last node interactively
only if it is an expression (i.e. expressions in loops or other blocks
are not displayed) 'last_expr_or_assign' will run the last expression
or the last assignment. Other values for this parameter will raise a
ValueError.
Experimental value: 'async' Will try to run top level interactive
async/await code in default runner, this will not respect the
interactivty setting and will only run the last node if it is an
expression.
compiler : callable
A function with the same interface as the built-in compile(), to turn
the AST nodes into code objects. Default is the built-in compile().
result : ExecutionResult, optional
An object to store exceptions that occur during execution.
Returns
-------
True if an exception occurred while running code, False if it finished
running.
"""
if not nodelist:
return
if interactivity == 'last_expr_or_assign':
if isinstance(nodelist[-1], _assign_nodes):
asg = nodelist[-1]
if isinstance(asg, ast.Assign) and len(asg.targets) == 1:
target = asg.targets[0]
elif isinstance(asg, _single_targets_nodes):
target = asg.target
else:
target = None
if isinstance(target, ast.Name):
nnode = ast.Expr(ast.Name(target.id, ast.Load()))
ast.fix_missing_locations(nnode)
nodelist.append(nnode)
interactivity = 'last_expr'
_async = False
if interactivity == 'last_expr':
if isinstance(nodelist[-1], ast.Expr):
interactivity = "last"
else:
interactivity = "none"
if interactivity == 'none':
to_run_exec, to_run_interactive = nodelist, []
elif interactivity == 'last':
to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
elif interactivity == 'all':
to_run_exec, to_run_interactive = [], nodelist
elif interactivity == 'async':
_async = True
else:
raise ValueError("Interactivity was %r" % interactivity)
try:
if _async:
# If interactivity is async the semantics of run_code are
# completely different Skip usual machinery.
mod = ast.Module(nodelist)
async_wrapper_code = compiler(mod, 'cell_name', 'exec')
exec(async_wrapper_code, self.user_global_ns, self.user_ns)
async_code = removed_co_newlocals(self.user_ns.pop('async-def-wrapper')).__code__
if (yield from self.run_code(async_code, result, async_=True)):
return True
else:
for i, node in enumerate(to_run_exec):
mod = ast.Module([node])
code = compiler(mod, cell_name, "exec")
if (yield from self.run_code(code, result)):
return True
for i, node in enumerate(to_run_interactive):
mod = ast.Interactive([node])
code = compiler(mod, cell_name, "single")
if (yield from self.run_code(code, result)):
return True
# Flush softspace
if softspace(sys.stdout, 0):
print()
except:
# It's possible to have exceptions raised here, typically by
# compilation of odd code (such as a naked 'return' outside a
# function) that did parse but isn't valid. Typically the exception
# is a SyntaxError, but it's safest just to catch anything and show
# the user a traceback.
# We do only one try/except outside the loop to minimize the impact
# on runtime, and also because if any node in the node list is
# broken, we should stop execution completely.
if result:
result.error_before_exec = sys.exc_info()[1]
self.showtraceback()
return True
return False
【问题讨论】:
-
您应该在 JetBrains 的错误跟踪器上报告此错误:youtrack.jetbrains.com/issues/PY
-
我已经这样做了,但是如果您查看他们的错误跟踪器,您会发现请求很少得到答复。
-
我一直在pycharm中使用virtualenv,从来没有遇到过这种情况。
标签: python pycharm virtualenv