在3.5.3虚拟机中运行时,Vim中出现Python错误

2024-10-03 15:28:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我在ubuntu16.04上安装了多个Python版本。我使用pipenv创建虚拟环境来处理使用不同版本的项目。但是,对于python3.5.x只有在激活的virtualenv中使用Vim 8(通过PPA安装)编辑一个文件时,我会遇到这样的错误:

"test.py" 49L, 1434C
Error detected while processing function jedi#init_python[4]..<SNR>48_init_python[27]..jedi#setup_python_imports:
line   25:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/user/.local/share/virtualenvs/test-AYf6DmHL/lib/python3.5/imp.py", line 19, in <module>
    from importlib._bootstrap import _ERR_MSG, _exec, _load, _builtin_from_name
  File "/home/user/.local/share/virtualenvs/test-AYf6DmHL/lib/python3.5/importlib/__init__.py", line 57, in <module>
    import types
  File "/home/user/.local/share/virtualenvs/test-AYf6DmHL/lib/python3.5/types.py", line 166, in <module>
    import functools as _functools
  File "/home/user/.local/share/virtualenvs/test-AYf6DmHL/lib/python3.5/functools.py", line 23, in <module>
    from weakref import WeakKeyDictionary
  File "/home/user/.local/share/virtualenvs/test-AYf6DmHL/lib/python3.5/weakref.py", line 12, in <module>
    from _weakref import (
ImportError: cannot import name '_remove_dead_weakref'

这个问题似乎会影响到所有与python相关的Vim插件。对于其他Python版本(例如3.6.x、3.7.x),这种情况不会发生。是什么导致了这个问题?在

更新:似乎当我在3.5.x的virtualenv中运行Vim时,Vim的解释器sys.path会以某种方式得到更新。例如,这是3.6.0 virtualenv中:py3 import sys; print(sys.path)的输出:

['/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5 /dist-packages', '/usr/lib/python3/dist-packages', '_vim_path_']

这是3.5.3:

^{pr2}$

{cdr>可以在容器中复制问题。在


Tags: inpytestimportsharehomelibusr
3条回答

可能是其他python版本在这里干扰。你有PYTHONPATH集吗?在

echo $PYTHONPATH

如果设置了,可以尝试取消设置。在使用虚拟环境时通常不需要它。在

Debian Bug report logs - #852163: python3.5-minimal wont configure说明此错误(对于python3.5和python3.6)是由于在/usr/local中安装了另一个不兼容的Python。在

Some further digging with another user in #debian who was experiencing this problem indicated that it was related to a locally installed /usr/local/bin/python3.5. The stdlib in /usr/lib/python3.5 is picked up by the /usr/local/bin/python3.5 but is not compatible with it, resulting in the stacktrace shown.

Removing /usr/local/bin/python3* and all traces of that installation from /usr/local/lib (plus other associated tools) solved this problem for the user I was helping in #debian.

斯图尔特·普雷斯科特(link

这个问题是由系统安装的python引起的,它与个人安装的python冲突。在

尝试使用sudo运行python。我认为操作系统附带的python需要以管理员权限运行。在

相关问题 更多 >