Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name '_remove_dead_weakref'

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'
like image 315
Miguel Halanoca Ramos Avatar asked Jan 26 '18 15:01

Miguel Halanoca Ramos


2 Answers

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
like image 92
Comzyh Avatar answered Oct 03 '22 07:10

Comzyh


Adding to @Comzyh answer, this indeed is due to mixed python versions when an upgrade happen due to any reason. A quick fix is to remove your venv python binary i.e. rm <path-to-your-env>/bin/python and copying your system python binary to your venv like cp /usr/bin/python <path-to-your-env>/bin/python. This will fix the weak ref error

like image 30
Mohsin Khan Avatar answered Oct 03 '22 06:10

Mohsin Khan