“python setup.py install”和“pip install”之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15724093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:48:00  来源:igfitidea点击:

Difference between 'python setup.py install' and 'pip install'

pythonvirtualenvpipsetup.py

提问by user2125465

I have an external package I want to install into my python virtualenv from a tar file. What is the best way to install the package?

我有一个外部包,我想从 tar 文件安装到我的 python virtualenv 中。安装软件包的最佳方法是什么?

I've discovered 2 ways that can do it:

我发现了两种方法可以做到:

  1. Extract the tar file, then run python setup.py installinside of the extracted directory.
  2. pip install packagename.tar.gzfrom example # 7 in https://pip.pypa.io/en/stable/reference/pip_install/#examples
  1. 解压缩 tar 文件,然后python setup.py install在解压缩的目录中运行。
  2. pip install packagename.tar.gz来自https://pip.pypa.io/en/stable/reference/pip_install/#examples 中的示例 #7

Is if there is any difference doing them in these 2 ways.

以这两种方式进行操作是否有任何区别。

采纳答案by Michael0x2a

On the surface, both do the same thing: doing either python setup.py installor pip install <PACKAGE-NAME>will install your python package for you, with a minimum amount of fuss.

从表面上看,两者都做同样的事情:做python setup.py installpip install <PACKAGE-NAME>将为你安装你的 python 包,最小量的大惊小怪。

However, using pip offers some additional advantages that make it much nicer to use.

但是,使用 pip 提供了一些额外的优势,使其更好用。

  • pip will automatically download all dependencies for a package for you. In contrast, if you use setup.py, you often have to manually search out and download dependencies, which is tedious and can become frustrating.
  • pip keeps track of various metadata that lets you easily uninstall and update packages with a single command: pip uninstall <PACKAGE-NAME>and pip install --upgrade <PACKAGE-NAME>. In contrast, if you install a package using setup.py, you have to manually delete and maintain a package by hand if you want to get rid of it, which could be potentially error-prone.
  • You no longer have to manually download your files. If you use setup.py, you have to visit the library's website, figure out where to download it, extract the file, run setup.py... In contrast, pip will automatically search the Python Package Index(PyPi) to see if the package exists there, and will automatically download, extract, and install the package for you. With a few exceptions, almost every single genuinely useful Python library can be found on PyPi.
  • pip will let you easily installwheels, which is the new standard of Python distribution. More info about wheels.
  • pip offers additional benefits that integrate well with using virtualenv, which is a program that lets you run multiple projects that require conflicting libraries and Python versions on your computer. More info.
  • pip is bundled by default with Python as of Python 2.7.9 on the Python 2.x series, and as of Python 3.4.0 on the Python 3.x series, making it even easier to use.
  • pip 会自动为你下载一个包的所有依赖项。相比之下,如果使用setup.py,则经常需要手动搜索和下载依赖项,这既乏味又令人沮丧。
  • pip 跟踪各种元数据,让您可以使用单个命令轻松卸载和更新包:pip uninstall <PACKAGE-NAME>pip install --upgrade <PACKAGE-NAME>. 相比之下,如果您使用 安装包setup.py,如果您想删除它,则必须手动删除和维护包,这可能容易出错。
  • 您不再需要手动下载文件。如果你使用setup.py,你必须访问库的网站,找出下载它的位置,解压缩文件,运行setup.py......相反,pip会自动搜索Python包索引(PyPi)以查看该包是否存在,并且将自动为您下载、解压缩和安装软件包。除了少数例外,几乎每个真正有用的 Python 库都可以在 PyPi 上找到。
  • pip 可以让你轻松安装轮子,这是 Python 发行版的新标准。有关车轮的更多信息
  • pip 提供了与 using 很好地集成的额外好处virtualenv,这是一个程序,可让您在计算机上运行需要冲突库和 Python 版本的多个项目。更多信息
  • 默认情况下,从 Python 2.7.9 开始,Python 2.x 系列和 Python 3.x 系列的 Python 3.4.0 都将 pip 与 Python 捆绑在一起,使其更易于使用。

So basically, use pip. It only offers improvements over using python setup.py install.

所以基本上,使用pip。它仅提供对使用python setup.py install.



If you're using an older version of Python, can't upgrade, and don't have pip installed, you can find more information about installing pip at the following links:

如果您使用的是旧版本的 Python,无法升级,并且没有安装 pip,您可以在以下链接中找到有关安装 pip 的更多信息:

pip, by itself, doesn't really require a tutorial. 90% of the time, the only command you really need is pip install <PACKAGE-NAME>. That said, if you're interested in learning more about the details of what exactly you can do with pip, see:

pip 本身并不需要教程。90% 的情况下,您真正​​需要的唯一命令是pip install <PACKAGE-NAME>. 也就是说,如果您有兴趣了解有关 pip 究竟可以做什么的详细信息,请参阅:

It is also commonly recommended that you use pip and virtualenv together. If you're a beginner to Python, I personally think it'd be fine to start of with just using pip and install packages globally, but eventually I do think you should transition to using virtualenv as you tackle more serious projects.

通常还建议您将 pip 和 virtualenv 一起使用。如果您是 Python 的初学者,我个人认为开始时只使用 pip 并全局安装包是可以的,但最终我确实认为您应该在处理更严肃的项目时过渡到使用 virtualenv。

If you'd like to learn more about using pip and virtualenv together, see:

如果您想了解有关同时使用 pip 和 virtualenv 的更多信息,请参阅:

回答by éric Araujo

python setup.py installis the analog of make install: it's a limited way to compile and copy files to destination directories. This doesn't mean that it's the best way to really install software on your system.

python setup.py install与 make install 类似:它是一种将文件编译和复制到目标目录的有限方式。这并不意味着它是在您的系统上真正安装软件的最佳方式。

pipis a package manager, which can install, upgrade, list and uninstall packages, like familiar package managers including: dpkg, apt, yum, urpmi, portsetc. Under the hood, it will run python setup.py install, but with specific options to control how and where things end up installed.

pip是一个包管理器,它可以安装、升级、列出和卸载包,就像熟悉的包管理器,包括:dpkg, apt, yum, urpmi, ports等。在幕后,它会运行python setup.py install,但有特定的选项来控制最终安装的方式和位置。

In summary: use pip.

总结:使用pip.

回答by themefield

The question is about the preferred method to install a local tarballcontaining a python package, NOTabout the advantage of uploading package to an indexing service like PyPi.

问题是关于安装包含 python 包的本地 tarball 的首选方法,而不是关于将包上传到像 PyPi 这样的索引服务的优势。

As lest I know some software distributor does not upload their package to PyPi, instead asking developers to download package from their website and install.

以免我知道一些软件分销商不会将他们的软件包上传到 PyPi,而是要求开发人员从他们的网站下载软件包并安装。

python setup.py install

python setup.py 安装

This can work but not recommended. It's not necessary to unwrap the tarball file and go into it to run setup.py file.

这可以工作,但不推荐。没有必要解开 tarball 文件并进入它来运行 setup.py 文件。

pip install ../path/to/packagename.tar.gz

pip install ../path/to/packagename.tar.gz

This is the way designed and preferred. Concise and align with PyPi-style packages.

这是设计和首选的方式。简洁并与 PyPi 风格的包保持一致。

More information about pip installcan be found here: https://pip.readthedocs.io/en/stable/reference/pip_install/

有关更多信息,请pip install访问:https: //pip.readthedocs.io/en/stable/reference/pip_install/