简体   繁体   English

PyCharm 虚拟环境和 Anaconda 环境有什么区别?

[英]What is the difference between PyCharm Virtual Environment and Anaconda Environment?

When I create a new project in PyCharm, it creates a new Virtual Environment.当我在 PyCharm 中创建一个新项目时,它会创建一个新的虚拟环境。 I have read that when I execute Python scripts, they are executed using the interpreter in this environment instead of System Environment.我读过,当我执行 Python 脚本时,它们是在此环境中使用解释器而不是系统环境来执行的。 So, if I need to install some packages, I can install them in only this environment and not in the system environment.所以,如果我需要安装一些包,我只能在这个环境中而不是在系统环境中安装它们。 That's cool.这很酷。

I have also read about Anaconda Environment.我还阅读了有关 Anaconda Environment 的信息。 When I create a new Anaconda environment, it creates a new one apart from system env.当我创建一个新的 Anaconda 环境时,它会创建一个除系统环境之外的新环境。 For my projects, I can use this environment and install only the required packages here and not in the main system environment.对于我的项目,我可以使用这个环境并且只在此处安装所需的包,而不是在主系统环境中。

Now, my question is what is the difference between virtual environment created by PyCharm and the environment created by Anaconda?现在,我的问题是 PyCharm 创建的虚拟环境和 Anaconda 创建的环境有什么区别? The virtual env created by PyCharm is around 15-20MB while that of Anaconda is 90MB. PyCharm 创建的虚拟环境大约为 15-20MB,而 Anaconda 为 90MB。 So, there must be a difference.所以,一定是有区别的。 Also, I have read that I can configure my PyCharm to use the Anaconda Environment interpreter.另外,我已经读到我可以配置我的 PyCharm 以使用 Anaconda Environment 解释器。

So, what is the difference between the environments created by PyCharm and Anaconda?那么,PyCharm 和 Anaconda 创建的环境有什么区别呢?

I have to clarify that anaconda is just a collection.我必须澄清一下, anaconda只是一个集合。 The real environment manager is conda .真正的环境管理器是conda Here is miniconda .miniconda It just contains the necessary parts to manage the environment instead of a full anaconda collection.它只包含管理环境的必要部分,而不是完整的anaconda集合。

conda is beyond a simple Python packages manager but is a system-wide package manager. conda不仅仅是一个简单的 Python 包管理器,而是一个系统范围的包管理器。 It will help you to install packages without pain.它将帮助您轻松安装软件包。 A classic example is installing numpy on Windows.一个经典的例子是在 Windows 上安装numpy Without conda , it is really difficult as it needs a specific C compiler which is difficult to obtain.没有conda ,真的很困难,因为它需要一个很难获得的特定 C 编译器。 But with conda , you can install numpy with just one command conda install numpy .但随着conda ,您可以安装numpy只用一个命令conda install numpy It will automatically solve compiler problem and C dependencies.它将自动解决编译器问题和 C 依赖项。


So back to your question, when you create an env in Pycharm, it will ask you which env do you want to create: Virtualenv Environment , Conda Environment , or Pipenv Environment .回到您的问题,当您在 Pycharm 中创建 env 时,它会询问您要创建哪个 env: Virtualenv EnvironmentConda EnvironmentPipenv Environment As for me, I usually choose Pipenv Environment as this env will be bound to the current project and can generate a lock file.至于我,我通常选择Pipenv Environment因为这个 env 会绑定到当前项目并且可以生成一个锁定文件。

In this case, I think you can understand it now: There isn't an env named "created by PyCharm" or "Anaconda".在这种情况下,我想您现在可以理解了:没有名为“由 PyCharm 创建”或“Anaconda”的环境。 There are only envs named "created by Virtualenv, Conda or Pipenv".只有名为“由 Virtualenv、Conda 或 Pipenv 创建”的环境。 And Pycharm just uses and wraps one of them.而 Pycharm 只是使用并包装了其中之一。


So what is the difference between Conda Environment and Virtualenv Environment ( Pipenv Environment essentially is a Virtualenv Environment with sophisticated pip )?那么Conda EnvironmentVirtualenv Environment ( Pipenv Environment本质上是一个带有复杂pipVirtualenv Environment ) 之间有什么区别? The difference comes from their different purposes.不同之处在于他们不同的目的。

