本文介绍了ImportError:无法导入名称"_remove_dead_weakref"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  bash -cl"/home/encuentrum/venv-encuentrum3/bin/python/usr/share/pycharm/helpers/pycharm/django_manage.py check/home/encuentrum/GitLab/encuentrum3/ENCUENTRUM/packers_"追溯(最近一次通话):< module>中的文件"/usr/share/pycharm/helpers/pycharm/django_manage.py",第5行.从pycharm_run_utils导入Adjust_django_sys_path< module>中的文件"/usr/share/pycharm/helpers/pycharm/pycharm_run_utils.py",第4行.进口小商品< module>中的文件"/home/encuentrum/venv-encuentrum3/lib/python3.6/imp.py",第19行.从importlib._bootstrap导入_ERR_MSG,_exec,_load,_builtin_from_name在< module>中的文件"/home/encuentrum/venv-encuentrum3/lib/python3.6/importlib/__init__.py"中,第57行.进口类型在< module>中的文件"/home/encuentrum/venv-encuentrum3/lib/python3.6/types.py"第171行.将functools导入为_functools< module>中的文件"/home/encuentrum/venv-encuentrum3/lib/python3.6/functools.py",第23行.从weakref导入WeakKeyDictionary< module>中的文件"/home/encuentrum/venv-encuentrum3/lib/python3.6/weakref.py",第12行.从_weakref导入(ImportError:无法导入名称"_remove_dead_weakref" 
解决方案

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

分析

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

当我在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

当我通过apt-get更新我的Python3.5安装时,apt-get执行 python3.5 -E -S/usr/lib/python3.5/py_compile.py $ files (post-install脚本)中,它会触发weakref问题,这是我的日志

 设置python3.5-minimal(3.5.3-1 + deb9u1)...追溯(最近一次通话):< module>中的文件"/usr/lib/python3.5/py_compile.py",第6行.导入importlib._bootstrap_external< module>中的文件"/usr/lib/python3.5/importlib/__init__.py",第57行.进口类型< module>中的文件"/usr/lib/python3.5/types.py",行166.将functools导入为_functools< module>中的文件"/usr/lib/python3.5/functools.py",第23行.从weakref导入WeakKeyDictionary< module>中的文件"/usr/lib/python3.5/weakref.py",第12行.从_weakref导入(ImportError:无法导入名称"_remove_dead_weakref" 

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

来自apt-get的官方Python 3.5.3

  Python 3.5.3(默认,2018年9月27日,17:25:39)Linux上的[GCC 6.3.0 20170516]>>>从_weakref导入_remove_dead_weakref>>> 

我自己的Python 3.5.1安装

  Python 3.5.1(默认,2016年4月23日,16:40:21)Linux上的[GCC 4.9.2]键入帮助",版权",信用"或许可证"以获取更多信息.>>>>>>从_weakref导入_remove_dead_weakref追溯(最近一次通话):在< module>中的文件< stdin>",第1行,ImportError:无法导入名称"_remove_dead_weakref">>> 

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

  $其中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 

测试

  $其中python3.5/usr/bin/python3.5 

Yesterday updated my ubuntu 17.04 to ubuntu 17.10. any comments? Appear when I try to run server in 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'
解决方案

Maybe you have mixed your multiple Python installations, the newer version of weakref are not compatible with older version python binary, try to remove any one (the older one is recommended) of Python installation.

Analysis

For my case, I have installed older version Python (3.5.1) before, and upgrade my Debian installation. The newer Debian upgrade it's Python3.5 to 3.5.3 which have _remove_dead_weakref in _weakref in its Python binary

When I type $ where python3.5, I get

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

The /usr/local/bin/python3.5 is my own older installation, and /usr/bin/python3.5 is Debian offical Python3.5

When I update my Python3.5 installation by apt-get, apt-get execute python3.5 -E -S /usr/lib/python3.5/py_compile.py $files (post-install script) in the deb package.`, it triggers the weakref issue, here is my log

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'

I tested Python 3.5.1 and Python 3.5.3 with same import action, here are the compares

Official Python 3.5.3 from apt-get

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

My own Python 3.5.1 installation

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'
>>> 

So, I confirm that python3.5 in /usr/local/bin/ cannot use _remove_dead_weakref.But which python did apt-get use in post-installation script? Try it.

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

So, here is why. The post-installation script use my custom installation of python, along with newer python library (/usr/lib/python3.5/weakref.py)

Fix it!

As I said, disable older version of python

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

Test

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

这篇关于ImportError:无法导入名称"_remove_dead_weakref"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 13:40