简体   繁体   English

python:pythonbrew和virtualenv有什么区别?

[英]python: what's the difference between pythonbrew and virtualenv?

I am new to python and I am planning to learn django. 我是python的新手,我打算学习django。 I had a bit of experience with ruby (not rails) and I am familiar with RVM however I don't understand the difference between pythonbrew and virtualenv . 我有一些ruby(而不是rails)的经验,我熟悉RVM但是我不明白pythonbrewvirtualenv之间的区别。 I know pythonbrew is a mimic of RVM but I thought virtualenv is already doing what RVM does (or vice versa that pythonbrew is already doing what RVM does). 我知道pythonbrewRVM的模仿,但我认为virtualenv已经在做RVM所做的事情(反之亦然, pythonbrew已经在做RVM的工作)。 Can someone please explain and perhaps provide some concrete examples/usages to help me understand it. 有人可以解释一下,或许可以提供一些具体的例子/用法来帮助我理解它。 Thanks very much! 非常感谢!

Pythonbrew is akin to Ruby's rvm : It's a shell function that allows you to: Pythonbrew类似于Ruby的rvm :它是一个shell函数,允许您:

  • Build one or more complete self-contained versions of Python, each stored locally under your home directory. 构建一个或多个完整的自包含Python版本,每个版本都存储在您的主目录下。 You can build multiple versions of Python this way. 您可以通过这种方式构建多个版本的Python。
  • Switch between the versions of Python easily. 轻松切换Python版本。

The Pythons you build are completely isolated from each other, and from whatever version(s) of Python are installed system-wide. 您构建的Pythons彼此完全隔离,并且从系统范围内安装的Python版本完全隔离。

Virtualenv is similar, but not quite the same. Virtualenv类似,但不完全相同。 It creates a Python virtual environment that, conceptually, sits on top of some existing Python installation (usually the system-wide one, but not always). 它创建了一个Python虚拟环境,从概念上讲,它位于一些现有的Python安装之上(通常是系统范围的安装,但并非总是如此)。 By default, on Unix platforms (and the Mac), it creates symbolic links to the various Python library modules, so you're literally sharing those modules with the "real" underlying Python implementation. 默认情况下,在Unix平台(和Mac)上,它会创建指向各种Python库模块的符号链接,因此您可以使用“真正的”底层Python实现来共享这些模块。 But, virtualenv has its own "bin" directory and "site-packages" directory. 但是,virtualenv有自己的“bin”目录和“site-packages”目录。 Anything extra you install in the Python virtual environment is only available within that environment. 您在Python虚拟环境中安装的任何额外内容仅在该环境中可用。

One advantage to Pythonbrew is that the Python environments it creates are truly, and completely, self-contained. Pythonbrew的一个优点是它创建的Python环境是真正的,完全是自包含的。 They cannot be contaminated by anything that gets screwed up in an underlying base Python install, because there isn't an underlying base install. 它们不会被任何在基础Python安装中搞砸的东西污染,因为没有底层的基础安装。 This is not true of virtualenv environments. 对于virtualenv环境而言,情况并非如此。 If you create a virtualenv Python, and then you somehow screw up the base Python instance it sits above (eg, accidentally deleting part of the base Python's "site" directory while logged in as root), you'll screw up any virtualenv environment based on that Python, too. 如果你创建了一个virtualenv Python,然后你以某种方式搞砸了它所在的基础Python实例(例如,在以root用户身份登录时意外删除了基本Python的“站点”目录的一部分),你将搞砸任何virtualenv环境在那个Python上也是如此。

However, virtualenv has its own advantages. 然而,virtualenv有其自身的优势。 Probably the biggest advantage is that it is lightweight. 可能最大的优点是它很轻巧。 Since Pythonbrew compiles Python from scratch, to create one of its environments, creating a Pythonbrew Python environment takes some time. 由于Pythonbrew从头开始编译Python,要创建一个环境,创建Pythonbrew Python环境需要一些时间。 By comparison, creating a virtualenv Python environment is really fast. 相比之下,创建virtualenv Python环境非常快。

You can, in fact, use them together. 事实上,你可以一起使用它们。 Here's one situation where you might want to do that. 这是您可能想要这样做的一种情况。

  • Your base system uses Python 2.6. 您的基本系统使用Python 2.6。
  • You need to install Python 2.7. 您需要安装Python 2.7。
  • For whatever reason, you can't (or don't want to) install Python 2.7 system wide, side-by-side with Python 2.6. 无论出于何种原因,您都不能(或者不想)在Python 2.6系统范围内安装Python 2.7。

