【问题标题】:Why does "python setup.py sdist" create unwanted "PROJECT-egg.info" in project root directory?为什么“python setup.py sdist”会在项目根目录中创建不需要的“PROJECT-egg.info”?
【发布时间】:2011-04-16 08:19:34
【问题描述】:

当我跑步时

  python setup.py sdist

它在我的 ./dist 目录中创建一个 sdist。这包括我的“dist”文件夹内的 zip 中的“PROJECT-egg.info”文件,我不使用它,但它不会伤害我,所以我忽略了它。

我的问题是为什么它在我的项目根目录中创建一个“PROJECT-egg.info”文件夹?我可以让它停止创建这个吗?如果没有,我可以在创建 sdist 后立即删除它吗?

我正在使用从 setuptools 导入的“setup”功能。 WindowsXP、Python2.7、Setuptools 0.6c11、Distribute 0.6.14。

我的设置配置如下:

{'author': 'Jonathan Hartley',
 'author_email': 'tartley@tartley.com',
 'classifiers': ['Development Status :: 1 - Planning',
                 'Intended Audience :: Developers',
                 'License :: OSI Approved :: BSD License',
                 'Operating System :: Microsoft :: Windows',
                 'Programming Language :: Python :: 2.7'],
 'console': [{'script': 'demo.py'}],
 'data_files': [('Microsoft.VC90.CRT',
                 ['..\\lib\\Microsoft.VC90.CRT\\Microsoft.VC90.CRT.manifest',
                  '..\\lib\\Microsoft.VC90.CRT\\msvcr90.dll'])],
 'description': 'Utilities for games and OpenGL graphics, built around Pyglet.\n',
 'keywords': '',
 'license': 'BSD',
 'long_description': "blah blah blah",
 'name': 'pygpen',
 'options': {'py2exe': {'ascii': True,
                        'bundle_files': 1,
                        'dist_dir': 'dist/pygpen-0.1-windows',
                        'dll_excludes': [],
                        'excludes': ['_imaging_gif',
                                     '_scproxy',
                                     'clr',
                                     'dummy.Process',
                                     'email',
                                     'email.base64mime',
                                     'email.utils',
                                     'email.Utils',
                                     'ICCProfile',
                                     'Image',
                                     'IronPythonConsole',
                                     'modes.editingmodes',
                                     'startup',
                                     'System',
                                     'System.Windows.Forms.Clipboard',
                                     '_hashlib',
                                     '_imaging',
                                     '_multiprocessing',
                                     '_ssl',
                                     '_socket',
                                     'bz2',
                                     'pyexpat',
                                     'pyreadline',
                                     'select',
                                     'win32api',
                                     'win32pipe',
                                     'calendar',
                                     'cookielib',
                                     'difflib',
                                     'doctest',
                                     'locale',
                                     'optparse',
                                     'pdb',
                                     'pickle',
                                     'pyglet.window.xlib',
                                     'pyglet.window.carbon',
                                     'pyglet.window.carbon.constants',
                                     'pyglet.window.carbon.types',
                                     'subprocess',
                                     'tarfile',
                                     'threading',
                                     'unittest',
                                     'urllib',
                                     'urllib2',
                                     'win32con',
                                     'zipfile'],
                        'optimize': 2}},
 'packages': ['pygpen'],
 'scripts': ['demo.py'],
 'url': 'http://code.google.com/p/edpath/',
 'version': '0.1',
 'zipfile': None}

【问题讨论】:

  • FTR:相关问题(但不重复:这个是关于 sdist 期间创建的 egg-info 文件,另一个是关于安装期间创建的 egg-info):stackoverflow.com/q/23460191/821378

标签: python setuptools distutils distribute setup.py


【解决方案1】:

此目录是作为源代码分发构建过程的一部分而有意创建的。稍微看看developer guide for setuptools,您就会知道原因:

但是,请务必忽略 处理的 distutils 文档 使用 MANIFEST 或它是如何生成的 来自 MANIFEST.in; setuptools 防护罩 你从这些问题中并不起作用 在任何情况下都是一样的。不像 distutils, setuptools 重新生成 源分发清单文件 每次构建源时 分发,并在内部构建 项目的 .egg-info 目录,out 你的主要项目的方式 目录。因此,您无需 担心它是否是最新的 与否。

您可以在构建完成后安全地删除该目录。

奖金编辑:

我在我的许多 Python 项目的setup.py 中自定义了clean 命令,以删除*.egg-infodistbuild*.pyc 以及其他文件。这是一个在setup.py 中如何完成的示例:

import os
from setuptools import setup, Command

class CleanCommand(Command):
    """Custom clean command to tidy up the project root."""
    user_options = []
    def initialize_options(self):
        pass
    def finalize_options(self):
        pass
    def run(self):
        os.system('rm -vrf ./build ./dist ./*.pyc ./*.tgz ./*.egg-info')

