为什么我们需要 Django 中的虚拟环境?

Why do we need Virtual Environment in Django?

我是 Django 的新手,我想知道为什么建议设置虚拟环境。 我问了 google,发现 Python 提供了一个工具 virtualenv 来创建一个 isolated Python 环境.

不过,我还是不明白隔离环境是什么意思。 好吧,简而言之,我的问题是我不明白为什么我们需要简单的虚拟环境。

您可以在 PEP 405 中看到关于 python venv 的信息:https://www.python.org/dev/peps/pep-0405/

还有https://realpython.com/python-virtual-environments-a-primer/#why-the-need-for-virtual-environments中提到的一些原因:

Consider the following scenario where you have two projects: ProjectA and ProjectB, both of which have a dependency on the same library, ProjectC. The problem becomes apparent when we start requiring different versions of ProjectC. Maybe ProjectA needs v1.0.0, while ProjectB requires the newer v2.0.0, for example. This is a real problem for Python since it can’t differentiate between versions in the site-packages directory. So both v1.0.0 and v2.0.0 would reside in the same directory with the same name: /System/Library/Frameworks/Python.framework/Versions/3.5/Extras/lib/python/ProjectC Since projects are stored according to just their name, there is no differentiation between versions. Thus, both projects, ProjectA and ProjectB, would be required to use the same version, which is unacceptable in many cases.