In such a case, you could use Pythonbrew to install a base Python 2.7 under your home directory , where it doesn't conflict with anything installed elsewhere. 在这种情况下,您可以使用Pythonbrew 在您的主目录下安装基本Python 2.7,它不会与其他地方安装的任何内容冲突。 Then, you can create one or more lightweight virtualenv Python environments that are based on your Pythonbrew-installed 2.7 Python. 然后,您可以创建一个或多个基于Pythonbrew安装的2.7 Python的轻量级virtualenv Python环境。 For instance, you could use virtualenv to spin up short-lived test environments for Python 2.7 that way. 例如,您可以使用virtualenv以这种方式为Python 2.7启动短期测试环境。

I doubt most people actually do that. 我怀疑大多数人实际上是这样做的。 (I don't.) But there's no reason you can't. (我没有。)但是没有理由你不能。

For what its worth I've never heard of PythonBrew before, but I know (and love) virtualenv. 我以前从未听说过PythonBrew的价值,但我知道(和爱)virtualenv。

Virtualenv is used to create separate environments, based on the python install you have on your machine. Virtualenv用于创建单独的环境, 基于您在计算机上安装的python。 That is, if I have python 2.7 I can create a number of isolated python 2.7 environments, but I can't create python2.6 environments. 也就是说,如果我有python 2.7,我可以创建一些孤立的python 2.7环境,但是我无法创建python2.6环境。

According to this (which I found via google) Pythonbrew seems to be focussed on installing other python versions. 根据这个 (我通过谷歌发现)Pythonbrew似乎专注于安装其他python版本。 So I guess you would use 'brew to install py2.6 and 2.7 and then virtualenv to create environments for each. 所以我猜你会用'brew来安装py2.6和2.7然后使用virtualenv为每个创建环境。

Or, it seems, 'brew can create the environments too, using virtualenv. 或者,似乎'brew也可以创建环境,使用virtualenv。

Why a different python interpreter is not really an isolated environment. 为什么不同的python解释器实际上不是一个孤立的环境。

Each python installation has a set of packages (placed in 'site-packages' I think). 每个python安装都有一组包(我认为放在'site-packages'中)。 If you install a new package it is added to this set, and available for all your python code. 如果你安装了一个新包,它会被添加到这个集合中,并且可用于你所有的python代码。

This can be a problem if you have one project that you build on Django0.96 and you want to start a new project using Django1.3. 如果您在Django0.96上构建了一个项目并且想要使用Django1.3启动一个新项目,那么这可能是一个问题。 If you just update your system version of Django that would affect you old project too. 如果您只是更新Django的系统版本,那也会影响您的旧项目。

With virtualenvs you could create one environment with Django1.3 and another with Django0.96, both being python2.7. 使用virtualenvs,您可以使用Django1.3创建一个环境,使用Django0.96创建另一个环境,两者都是python2.7。 If you were OK with running your old project in python2.6 and the new one in python2.7 you could do that too, but what about your next two projects using diffenret versions from Django-Trunk then? 如果您可以在python2.6中运行旧项目,在python2.7中运行新项目,那么您也可以这样做,但是接下来的两个项目使用Django-Trunk的diffenret版本呢?

Python brew is for building and install, maybe like some buildbot. Python brew用于构建和安装,可能像一些buildbot。 I'm not so familiar. 我不太熟悉。 Virtualenv is mainly for, when you got different version of python, or you wan to try some package without disturbing on-system version. Virtualenv主要用于,当你有不同版本的python,或者你想尝试一些包而不会打扰系统版本。


Ok, this revels something 好吧,这有点令人厌恶

Create isolated python environments (uses virtualenv ): 创建孤立的python环境(使用virtualenv ):

pythonbrew venv init
pythonbrew venv create proj
pythonbrew venv list
pythonbrew venv use proj
pythonbrew venv delete proj

From http://pypi.python.org/pypi/pythonbrew/ 来自http://pypi.python.org/pypi/pythonbrew/

Since all the answers above are pretty old, I'd like to summarize my findings here. 由于上述所有答案都很陈旧,我想在此总结一下我的发现。 I was trying to figure out how this works with Python after coming from rvm/ruby and could not find a clear explanation anywhere online. 来自rvm / ruby​​之后我试图弄清楚它是如何与Python一起工作的,并且无法在网上找到明确的解释。

So we have the following options on Macos: 所以我们在Macos上有以下选项:

Homebrew (MacOS only) Homebrew(仅限MacOS)

...Can install python and python3 . ...可以安装pythonpython3 They will be stored in Homebrew's Cellar and symlinked from /usr/local/bin . 它们将存储在Homebrew的Cellar中,并通过/usr/local/bin符号链接。 Default python installed using brew is 2.7.6 as of now. 截至目前,使用brew安装的默认python是2.7.6。

Packages installed using pip will go in default location (you also have pip and pip3 symlinked as well). 使用pip安装的软件包将进入默认位置(同样也有pippip3链接)。

Pyenv (successor of Pythonbrew) Pyenv(Pythonbrew的继承者)

...Is an alternative to Homebrew (on Macos) way to install and maintain multiple versions of Python. ...可以替代Homebrew(在Macos上)安装和维护多个版本的Python。 Linux does not have Homebrew so Pyenv is kind of a specialized version of it just for Python. Linux没有Homebrew,所以Pyenv只是Python的专用版本。 It also builds Python from source. 它还从源代码构建Python。

Pyenv keeps python installations in ~/.pyenv/versions/ and allows to quickly switch between and use same names for binaries ( python , pip etc). Pyenv将python安装保存在~/.pyenv/versions/并允许在二进制文件( pythonpip等)之间快速切换和使用相同的名称。 It uses "shim" binaries which are fake binaries like python , pip etc which mimic Python's and instead just silently redirect execution to the currently active version. 它使用“shim”二进制文件,它们是伪造的二进制文件,如pythonpip等,它们模仿Python,而只是默默地将执行重定向到当前活动版本。

Packages installed using pip will go into active Python installation. 使用pip安装的软件包将进入活动的Python安装。

So, neither of those methods really enough to maintain separate python installations and package version sets (like rvm does with gemsets) per project. 因此,这些方法都不足以维护每个项目的单独python安装包版本集(如rvm对gemsets一样)。 Hence: 因此:

Virtualenv VIRTUALENV

...Is the closest thing to rvm. ...是最接近rvm的东西。 To quote this post : 引用这篇文章

it sets up a clean copy of Python in a new directory by copying or linking files from your primary Python installation to create new bin and lib directories 它通过复制或链接主Python安装中的文件来创建新的bin和lib目录,从而在新目录中设置Python的干净副本

So it uses the currently active copy of Python and copies it into a separate directory. 因此它使用当前活动的Python副本并将其复制到一个单独的目录中。 virtualenvwrapper adds functionality to manage those environments and automatically activate them using cd just like rvm does. virtualenvwrapper添加了管理这些环境的功能,并像使用rvm一样使用cd自动激活它们。

This allows to isolate python version and libraries installed used for each project. 这允许隔离为每个项目安装的python版本和库。 However, it does not install python versions itself. 但是,它本身并不安装python版本。

Thus, sounds like most people use a combination of pyenv + virtualenv or brew + virtualenv (brew is Macos specific of course). 因此,听起来像大多数人使用pyenv + virtualenvbrew + virtualenv (brew当然是具体的Macos)。 The first part is used to install python versions (if needed) and the second one is to clone them for different projects and switch between them. 第一部分用于安装python版本(如果需要),第二部分是为不同的项目克隆它们并在它们之间切换。

PS: I am just starting to figure it out, please correct me if anything here is wrong. PS:我刚刚开始明白这个问题,如果有任何错误,请纠正我。

PPS: It seems to me that this whole business can be improved by combining pyenv and virtualenv under one roof... PPS:在我看来,通过将pyenv和virtualenv结合在一起,可以改善整个业务......

"pythonbrew is a program to automate the building and installation 
 of Python in the users $HOME."

By contrast, virtualenv provides an isolated environment for developing a project - it keeps all of the libraries for that project in one place, and it makes it much easier to relocate (and so deploy) the project. 相比之下,virtualenv为开发项目提供了一个独立的环境 - 它将该项目的所有库保存在一个地方,这使得重新定位(以及部署)项目变得更加容易。

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

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