官方的“首选”是什么?在系统范围内安装 pip 和 virtualenv 的方法?

发布于 2024-10-30 21:50:43 字数 492 浏览 4 评论 0 原文

人们似乎最常推荐的是这个:

$ 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 ...

或者完全不同的东西?

Is it this, which people seem to recommend most often:

$ sudo apt-get install python-setuptools
$ sudo easy_install pip
$ sudo pip install virtualenv

Or this, which I got from 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 ...

Or something entirely different?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

音栖息无 2024-11-06 21:50:43

如果您可以安装最新的 Python(2.7.9 及更高版本),Pip 现在已与其捆绑在一起。
请参阅: https://docs.python.org/2.7//installing/index.html
如果没有:
更新(来自发行说明):

从 v1.5.1 开始,pip 在运行 get-pip.py 之前不需要 setuptools。此外,如果尚未安装 setuptools(或分发),get-pip.py 将为您安装 setuptools。

我现在运行常规:

curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | sudo python

以下是官方安装说明:
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):

Beginning with v1.5.1, pip does not require setuptools prior to running get-pip.py. Additionally, if setuptools (or distribute) is not already installed, get-pip.py will install setuptools for you.

I now run the regular:

curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | sudo python

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

流年里的时光 2024-11-06 21:50:43

http://www.pip-installer.org/en/latest/installing.html 确实是这个问题的规范答案。

具体来说,系统范围的指令是:

$ curl -O http://python-distribute.org/distribute_setup.py
$ python distribute_setup.py
$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$ python get-pip.py

问题中引用的部分是 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:

$ curl -O http://python-distribute.org/distribute_setup.py
$ python distribute_setup.py
$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$ python get-pip.py

The section quoted in the question is the virtualenv instructions rather than the systemwide ones. The easy_install instructions have been around for longer, but it isn't necessary to do it that way any more.

万劫不复 2024-11-06 21:50:43

这个答案来自 Twitter 上的 @webology:

$ sudo apt-get install python-setuptools
$ sudo easy_install pip
$ sudo pip install --upgrade pip virtualenv virtualenvwrapper

我添加的注释:

  • 在 Mac/Windows(和 Linux,如果 apt 存储库已过时)上,您可以从 http://pypi.python.org/pypi/setuptools
  • 在 Windows 上,您必须在最后一步中省略 virtualenvwrapper 并以某种方式手动安装它。我不知道是否有办法在没有 Cygwin 的情况下做到这一点,但我希望如此。

This answer comes from @webology on Twitter:

$ sudo apt-get install python-setuptools
$ sudo easy_install pip
$ sudo pip install --upgrade pip virtualenv virtualenvwrapper

My added notes:

  • On Mac/Windows (and Linux if the apt repo is outdated) you'd replace the first step with downloading setuptools from http://pypi.python.org/pypi/setuptools
  • On Windows you'd have to omit virtualenvwrapper from the last step and install it manually somehow. I don't know if there's a way to do this without Cygwin, but I hope so.
指尖上的星空 2024-11-06 21:50:43

在 Ubuntu 12.04 上,我很幸运地使用了包管理器:

sudo apt-get install python-pip virtualenvwrapper

On Ubuntu 12.04 I've had good luck just using the package manager:

sudo apt-get install python-pip virtualenvwrapper
通知家属抬走 2024-11-06 21:50:43

没有首选方法 - 一切都取决于您的需求。通常,出于某种原因,您需要在系统上拥有不同的 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.

孤独难免 2024-11-06 21:50:43

这个问题确实没有单一的“答案”,但肯定有一些有用的概念可以帮助您做出决定。

在您的用例中需要回答的第一个问题是“我想使用系统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 还允许将软件包安装为多版本,这意味着不会创建单一的 module_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 with easy_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 the lib 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 module_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.

笑梦风尘 2024-11-06 21:50:43

执行此操作:

curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
python get-pip.py
pip install virtualenv

请参阅

Do this:

curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
python get-pip.py
pip install virtualenv

See

诠释孤独 2024-11-06 21:50:43

由于 virtualenvs 默认包含 pip,所以我几乎从不全局安装 pip。我所做的最终看起来更像是:

$ sudo apt-get install python-setuptools
$ curl -O http://python-distribute.org/distribute_setup.py
$ sudo python distribute_setup.py
$ sudo easy_install virtualenv

