简体   繁体   English

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

[英]Difference between 'python setup.py install' and 'pip install'

I have an external package I want to install into my python virtualenv from a tar file. 我有一个外部包我想从tar文件安装到我的python virtualenv中。 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. 解压缩tar文件,然后在解压缩的目录中运行python setup.py install
  2. pip install packagename.tar.gz from example # 7 in https://pip.pypa.io/en/stable/reference/pip_install/#examples https://pip.pypa.io/en/stable/reference/pip_install/#examples中的示例#7 pip install packagename.tar.gz

Is if there is any difference doing them in these 2 ways. 如果用这两种方式做它们有什么不同。

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. 从表面上看,两者都做同样的事情:做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. pip将自动为您下载包的所有依赖项。 In contrast, if you use setup.py , you often have to manually search out and download dependencies, which is tedious and can become frustrating. 相反,如果你使用setup.py ,你经常需要手动搜索和下载依赖项,这是乏味的,可能会令人沮丧。
  • 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> . pip跟踪各种元数据,使您可以使用单个命令轻松卸载和更新软件包: pip uninstall <PACKAGE-NAME>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. 相反,如果使用setup.py安装软件包,则必须手动删除并维护软件包(如果要删除它),这可能容易出错。
  • 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. 如果你使用setup.py ,你必须访问该库的网站,找出它的下载位置,解压缩文件,运行setup.py ...相比之下,pip会自动搜索Python包索引 (PyPi)以查看是否包存在那里,并将自动下载,提取和安装包。 With a few exceptions, almost every single genuinely useful Python library can be found on PyPi. 除了少数例外,几乎每个真正有用的Python库都可以在PyPi上找到。
  • pip will let you easily install wheels, which is the new standard of Python distribution. pip将让你轻松安装 wheel,这是Python发行版的新标准。 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. pip提供了与使用virtualenv完美集成的额外好处, virtualenv是一个允许您在计算机上运行需要冲突的库和Python版本的多个项目的程序。 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与Python 2.x系列中的Python 2.7.9以及Python 3.x系列中的Python 3.4.0捆绑在一起,使其更易于使用。

So basically, use pip. 基本上,请使用pip。 It only offers improvements over using python setup.py install . 它只提供了使用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. pip本身并不需要教程。 90% of the time, the only command you really need is pip install <PACKAGE-NAME> . 90%的情况下,您真正​​需要的唯一命令是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可以做些什么的详细信息,请参阅:

It is also commonly recommended that you use pip and virtualenv together. 通常也建议您一起使用pip和virtualenv。 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. 如果你是Python的初学者,我个人认为只需在全球范围内使用pip和安装包就可以了。但最终我认为你应该转换到使用virtualenv来处理更严肃的项目。

If you'd like to learn more about using pip and virtualenv together, see: 如果您想了解有关一起使用pip和virtualenv的更多信息,请参阅:

python setup.py install is the analog of make install: it's a limited way to compile and copy files to destination directories. python setup.py install是make install的类比:它是一种将文件编译和复制到目标目录的有限方法。 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. pip是一个包管理器,可以安装,升级,列出和卸载包,比如熟悉的包管理器,包括: dpkg, apt, yum, urpmi, ports等。引擎盖下,它将运行python setup.py install ,但具体用于控制最终安装的方式和位置的选项。

In summary: use pip . 总结:使用pip

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

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