问题:’python setup.py install’和’pip install’之间的区别

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

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

  1. 提取tar文件,然后python setup.py install在提取的目录中运行。
  2. pip install packagename.tar.gz来自https://pip.pypa.io/en/stable/reference/pip_install/#examples中的示例7

这两种方式在做上是否有区别。

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?

I’ve discovered 2 ways that can do it:

  1. Extract the tar file, then run python setup.py install inside of the extracted directory.
  2. pip install packagename.tar.gz from example # 7 in https://pip.pypa.io/en/stable/reference/pip_install/#examples

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


回答 0

从表面上看,都做同样的事情:无论是做python setup.py install还是pip install <PACKAGE-NAME>会安装Python包的你,有大惊小怪的最低金额。

但是,使用pip具有一些其他优点,使其更易于使用。

  • pip将自动为您下载软件包的所有依赖项。相反,如果使用setup.py,则通常必须手动搜索并下载依赖项,这很乏味并且会令人沮丧。
  • pip跟踪各种元数据,使您可以使用一个命令轻松卸载和更新软件包:pip uninstall <PACKAGE-NAME>pip install --upgrade <PACKAGE-NAME>。相反,如果您使用setup.py,则要想摆脱它,必须手动手动删除和维护该软件包,这可能容易出错。
  • 您不再需要手动下载文件。如果您使用setup.py,则必须访问图书馆的网站,弄清楚从哪里下载,提取文件,运行setup.py…相比之下,pip会自动搜索Python软件包索引(PyPi)来查看该软件包是否存在,以及会自动为您下载,解压缩并安装该软件包。除了少数exceptions,几乎所有真正有用的Python库都可以在PyPi上找到。
  • pip可让您轻松安装轮子,这是Python发行版的新标准。有关轮子的更多信息
  • pip提供了与using良好集成的其他好处virtualenv,该程序使您可以运行多个项目,这些项目需要计算机上具有冲突的库和Python版本。更多信息
  • pip默认情况下与Python 2.x系列的Python 2.7.9及Python 3.x系列的Python 3.4.0及更高版本捆绑在一起,从而更加易于使用。

因此,基本上,使用点子。它仅提供对的改进python setup.py install


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

pip本身并不需要教程。90%的时间,您真正需要的唯一命令是pip install <PACKAGE-NAME>。就是说,如果您想了解更多有关pip的功能的详细信息,请参阅:

通常也建议同时使用pip和virtualenv。如果您是Python的初学者,我个人认为最好只使用pip并在全球范围内安装软件包,但最终我还是认为在处理更严重的项目时,您应该过渡到使用virtualenv。

如果您想了解有关一起使用pip和virtualenv的更多信息,请参见:

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

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

  • 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 install wheels, 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.

So basically, use pip. It only offers improvements over using 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:

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:

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.

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


回答 1

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

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

总结:使用pip

python setup.py install is 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.

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

In summary: use pip.


回答 2

问题是关于安装包含python软件包而不是NOT 的本地tarball的首选方法关于将软件包上传到索引服务(如PyPi)的好处。

至少我知道一些软件发行商不会将其软件包上传到PyPi,而是要求开发人员从其网站下载软件包并进行安装。

python setup.py安装

这可以工作,但不建议这样做。无需解压缩tarball文件并进入其中以运行setup.py文件。

点安装../path/to/packagename.tar.gz

这是设计和首选的方式。简洁并与PyPi样式的包对齐。

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

The question is about the preferred method to install a local tarball containing a python package, NOT about the advantage of uploading package to an indexing service like 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.

python setup.py install

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

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

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

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


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。