Conda Environment is usually for "Python user". Conda Environment通常用于“Python 用户”。 They use Python as a tool to do some other works such as web crawling, data mining, and image processing.他们使用 Python 作为工具来完成一些其他工作,例如网络爬虫、数据挖掘和图像处理。 They don't know much about Python(as they don't need to know) so conda is as automatical as possible.他们对 Python 了解不多(因为他们不需要知道)所以conda尽可能是自动的。 And their tasks can be anywhere in the computer so the envs created by conda are located in user-wide directories.而他们的任务可以在电脑中任何地方,通过创建ENVS conda位于用户级目录。 And they sometimes need different Python versions, this can be done in conda but not virtualenv .他们有时需要不同的 Python 版本,这可以在conda完成,但不能在virtualenv

Virtualenv Environment is usually for "Python developer". Virtualenv Environment通常用于“Python 开发人员”。 They use Python to build applications or packages.他们使用 Python 来构建应用程序或包。 The envs created by Virtualenv are usually located in the current project's directory. Virtualenv 创建的Virtualenv通常位于当前项目的目录中。 So you can create an env for every application and manage dependencies easily.因此,您可以为每个应用程序创建一个 env 并轻松管理依赖项。

To sum up:总结:

Conda Environment : Conda Environment

  1. Manage not only Python packages but also different Python versions and system-wide dependencies.不仅管理 Python 包,还管理不同的 Python 版本和系统范围的依赖项。
  2. Envs are located in user-wide directories.环境位于用户范围的目录中。
  3. Fewer envs.更少的环境。

Virtualenv Environment : Virtualenv Environment

  1. Manage Python packages.管理 Python 包。 The main purpose is to separate dependencies for every application.主要目的是为每个应用程序分离依赖项。
  2. Envs are usually located in project-wide directories.环境通常位于项目范围的目录中。 (Although pipenv creates env in user-wide directories by default, many people think in project directories should be the default.) (虽然pipenv默认在用户范围的目录中创建env,但很多人认为在项目目录中应该是默认的。)
  3. Much more envs.(A new env for every application)更多环境。(每个应用程序的新环境)

For me, I use both of them.对我来说,我同时使用它们。 I use conda to manage different Python versions and use pipenv to manage dependencies for my applications.我使用conda来管理不同的 Python 版本,并使用pipenv来管理我的应用程序的依赖项。

Both environments are based on python's virtualenv , you can use them independently and configure (or install) packages inside it as you need, without the worry of conflicts.两种环境都基于 python 的virtualenv ,您可以独立使用它们并根据需要在其中配置(或安装)包,而无需担心冲突。 This is the essence of virtualenv.这就是 virtualenv 的本质。

Anaconda is a python distribution (just like linux distros) it by default add other packages based of it's opinion of what developers need. Anaconda是一个 python 发行版(就像 linux 发行版),默认情况下,它会根据开发人员需要的意见添加其他软件包。 Hence, the installation is larger than if you install plain vanilla python.因此,安装比安装普通的 vanilla python 更大。 This is also why it's virtual environment is quite large.这也是它的虚拟环境相当大的原因。

Pycharm is an IDE, which happens to support the virtualenv feature of python. Pycharm是一个IDE,正好支持python 的virtualenv 特性。 So it can create it for you, if you wish.因此,如果您愿意,它可以为您创建它。 It can use plain python distro to create it, so this will have a smaller size than if it uses a distro like Anaconda, as you have noticed.它可以使用普通的 python 发行版来创建它,因此与使用像 Anaconda 这样的发行版相比,它的大小会更小,正如您所注意到的。

The size issue is not specific to Anaconda, if you list all packages installed for you by anaconda conda list and install it manually yourself in the "lightweight" virtualenv you will see the size go up too.大小问题不是 Anaconda 特有的,如果您列出 anaconda conda list为您安装的所有软件包,并在“轻量级”virtualenv 中手动安装它,您将看到大小也会增加。 I believe you get my point.我相信你明白我的意思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM