ImportError:无法在django virtualenv中导入名称_remove_dead_weakref

时间:2019-04-04 11:41:59

标签: django python-2.7 virtualenv

将我的Ubuntu桌面从16.04升级到18.04后,Django的virtualenv拒绝启动:

(.djangoenv) mw@desktop:~/theapp$ python manage.py runserver
Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
  File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 10, in <module>
    from django.apps import apps
  File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/apps/__init__.py", line 1, in <module>
    from .config import AppConfig   # NOQA
  File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/apps/config.py", line 4, in <module>
    from django.core.exceptions import AppRegistryNotReady, ImproperlyConfigured
  File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/core/exceptions.py", line 5, in <module>
    from django.utils.encoding import force_text
  File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/utils/encoding.py", line 10, in <module>
    from django.utils.functional import Promise
  File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/utils/functional.py", line 1, in <module>
    import copy
  File "/usr/lib/python2.7/copy.py", line 52, in <module>
    import weakref
  File "/usr/lib/python2.7/weakref.py", line 14, in <module>
    from _weakref import (
ImportError: cannot import name _remove_dead_weakref 

djangoenv上和外部

$ python --version : 
`Python 2.7.12` 

希望解决该问题,我也在台式机上安装了Python 2.7.16,所以我得到了:

$ python2.7 --version
Python 2.7.16

当我尝试安装新的virtualenv时,也会出现相同的错误:

$ virtualenv .blaenv
Running virtualenv with interpreter /home/mw/.djangoenv/bin/python2
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 21, in <module>
    import logging
  File "/usr/lib/python2.7/logging/__init__.py", line 26, in <module>
    import sys, os, time, cStringIO, traceback, warnings, weakref, collections
  File "/usr/lib/python2.7/weakref.py", line 14, in <module>
    from _weakref import (
ImportError: cannot import name _remove_dead_weakref

在其他情况下回答相同的错误也无济于事,我的搜索没有找到任何线索。 非常感谢您的提示。

1 个答案:

答案 0 :(得分:1)

根据https://askubuntu.com/questions/981663/python2-7-broken-by-weakref-import-error-please-help,您可能需要重新创建您的virtualenv。我怀疑这里的问题是您需要deactivate当前的env才能尝试创建新的env,并确保系统python2解释器是运行virtualenv命令的那个。