升级Ubuntu17.04至17.10后PyCharm运行Django项目报_remove_dead_weakref导入错误
Hey there, let's sort out this frustrating error you're hitting after upgrading Ubuntu 17.04 to 17.10 and trying to run your Django project in PyCharm.
What's causing this?
This error almost always boils down to a corrupted Python virtual environment. When you upgrade your Ubuntu system, it often updates system-level Python components. Since virtual environments rely on links to some system Python files, these updates can break the dependencies in your existing venv—leading to weird import errors like the one you're seeing with _remove_dead_weakref from _weakref.
Step-by-Step Fix
Here's how to get your project back up and running:
Backup your existing virtual environment (optional but safe)
Make a copy of your venv folder just in case something goes wrong:cp -r /home/encuentrum/venv-encuentrum3 /home/encuentrum/venv-encuentrum3_backupDelete the corrupted virtual environment
Get rid of the broken venv:rm -rf /home/encuentrum/venv-encuentrum3Create a fresh virtual environment
Recreate the venv using Python 3.6 (matching your original setup):python3.6 -m venv /home/encuentrum/venv-encuentrum3Activate the new venv and reinstall dependencies
- Activate the environment:
source /home/encuentrum/venv-encuentrum3/bin/activate - If you have a
requirements.txtfile for your project, install all dependencies at once:pip install -r /home/encuentrum/GitLab/encuentrum3/ENCUENTRUM/requirements.txt - If you don't have a requirements file, manually install Django and any other packages your project needs (e.g.,
pip install django).
- Activate the environment:
Update PyCharm's interpreter configuration
Open PyCharm and navigate to:File > Settings > Project: ENCUENTRUM > Python Interpreter
- Click the gear icon → Add
- Select "Existing environment" and browse to the new venv's Python executable:
/home/encuentrum/venv-encuentrum3/bin/python - Save the configuration.
Test running your Django server
Try launching the server again in PyCharm—this should resolve the import error.
内容的提问来源于stack exchange,提问作者Miguel Halanoca Ramos

