ImportError:无法导入名称' _remove_dead_weakref'

时间:2018-01-26 15:51:00

标签: python django pycharm

昨天将我的ubuntu 17.04更新为ubuntu 17.10。任何意见?当我尝试在pycharm中运行服务器时出现。 #django project。

bash -cl "/home/encuentrum/venv-encuentrum3/bin/python /usr/share/pycharm/helpers/pycharm/django_manage.py check /home/encuentrum/GitLab/encuentrum3/ENCUENTRUM/packers_"
Traceback (most recent call last):
  File "/usr/share/pycharm/helpers/pycharm/django_manage.py", line 5, in <module>
    from pycharm_run_utils import adjust_django_sys_path
  File "/usr/share/pycharm/helpers/pycharm/pycharm_run_utils.py", line 4, in <module>
    import imp
  File "/home/encuentrum/venv-encuentrum3/lib/python3.6/imp.py", line 19, in <module>
    from importlib._bootstrap import _ERR_MSG, _exec, _load, _builtin_from_name
  File "/home/encuentrum/venv-encuentrum3/lib/python3.6/importlib/__init__.py", line 57, in <module>
    import types
  File "/home/encuentrum/venv-encuentrum3/lib/python3.6/types.py", line 171, in <module>
    import functools as _functools
  File "/home/encuentrum/venv-encuentrum3/lib/python3.6/functools.py", line 23, in <module>
    from weakref import WeakKeyDictionary
  File "/home/encuentrum/venv-encuentrum3/lib/python3.6/weakref.py", line 12, in <module>
    from _weakref import (
ImportError: cannot import name '_remove_dead_weakref'

2 个答案:

答案 0 :(得分:1)

除了@Comzyh答案外,这确实是由于任何原因导致升级时混合的python版本。一种快速的解决方法是删除venv python二进制文件,即p,然后将系统python二进制文件复制到venv中,例如// 1st loop for(p = &charr[0]; p <= &charr[4]; p++) // printing statement // 2nd loop for(p = &charr[4]; p >= &charr[0]; p--) // reverse printing statement //3rd loop for(p = &charr[0]; p <= &charr[4]; p++) // sum statement 。这将解决弱引用错误

答案 1 :(得分:0)

也许您混合了多个Python安装,weakref的较新版本与较早版本的python二进制文件不兼容,请尝试删除任何一个(建议较旧的)Python安装。

分析

对于我来说,我之前已经安装了旧版本的Python(3.5.1),并升级了Debian安装。较新的Debian将其Python3.5升级到3.5.3,其Python二进制文件中的_remove_dead_weakref中有_weakref

当我输入$ where python3.5时,我得到

/usr/local/bin/python3.5    
/usr/local/bin/python3.5    
/usr/bin/python3.5

/usr/local/bin/python3.5是我自己的较旧安装,/usr/bin/python3.5是Debian官方Python3.5

当我通过deb软件包中的apt-get,apt-get execute python3.5 -E -S /usr/lib/python3.5/py_compile.py $files(安装后脚本)更新Python3.5安装时,会触发weakref问题,这是我的日志

Setting up python3.5-minimal (3.5.3-1+deb9u1) ...
Traceback (most recent call last):
  File "/usr/lib/python3.5/py_compile.py", line 6, in <module>
    import importlib._bootstrap_external
  File "/usr/lib/python3.5/importlib/__init__.py", line 57, in <module>
    import types
  File "/usr/lib/python3.5/types.py", line 166, in <module>
    import functools as _functools
  File "/usr/lib/python3.5/functools.py", line 23, in <module>
    from weakref import WeakKeyDictionary
  File "/usr/lib/python3.5/weakref.py", line 12, in <module>
    from _weakref import (
ImportError: cannot import name '_remove_dead_weakref'

我以相同的导入操作测试了Python 3.5.1和Python 3.5.3,下面是比较

来自apt-get的官方Python 3.5.3

Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
>>> from _weakref import _remove_dead_weakref
>>> 

我自己的Python 3.5.1安装

Python 3.5.1 (default, Apr 23 2016, 16:40:21) 
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> from _weakref import _remove_dead_weakref
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name '_remove_dead_weakref'
>>> 

因此,我确认/usr/local/bin/中的python3.5不能使用_remove_dead_weakref。 但是apt-get在安装后脚本中使用了哪个python?试试吧。

$ which python3.5
/usr/local/bin/python3.5

所以,这就是原因。安装后脚本使用我自定义的python安装以及更新的python库(/usr/lib/python3.5/weakref.py

解决!

正如我所说,禁用旧版本的python

sudo mv /usr/local/bin/python3.5 /usr/local/bin/python3.5.bak

测试

$ which python3.5
/usr/bin/python3.5