然后我根据自己的喜好继续安装和设置 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:

$ sudo apt-get install python-setuptools
$ curl -O http://python-distribute.org/distribute_setup.py
$ sudo python distribute_setup.py
$ sudo easy_install virtualenv

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

守不住的情 2024-11-06 21:50:43

@ericholscher 在 Twitter 上说:“官方文档中的那个……”

这是一个很好的观点,你应该按照文档所说的去做。

引自官方 pip 安装说明 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 ...

@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:

$ 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 ...

从发行版包开始,您可以使用:

sudo apt-get install python-virtualenv

它允许您创建 virtualenvs,或者

sudo apt-get install python{,3}-pip

它允许您将任意包安装到您的主目录。

如果您习惯了 virtualenv,第一个命令将为您提供所需的一切(请记住,pip 已捆绑并将安装在您创建的任何 virtualenv 中)。

如果您只想安装软件包,第二个命令将为您提供所需的内容。像这样使用 pip:

pip install --user something

放入类似的内容

PATH=~/.local/bin:$PATH

并在 ~/.bashrc


。如果您的发行版很古老并且您根本不想使用它的软件包(可能除了 Python 本身),您可以下载 virtualenv,无论是作为 tarball 还是作为独立脚本:

wget -O ~/bin/virtualenv https://raw.github.com/pypa/virtualenv/master/virtualenv.py
chmod +x ~/bin/virtualenv

如果您的发行版更前沿Python3.3 具有内置的类似 virtualenv 的功能:

python3 -m venv ./venv

运行速度更快,但不包括 setuptools 和 pip。

Starting from distro packages, you can either use:

sudo apt-get install python-virtualenv

which lets you create virtualenvs, or

sudo apt-get install python{,3}-pip

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:

pip install --user something

and put something like

PATH=~/.local/bin:$PATH

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:

wget -O ~/bin/virtualenv https://raw.github.com/pypa/virtualenv/master/virtualenv.py
chmod +x ~/bin/virtualenv

If your distro is more of the bleeding edge kind, Python3.3 has built-in virtualenv-like abilities:

python3 -m venv ./venv

This runs way faster, but setuptools and pip aren't included.

遗忘曾经 2024-11-06 21:50:43

要在 mac (osx) 上安装 pip,以下代码对我来说非常有用:

sudo easy_install pip

To install pip on a mac (osx), the following one liner worked great for me:

sudo easy_install pip
演出会有结束 2024-11-06 21:50:43

在 Debian 上,最好的方法是

sudo apt-get install python-pip

On Debian the best way to do it would be

sudo apt-get install python-pip

一笑百媚生 2024-11-06 21:50:43

在Raspbian中,甚至无需提及python2.7。事实上,如果 python 版本低于 2.7.9,这是安装 pip 的最佳方法。

curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | sudo python

感谢@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.

curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | sudo python

Thanks to @tal-weiss

小情绪 2024-11-06 21:50:43

我使用 get-pip 和 virtualenv-burrito 来安装所有这些。不确定是否需要 python-setuptools。

# might be optional. I install as part of my standard ubuntu setup script
sudo apt-get -y install python-setuptools

# install pip (using get-pip.py from pip contrib)
curl -O https://raw.github.com/pypa/pip/develop/contrib/get-pip.py && sudo python get-pip.py

# one-line virtualenv and virtualenvwrapper using virtualenv-burrito
curl -s https://raw.github.com/brainsik/virtualenv-burrito/master/virtualenv-burrito.sh | bash

I use get-pip and virtualenv-burrito to install all this. Not sure if python-setuptools is required.

# might be optional. I install as part of my standard ubuntu setup script
sudo apt-get -y install python-setuptools

# install pip (using get-pip.py from pip contrib)
curl -O https://raw.github.com/pypa/pip/develop/contrib/get-pip.py && sudo python get-pip.py

# one-line virtualenv and virtualenvwrapper using virtualenv-burrito
curl -s https://raw.github.com/brainsik/virtualenv-burrito/master/virtualenv-burrito.sh | bash
甲如呢乙后呢 2024-11-06 21:50:43

前一种方法很好。我能看到的唯一问题是你最终可能会得到旧版本的 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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文