# Further down when you call setup()
setup(
    # ... Other setup options
    cmdclass={
        'clean': CleanCommand,
    }
)

为了说明,在一个名为“poop”的虚拟项目上运行python setup.py build(是的,我很成熟)后,会发生这种情况:

$ python setup.py build
running build
running build_py
creating build
creating build/lib
creating build/lib/poop
copying poop/__init__.py -> build/lib/poop

现在如果我们运行python setup.py clean:

$ python setup.py clean
running clean
removed `./build/lib/poop/__init__.py'
removed directory: `./build/lib/poop'
removed directory: `./build/lib'
removed directory: `./build'

多田!

【讨论】:

  • 非常感谢您的洞察力。我永远无法弄清楚我是否应该在文档中搜索 distutils 或 setuptools 或分发。在选择你的答案之前,我会稍等片刻,以防万一我还没有发现类似 kwarg 的东西告诉 setuptools 不要这样做。
  • 所以我在我的 Makefile 中添加了行以删除“make clean”上的 *.egg-info,或者在执行“make sdist”之后。感谢您向我保证,这不一定会破坏一切。
  • 不客气。这正是我在setup.py 中所做的。我很高兴我能提供帮助! :)
  • @jathanism 您是如何将这种额外的行为添加到您的setup.py 中的,例如,python setup.py clean 会删除.egg-INFO 目录?
  • 看看这个setup.py,看看我是怎么做到的。这很容易。
【解决方案2】:

请注意,您可以让 PROJECT.egg-info 工件从您的 sdist 中完全消失。

命令setup.py egg_info默认会使用source root作为egg base,导致PROJECT.egg-info目录被打包到sdist中。

您可以通过传递选项--egg-base 来配置egg base。 这将在其他地方创建PROJECT.egg-info 目录,将其完全排除在您的源代码分发之外。您也可以使用setup.cfg 来设置该属性。

以下用于创建不带PROJECT.egg-info 的 sdist 的命令适用于我:

python setup.py egg_info --egg-base /tmp sdist

或者setup.cfg

[egg_info]
egg_base = /tmp

【讨论】:

  • 遗憾的是,setuptools 在某些时候故意破坏了对绝对egg_base 路径名的支持。现在发出以下致命错误:setup() arguments must *always* be /-separated paths relative to the setup.py directory, *never* absolute paths. Sigh.
【解决方案3】:

-egg.info 文件夹并不总是可以删除的临时工件。

例如,如果您使用pip install -e YOURPACKAGE 进行“可编辑”安装(通过类似python setup.py develop 的符号链接工作,因此您不必每次在本地编辑软件包时都重新安装),-egg.info当您的包导入另一个源时,运行时需要该文件夹。如果它不存在,您将收到DistributionNotFound 错误。

【讨论】:

  • 我早些时候投了赞成票,但现在我意识到这个答案并没有澄清:这适用于 包装 时,还是仅在安装时适用?我们可以合理地假设可编辑安装可以在安装过程中从 sdist 生成它自己的 egg info 文件夹。同时,问题更集中在构建 sdist 时创建的 egg info 文件夹。
【解决方案4】:

Pythons 打包和构建系统已损坏恕我直言。因此,对于人们认为开箱即用的事情,有许多技巧和变通方法。

但是,我发现删除 *.egg-info 的“最干净”的方法是使用普通的 clean --all 开关和 egg_info 将 *.egg-info 文件放在将被清除的子文件夹中通过 clean 命令。这里举个例子:

在您的setup.cfg 中使用如下内容:

[egg_info]
egg_base = ./build/lib

其中./build/libclean --all 将删除的文件夹。然后在使用 setuptools 构建项目时,使用带有 --all 标志的 clean 命令,例如

python setup.py bdist_wheel clean --all

如果您还想构建源包,请确保在 sdist 之前构建 bdist_wheel,以便存在 build/lib 文件夹,例如:

python setup.py bdist_wheel sdist clean --all

【讨论】:

    【解决方案5】:

    jathanism's method 的替代解决方案可能是使用 egg_info 挂钩。我在每次构建之前用它来清理:

    from pathlib import Path
    from setuptools import setup
    from setuptools.command.egg_info import egg_info
    
    here = Path(__file__).parent.resolve()
    
    class CleanEggInfo(egg_info):
        def run(self):
           shutil.rmtree(here.joinpath('build'), ignore_errors=True)
           shutil.rmtree(here.joinpath('dist'), ignore_errors=True)
    
            for d in here.glob('*.egg-info'):
                shutil.rmtree(d, ignore_errors=True)
    
            egg_info.run(self)
    
    setup(
        cmdclass={
            'egg_info': CleanEggInfo,
        }
    )
    

    【讨论】:

      猜你喜欢
      • 2021-03-05
      • 2013-03-28
      • 2014-12-20
      • 2021-03-02
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-05
      相关资源
      最近更新 更多