首页 > 解决方案 > 更新后损坏的python虚拟环境

问题描述

今年冬天,我一直在按照本教程开发 Flask 应用程序。今天,因此在 3 个月后,我决定再次对其进行处理,但我的所有设置似乎都已损坏。应用程序开始只是激活 virtualenv 并调用flask run. 至于今天,命令返回:

No module named 'flask'

等等 for等等pippip3即使所有这些模块都在venv/bin. 我唯一的猜测是,从那时起,我将系统范围内的 python 更新为 Python 3.8.3rc1,它也不知何故成为了pythonvenv 中的默认值,即使我在python 3.7. 如果是这样的话,我不知道如何解决这个问题。你有什么建议吗?谢谢

标签: pythonvirtualenv

解决方案


When you created your virtual environment (let's call it v), two things happened:

  1. v/bin/python was a link to your system Python 3.7
  2. v/lib/python3.7 was created for packages installed to the virtual environment.

When you updated, the v link remained the same, but now it points to Python 3.8, which will look for a library directory named python3.8. Hence, your old Flask installation is invisible to the new Python.

Ideally, you wouldn't use your system Python for anything; install your own Python (under /usr/local/ or something), so that instead of upgrading to a new version of PYthon, you can simply install a new version along side it. Then your virtual environment can continue using whatever version of Python it was created with.

That said, you should probably just recreate your virtual environment from scratch.


推荐阅读