官方的“首选”是什么?在系统范围内安装 pip 和 virtualenv 的方法?
人们似乎最常推荐的是这个:
$ sudo apt-get install python-setuptools
$ sudo easy_install pip
$ sudo pip install virtualenv
还是我从 http://www 获得的这个.pip-installer.org/en/latest/installing.html:
$ curl -O https://github.com/pypa/virtualenv/raw/master/virtualenv.py
$ python virtualenv.py my_new_env
$ . my_new_env/bin/activate
(my_new_env)$ pip install ...
或者完全不同的东西?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
如果您可以安装最新的 Python(2.7.9 及更高版本),Pip 现在已与其捆绑在一起。
请参阅: https://docs.python.org/2.7//installing/index.html
如果没有:
更新(来自发行说明):
我现在运行常规:
以下是官方安装说明:
http://pip.readthedocs.org/en/latest/installing.html #install-pip
编辑 2013 年 7 月 25 日:
更改了 setuptools 安装的 URL。
编辑 2014 年 2 月 10 日:
删除了 setuptools 安装(感谢 @Ciantic)
编辑 2014 年 6 月 26 日:
再次更新了网址(感谢 @LarsH)
编辑 2015 年 3 月 1 日:
Pip 现在与 Python 捆绑在一起
If you can install the latest Python (2.7.9 and up) Pip is now bundled with it.
See: https://docs.python.org/2.7//installing/index.html
If not :
Update (from the release notes):
I now run the regular:
Here are the official installation instructions:
http://pip.readthedocs.org/en/latest/installing.html#install-pip
EDIT 25-Jul-2013:
Changed URL for setuptools install.
EDIT 10-Feb-2014:
Removed setuptools install (thanks @Ciantic)
EDIT 26-Jun-2014:
Updated URL again (thanks @LarsH)
EDIT 1-Mar-2015:
Pip is now bundled with Python
http://www.pip-installer.org/en/latest/installing.html 确实是这个问题的规范答案。
具体来说,系统范围的指令是:
问题中引用的部分是 virtualenv 指令,而不是系统范围的指令。
easy_install
说明已经存在了很长时间,但没有必要再这样做了。http://www.pip-installer.org/en/latest/installing.html is really the canonical answer to this question.
Specifically, the systemwide instructions are:
The section quoted in the question is the
virtualenv
instructions rather than the systemwide ones. Theeasy_install
instructions have been around for longer, but it isn't necessary to do it that way any more.这个答案来自 Twitter 上的 @webology:
我添加的注释:
This answer comes from @webology on Twitter:
My added notes:
在 Ubuntu 12.04 上,我很幸运地使用了包管理器:
On Ubuntu 12.04 I've had good luck just using the package manager:
没有首选方法 - 一切都取决于您的需求。通常,出于某种原因,您需要在系统上拥有不同的 Python 解释器。在这种情况下,您需要为每个解释器单独安装这些东西。除此之外:我更喜欢自己安装东西,而不是依赖预先打包的东西,有时会导致问题 - 但这只是一种可能的选择。
There is no preferred method - everything depends on your needs. Often you need to have different Python interpreters on the system for whatever reason. In this case you need to install the stuff individually for each interpreter. Apart from that: I prefer installing stuff myself instead of depending of pre-packaged stuff sometimes causing issues - but that's only one possible opionion.
这个问题确实没有单一的“答案”,但肯定有一些有用的概念可以帮助您做出决定。
在您的用例中需要回答的第一个问题是“我想使用系统Python吗?”如果您想使用随操作系统一起分发的 Python,那么使用 apt-get install 方法可能就可以了。不过,根据操作系统的分发方法,您仍然需要问更多问题,例如“我想安装此软件包的多个版本吗?”如果答案是肯定的,那么使用 apt 这样的东西可能不是一个好主意。 Dpkg 几乎只会在文件系统的根目录下解压存档,因此包维护者需要确保包在很少的假设下安全安装。对于大多数 debian 软件包,我会假设(有人可以在这里随意纠正我)它们只是解压并提供顶级软件包。
例如,假设包是“virtualenv”,您最终会得到
/usr/lib/python2.x/site-packages/virtualenv
。如果您使用easy_install
安装它,您会得到类似/usr/lib/python2.x/site-packages/virtualenv.egg-link
的内容,它可能指向/usr/lib/python2.x/site-packages/virtualenv-1.2-2.x.egg
这可能是一个目录或压缩的egg。 Pip 做了类似的事情,尽管它不使用 Egg,而是将顶级包直接放在lib
目录中。我可能偏离了路径,但重点是每种方法都考虑到了不同的需求。这就是为什么像 virtualenv 这样的工具很有用,因为它们允许您对 Python 库进行沙箱处理,这样您就可以拥有所需的库和版本的任意组合。
setuptools 还允许将软件包安装为多版本,这意味着不会创建单一的 mo
dule_name.egg-link
。要导入这些包,您需要使用 pkg_resources 和 __import__ 函数。回到你最初的问题,如果你对系统 python 感到满意,并计划使用 virtualenv 和 pip 为不同的应用程序构建环境,那么使用 apt-get 在系统级别安装 virtualenv 和/或 pip 似乎是完全合适的。但需要注意的是,如果您计划升级 Python 发行版,如果您链接回系统站点包,可能会通过您的 virtualenvs 产生连锁反应。
我还应该提到的是,这些选项本质上并不比其他选项更好。他们只是采取不同的方法。使用系统版本是安装 Python 应用程序的绝佳方法,但使用 Python 进行开发可能是一种非常困难的方法。简单的安装和设置工具在没有 virtualenv 的世界中非常方便,但是如果您需要使用同一库的不同版本,那么它也会变得相当笨拙。 Pip 和 virtualenv 实际上更像是虚拟机。您无需小心地并排安装东西,只需创建一个全新的环境即可。这里的缺点是,30+ virtualenvs 之后你可能会用掉相当多的磁盘空间并弄乱你的文件系统。
正如您所看到的,由于有很多选项,很难说要使用哪种方法,但是通过对您的用例进行一些调查,您应该能够找到一种可行的方法。
There really isn't a single "answer" to this question, but there are definitely some helpful concepts that can help you to come to a decision.
The first question that needs to be answered in your use case is "Do I want to use the system Python?" If you want to use the Python distributed with your operating system, then using the apt-get install method may be just fine. Depending on the operating system distribution method though, you still have to ask some more questions, such as "Do I want to install multiple versions of this package?" If the answer is yes, then it is probably not a good idea to use something like apt. Dpkg pretty much will just untar an archive at the root of the filesystem, so it is up to the package maintainer to make sure the package installs safely under very little assumptions. In the case of most debian packages, I would assume (someone can feel free to correct me here) that they simply untar and provide a top level package.
For example, say the package is "virtualenv", you'd end up with
/usr/lib/python2.x/site-packages/virtualenv
. If you install it witheasy_install
you'd get something like/usr/lib/python2.x/site-packages/virtualenv.egg-link
that might point to/usr/lib/python2.x/site-packages/virtualenv-1.2-2.x.egg
which may be a directory or zipped egg. Pip does something similar although it doesn't use eggs and instead will place the top level package directly in thelib
directory.I might be off on the paths, but the point is that each method takes into account different needs. This is why tools like virtualenv are helpful as they allow you to sandbox your Python libraries such that you can have any combination you need of libraries and versions.
Setuptools also allows installing packages as multiversion which means there is not a singular mo
dule_name.egg-link
created. To import those packages you need to use pkg_resources and the__import__
function.Going back to your original question, if you are happy with the system python and plan on using virtualenv and pip to build environments for different applications, then installing virtualenv and / or pip at the system level using apt-get seems totally appropriate. One word of caution though is that if you plan on upgrading your distributions Python, that may have a ripple effect through your virtualenvs if you linked back to your system site packages.
I should also mention that none of these options is inherently better than the others. They simply take different approaches. Using the system version is an excellent way to install Python applications, yet it can be a very difficult way to develop with Python. Easy install and setuptools is very convenient in a world without virtualenv, but if you need to use different versions of the same library, then it also become rather unwieldy. Pip and virtualenv really act more like a virtual machine. Instead of taking care to install things side by side, you just create an whole new environment. The downside here is that 30+ virtualenvs later you might have used up quite bit of diskspace and cluttered up your filesystem.
As you can see, with the many options it is difficult to say which method to use, but with a little investigation into your use cases, you should be able to find a method that works.
执行此操作:
请参阅
Do this:
See
由于 virtualenvs 默认包含 pip,所以我几乎从不全局安装 pip。我所做的最终看起来更像是:
然后我根据自己的喜好继续安装和设置 virtualenvwrapper,然后就可以开始了。也可能值得一看 Jeremy Avnet 的 virtualenv-burrito:
https://github.com/brainsik /virtualenv-墨西哥卷饼
Since virtualenvs contain pip by default, I almost never install pip globally. What I do ends up looking more like:
I then proceed to install and set up virtualenvwrapper to my liking and off I go. it might also be worthwhile to take a look at Jeremy Avnet's virtualenv-burrito:
https://github.com/brainsik/virtualenv-burrito
@ericholscher 在 Twitter 上说:“官方文档中的那个……”
这是一个很好的观点,你应该按照文档所说的去做。
引自官方 pip 安装说明 http://www.pip-installer.org /en/latest/installing.html:
@ericholscher says on Twitter, "The one in the official docs.."
It's a great point, you should do what the docs say.
Quoted from the official pip installation instructions at http://www.pip-installer.org/en/latest/installing.html:
从发行版包开始,您可以使用:
它允许您创建 virtualenvs,或者
它允许您将任意包安装到您的主目录。
如果您习惯了 virtualenv,第一个命令将为您提供所需的一切(请记住,pip 已捆绑并将安装在您创建的任何 virtualenv 中)。
如果您只想安装软件包,第二个命令将为您提供所需的内容。像这样使用 pip:
放入类似的内容
并在
~/.bashrc
中。如果您的发行版很古老并且您根本不想使用它的软件包(可能除了 Python 本身),您可以下载 virtualenv,无论是作为 tarball 还是作为独立脚本:
如果您的发行版更前沿Python3.3 具有内置的类似 virtualenv 的功能:
运行速度更快,但不包括 setuptools 和 pip。
Starting from distro packages, you can either use:
which lets you create virtualenvs, or
which lets you install arbitrary packages to your home directory.
If you're used to virtualenv, the first command gives you everything you need (remember, pip is bundled and will be installed in any virtualenv you create).
If you just want to install packages, the second command gives you what you need. Use pip like this:
and put something like
in your
~/.bashrc
.If your distro is ancient and you don't want to use its packages at all (except for Python itself, probably), you can download virtualenv, either as a tarball or as a standalone script:
If your distro is more of the bleeding edge kind, Python3.3 has built-in virtualenv-like abilities:
This runs way faster, but setuptools and pip aren't included.
要在 mac (osx) 上安装 pip,以下代码对我来说非常有用:
To install pip on a mac (osx), the following one liner worked great for me:
在 Debian 上,最好的方法是
sudo apt-get install python-pip
On Debian the best way to do it would be
sudo apt-get install python-pip
在Raspbian中,甚至无需提及
python2.7
。事实上,如果 python 版本低于 2.7.9,这是安装 pip 的最佳方法。感谢@tal-weiss
In Raspbian, there is even no need to mention
python2.7
. Indeed this is best way to install pip if python version in less then 2.7.9.Thanks to @tal-weiss
https://github.com/pypa/pip/raw/master /contrib/get-pip.py 现在可能是正确的方法。
https://github.com/pypa/pip/raw/master/contrib/get-pip.py is probably the right way now.
我使用 get-pip 和 virtualenv-burrito 来安装所有这些。不确定是否需要 python-setuptools。
I use get-pip and virtualenv-burrito to install all this. Not sure if python-setuptools is required.
前一种方法很好。我能看到的唯一问题是你最终可能会得到旧版本的 setuptools(如果 apt 存储库没有保持最新状态..
The former method is fine. The only problem I can see is that you might end up with an old version of setuptools (if the apt repository hasn't been kept up-to-date..