distutils.core和setuptools有什么区别

用pycharm自动生成的setup文件,使用的是distutils.core里的setup

from distutils.core import setup

但我看很多人写的setup,用的是setuptools

from setuptools import setup, find_packages

这个distutils和setuptools分别是什么背景,怎么同样功能出现两个包呢?

阅读 9.4k
1 个回答

学会看官方文档,distutils的介绍(distutils - python2):

The distutils package provides support for building and installing additional modules into a Python installation. The new modules may be either 100%-pure Python, or may be extension modules written in C, or may be collections of Python packages which include modules coded in both Python and C.

也就是说,整个distutils包就是负责建立Python扩展模块的安装器用的。
然后是文档的第二段:

Most Python users will not want to use this module directly, but instead use the cross-version tools maintained by the Python Packaging Authority. In particular, setuptools is an enhanced alternative to distutils that provides:

看见了吧,大部分Python用户会使用更先进的setuptools模块,然后文档下面列了几点setuptools的优点,这里就不贴出来了。

那么为什么Pycharm会用distutils呢?不是说setuptools更强大么?

原因很简单:
distutilsPython标准模块,setuptools是第三方模块。而Pycharm不知道你是否安装了setuptools,为避免不必要的麻烦,当然要用标准模块了。

然后我们看一下setuptools.setup到底是个什么东西,在setuptools/__init__.py中有这么一句:

setup = distutils.core.setup

就是这样。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题